首页 > 代码库 > NBUT 1218 You are my brother

NBUT 1218 You are my brother

$dfs$。

记录一下每一个节点的深度就可以了。

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespace std;int n;int dep[3000],in[3000];vector<int>g[3000];void dfs(int x,int y){    dep[x]=y;    for(int i=0;i<g[x].size();i++)        dfs(g[x][i],y+1);}int main(){    while(~scanf("%d",&n))    {        for(int i=0;i<=2000;i++) g[i].clear();        memset(in,0,sizeof in);        for(int i=1;i<=n;i++)        {            int A,B; scanf("%d%d",&A,&B);            g[B].push_back(A);            in[A]++;        }        for(int i=1;i<=2000;i++)        {            if(in[i]==0)            {                dfs(i,1);            }        }        if(dep[1]==dep[2]) printf("You are my brother\n");        else if(dep[1]>dep[2]) printf("You are my elder\n");        else printf("You are my younger\n");    }    return 0;}

 

NBUT 1218 You are my brother