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

用father[i]!=i判断不是树的根,为什么这样不行,求教

Posted by 678847360 at 2015-05-22 15:19:35 on Problem 2342
#include<iostream>
#include<string>
#define max(x,y) (x>y?x:y)
using namespace std;
int vis[6010],dp[6010][2],n,father[6010];
void dfs(int node)
{
 int i;
 vis[node]=1;
 for(i=1;i<=n;i++)
  if(!vis[i] && father[i]==node && father[i]!=i)//用father[i]!=i判断不是树的根
  {
   dfs(i);
   dp[node][0]+=max(dp[i][1],dp[i][0]);
   dp[node][1]+=dp[i][0];
  }
}
int main()
{
 int i;
 while(cin>>n)
 {
  memset(dp,0,sizeof(dp));
  for(i=1;i<=n;i++)
   cin>>dp[i][1];
  int temp1,temp2,root=0;
  for(i=1;i<=n;i++)
   father[i]=i;
  //memset(father,0,sizeof(father));
  while(cin>>temp1>>temp2,temp1+temp2>0)
  {
   father[temp1]=temp2;
   if(root==temp1 || root==0)
    root=temp2;
  }
  
  memset(vis,0,sizeof(vis));
  dfs(root);
  int max1=max(dp[root][1],dp[root][0]);
  cout<<max1<<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