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

未必还有孤立的结点????不是一棵树么?为什么这么写错了,而改成那样就对了?

Posted by xiangdaww at 2014-10-04 17:07:06 on Problem 2342
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int dp[6001][2],head[6001],f[6001],cnt;
bool vis[6001];
struct Edge
{
    int v;
    int next;
}edge[6000<<1+1];
void add(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt;
    ++cnt;
}
int dfs(int u)
{
    vis[u]=1;
    dp[u][0]=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        dfs(v);
        dp[u][0]+=max(dp[v][0],dp[v][1]);
        dp[u][1]+=dp[v][0];
    }
    return max(dp[u][0],dp[u][1]);
}
int main()
{
    int N,K,L,i;
    while(scanf("%d",&N)!=EOF)
    {
        memset(head,-1,sizeof(head));
        cnt=0;
        for(i=1;i<=N;++i)
        {
            scanf("%d",&dp[i][1]);
            vis[i]=0;
            f[i]=0;
        }
        while(scanf("%d%d",&L,&K)!=EOF&&(L+K))
        {
            add(K,L);
            ++f[L];
        }
        for(i=1;i<=N;++i)      //这里这样写就对了
            if(!f[i])
            {
                printf("%d\n",dfs(i));
                break;
            }
    }
    return 0;
}


如果改成下面这样就错了:why ????
int root=0;
bool flag=1;
while(scanf("%d%d",&L,&K)!=EOF&&(L+K))
        {
            add(K,L);
            if(flag||root=L)
            {
               flag=false;
               root=K;
            }
        }
    printf("%d\n",dfs(root));
          

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