UVA 10926 How Many Dependencies?

来源:互联网 发布:网络管理和信息安全 编辑:程序博客网 时间:2024/05/17 04:21

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;}
原创粉丝点击