UVA 10926

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

3
1 2
1 3
0
4
2 2 4
0
2 2 4
0
0

Sample Output

1
1

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 109
using 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);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值