CSUOJ 1010: Water Drinking

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1010

这道题是要我们找到一个队伍的最后一个骆驼的编号,队伍的要求有两点

1. 骆驼数最少

2. 与池塘相连

#include<cstdio>
#include<cstring>
#include<cstdlib>
#define MAXN 100005

int next[MAXN], s[MAXN];
int N, top, k;

int main()
{
while( scanf( "%d", &N) == 1)
{
int n = N;
top = 0;
memset( next, -1, sizeof next);
while( n --)
{
int a, b;
scanf( "%d%d", &a, &b);
if( a == 0) s[top ++] = b;
else
next[a] = b;
}
int min = MAXN;
for( int i = 0; i < top; i ++)
{
int ans = 1, t, j;
j = s[i];
while( j != -1)
{
ans ++;
t = j;
j = next[j];

}
if( ans < min ) {
min = ans;
k = t;
}
if( ans == min && t < k)
k = t;
}
printf( "%d\n", k);
}
return 0;
}

用并查集没写过...

你可能感兴趣的:(water)