其他分享
首页 > 其他分享> > poj1236 Network of Schools(Tarjan+缩点)

poj1236 Network of Schools(Tarjan+缩点)

作者:互联网

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27058   Accepted: 10670

Description

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B 
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school. 

Input

The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

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

Sample Output

1
2

Source

IOI 1996   题目大意 单组数据,n个点,之后1~n行输入每行有向连接的点,0结尾 任务A:至少选择几台电脑,通过文件传输使得n台电脑都能接收到文件 任务B:至少添加几条连接(边),使得通过任意一台电脑能到达所有n台电脑   思路 任务A:缩点后求解入度为0的点的个数 任务B:缩点后求解max(入度为0的点的个数,出度为0的点的个数)  
 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int N=1e2+5;
 7 int low[N],dfn[N],Stack[N],belong[N],rdu[N],cdu[N],vis[N][N];
 8 bool inStack[N];
 9 int n,m,tot,tag,top;
10 vector<int> g[N],vec[N];
11 
12 void tarjan(int u){
13     int v;
14     low[u]=dfn[u]=++tot;
15     Stack[++top]=u;
16     inStack[u]=true;
17     for(int i=0;i<g[u].size();i++){
18         v=g[u][i];
19         if(dfn[v]==0){
20             tarjan(v);
21             low[u]=min(low[u],low[v]);
22         }
23         else if(inStack[v]==true){
24             low[u]=min(low[u],dfn[v]);
25         }
26     }
27     if(dfn[u]==low[u]){
28         tag++;
29         do{
30             v=Stack[top--];
31             inStack[v]=false;
32             belong[v]=tag;
33         }while(u!=v);
34     }
35 }
36 
37 int main(){
38     scanf("%d",&n);
39     for(int i=1;i<=n;i++){
40         int x;
41         while(scanf("%d",&x),x){
42             g[i].push_back(x);
43         }
44     }
45     for(int i=1;i<=n;i++){
46         if(dfn[i]==0){
47             tot=0;
48             tarjan(i);
49         }
50     }
51     if(tag==1){
52         printf("1\n0\n");
53         return 0;
54     }
55     for(int i=1;i<=n;i++){
56         for(int j=0;j<g[i].size();j++){
57             int x=g[i][j],bi=belong[i],bx=belong[x];
58             if(bi!=bx&&vis[bi][bx]==0){
59                 vec[bi].push_back(bx);
60                 vis[bi][bx]=1;
61             }
62         }
63     }
64     for(int i=1;i<=tag;i++){
65         for(int j=0;j<vec[i].size();j++){
66             int x=vec[i][j];
67             cdu[i]++;
68             rdu[x]++;
69         }
70     }
71 //    for(int i=1;i<=tag;i++){
72 //        printf(" %d",i);
73 //        for(int j=0;j<vec[i].size();j++){
74 //            printf(" %d",vec[i][j]);
75 //        }
76 //        printf("\n");
77 //    }
78     int cnta=0,cntb=0;
79     for(int i=1;i<=tag;i++){
80         if(rdu[i]==0){
81             cnta++;
82         }
83         if(cdu[i]==0){
84             cntb++;
85         }
86     }
87     printf("%d\n",cnta);
88     printf("%d\n",max(cnta,cntb));
89 }

 

标签:缩点,school,int,list,schools,Schools,new,poj1236,software
来源: https://www.cnblogs.com/ChangeG1824/p/11424510.html