Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求测试数据啊~~样例和Discuss里的所有数据都过了怎么就A不了啊。。。。

Posted by saldfj19212 at 2012-03-09 20:04:52 on Problem 1947
求测试数据啊~~样例和Discuss里的所有数据都过了怎么就A不了啊。。。。 



#include<vector>
#include<iostream>
#include <stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define MAX 1500
#define INF 0xffffff
int N,p,ans;
int dp[MAX][MAX],ar[MAX],te[MAX],fa[MAX];
char s1[MAX],s2[MAX];
vector<int>son[MAX];

void dfs(int x)
{
    //cout<<"dp "<<x<<endl;
    if(son[x].size()==0)
      {dp[x][0]=1;//代表和父亲断开
      dp[x][1]=0;//和父亲相连且有1个节点
      }
    else
    {
        for(int i=0;i<son[x].size();i++)
            dfs(son[x][i]);
    }

      if(fa[x]!=-1)
    {
    dp[fa[x]][1]=son[fa[x]].size();
    dp[fa[x]][0]=1;
  //  cout<<p<<" "<<dp[fa[x]][1]<<" = "<<INF<<endl;
   // cout<<"dp fa "<<fa[x]<<endl;

              int temp[MAX];
              memcpy(temp,dp[fa[x]],sizeof(dp[fa[x]]));
    for(int k=1;;k++)
      {

       //   cout<<k<<endl;
      if(dp[x][k]==INF)//本节点选K个孤立点的存在可能,不选已算过
        break;
      else
        for(int p=1;;p++)
          {
              //cout<<p<<" "<<dp[fa[x]][p]<<" = "<<INF<<endl;
           if(temp[p]==INF)//父节点不算本节点的可能数
             {break;cout<<"break"<<endl;}
          else
             {
               //  cout<<"现在节点 "<<x<<" "<<" 父节点数 "<<p<<" 值 "<<" 子节点数 "<<k<<endl;

                 if(temp[k+p]==INF)//第一次赋值,新取一棵树,不断,-1
                   {
                       //cout<<"first dp "<<" 原为 "<<dp[fa[x]][k+p]<<endl;
                       dp[fa[x]][k+p]=temp[p]+dp[x][k]-1;
                  //     cout<<k+p<<" = "<< dp[fa[x]][k+p]<<endl;
                   }
                 else//新取一棵树,不用断-1(刷表)
                  {
                    //  cout<<"no first"<<endl;
                      dp[fa[x]][k+p]=min(temp[p]+dp[x][k]-1,temp[k+p]);
                    //   cout<<k+p<<" = "<< dp[fa[x]][k+p]<<endl;
                  }
             }
          }
      }
    }
    for(int k=0;;k++)
      {if(dp[x][k]==INF)
        break;
      // cout<<"dp 节点"<<x<<" 孤立数 "<<k<<" "<<dp[x][k]<<endl;
      }

}

int main()
{
  //freopen("C:\in.txt","r",stdin);
//   cin>>p>>t;
 // cout<<INF<<endl;
   int T,P;
   while(cin>>N>>P)
   if(N==0)
   cout<<"0"<<endl;
   else
   {
       ans=INF;
       for(int i=0;i<=N;i++)
         son[i].clear();
       for(int i=0;i<MAX;i++)
         for(int j=0;j<MAX;j++)
             dp[i][j]=INF;
       memset(fa,-1,sizeof(fa));
       for(int i=0;i<N-1;i++)
       {
           int f,s;
           cin>>f>>s;
           son[f].push_back(s);
           fa[s]=f;
       }
       dfs(1);
        ans=dp[1][P];
   for(int i=2;i<=N;i++)
     if(dp[i][P]+1<ans)
        ans=dp[i][P]+1;
   cout<<ans<<endl;
   }

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator