UVA 10926

来源:互联网 发布:lcp linux 编辑:程序博客网 时间:2024/06/16 22:00

Problem G - How Many Dependencies?

Time Limit: 1 second

In this problem you will need to find out which task has the most number of dependencies. A task A depends on another task B if B is a direct or indirect dependency of A.

For example, if A depends on B and B depends on C, then A has two dependencies, one direct and one indirect.

You can assume there will be no cyclic dependencies in the input.

Input

The input consists of a set of scenarios. Each scenario begins with one integer N0 < N ≤ 100, in a line indicating how many tasks this scenario contains. Then there will be N lines, one for each task. Each line will contain an integer 0 ≤ T ≤ N-1, the number of direct dependencies of that task, plus T integers, the identifiers of that dependencies. Tasks are numbered from 1 to N.

The input ends with a scenario where N = 0.

Output

For each scenario, print the number of the task with the greatest number of dependencies alone in a line. If there are ties, show the task with the lowest identifier.

Sample Input

31 21 3042 2 402 2 400

Sample Output

11

Problem setter: Jo�o Paulo Fernandes Farias


本题很简单,用类似floyd的方法求一次传递闭包就可以了。如果i可以到k,k可以到j,则i可以到j。

a[i][j]=a[i][j]|(a[i][k]&a[k][j])

#include<cstdio>#include<string>#define maxn 109using namespace std;int a[maxn][maxn];int main(){    int n;    while(scanf("%d",&n),n)    {        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)            if(i==j)a[i][j]=1;            else a[i][j]=0;        for(int i=1;i<=n;i++)        {            int num;            scanf("%d",&num);            for(int j=0;j<num;j++)            {                int x;                scanf("%d",&x);                a[i][x]=1;            }        }        for(int k=1;k<=n;k++)            for(int i=1;i<=n;i++)                for(int j=1;j<=n;j++)        a[i][j]=a[i][j]|(a[i][k]&a[k][j]);        int cur=-1,ans;        for(int i=1;i<=n;i++)        {            int num=0;            for(int j=1;j<=n;j++)                if(a[i][j])num++;            if(num>cur)            {                ans=i;                cur=num;            }        }        printf("%d\n",ans);    }}