UVA 10926 How Many Dependencies?

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 N, 0 < 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

题意:
给你几个任务以及这些任务的依赖关系,输出子任务最多的任务的代号


#include <iostream>
#include <cstring>
using namespace std;
int connect[110][110];//记录下依赖关系
int mark[110];//记录访问情况
int T,s=0;
void dfs(int n)
{
    if(mark[n]==1)
        return;
    mark[n]=1;
    for(int i=0;i<=T;i++)
    {
        if(connect[n][i]==1&&mark[i]!=1)
        {
            s++;//子任务数+1
            dfs(i);
        }

    }
}
int main()
{
    int i,k,j,max;
    int sum[110]={0};//记录每个任务的子任务个数
    while(cin>>T&&T)
    {
        max=1;
        memset(connect,0,sizeof(connect));
        for(i=1;i<=T;i++)
        {
            cin>>k;//输入子任务个数
           while(k--)
            {
                cin>>j;
                connect[i][j]=1;
            }
        }
        for(i=1;i<=T;i++)
        {
            memset(mark,0,sizeof(mark));
            dfs(i);//从第一个任务开始搜索
            sum[i]=s;//储存在数组
            s=0;
        }
        for(i=1;i<=T;i++)
        {
            if(sum[i]>sum[max])
                max=i;//输有出最大子任务的任务下标
        }
        cout<<max<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值