标签:
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
【题意】
N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
【题解】
找强连通分量,缩点。记f[i]为缩完点后的新图中各点入度,g[i]为出度,ans1为f[i]==0的点的数目,ans2为g[i]==0的点的数目则第一问为ans1,第二问则为max{ans1,ans2}。
至于第二问的解释,我的想法是对于得到的DAG图,考虑其中的出度为0的点和入度为0的点组成的点集V,将这些点相连,最多这需要max{ans1,ans2}条边,就能使整个图成为强连通分量。
但是请注意,大家可能都没发现,这个结论的前提是DAG图是连通的情况下才成立。如果DAG图有多个连通分量,则还要考虑将多个连通分量合并的所需代价。幸运的是,这道题保证了只有一个连通分量。(题目第一句话所说)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<stack> 5 #include<vector> 6 using namespace std; 7 #define N 106 8 int n; 9 int tot; 10 11 int head[N]; 12 int vis[N]; 13 int tt; 14 int scc; 15 stack<int>s; 16 int dfn[N],low[N]; 17 int col[N]; 18 19 struct Node 20 { 21 int from; 22 int to; 23 int next; 24 }edge[N<<6]; 25 void init() 26 { 27 tot=0; 28 scc=0; 29 tt=0; 30 memset(head,-1,sizeof(head)); 31 memset(dfn,-1,sizeof(dfn)); 32 memset(low,0,sizeof(low)); 33 memset(vis,0,sizeof(vis)); 34 memset(col,0,sizeof(col)); 35 } 36 void add(int s,int u)//邻接矩阵函数 37 { 38 edge[tot].from=s; 39 edge[tot].to=u; 40 edge[tot].next=head[s]; 41 head[s]=tot++; 42 } 43 void tarjan(int u)//tarjan算法找出图中的所有强连通分支 44 { 45 dfn[u] = low[u]= ++tt; 46 vis[u]=1; 47 s.push(u); 48 int cnt=0; 49 for(int i=head[u];i!=-1;i=edge[i].next) 50 { 51 int v=edge[i].to; 52 if(dfn[v]==-1) 53 { 54 // sum++; 55 tarjan(v); 56 low[u]=min(low[u],low[v]); 57 } 58 else if(vis[v]==1) 59 low[u]=min(low[u],dfn[v]); 60 } 61 if(dfn[u]==low[u]) 62 { 63 int x; 64 scc++; 65 do{ 66 x=s.top(); 67 s.pop(); 68 col[x]=scc; 69 vis[x]=0; 70 }while(x!=u); 71 } 72 } 73 int main() 74 { 75 while(scanf("%d",&n)==1) 76 { 77 init(); 78 for(int i=1;i<=n;i++) 79 { 80 int x; 81 scanf("%d",&x); 82 while(x!=0) 83 { 84 add(i,x); 85 scanf("%d",&x); 86 } 87 } 88 89 for(int i=1;i<=n;i++) 90 { 91 if(dfn[i]==-1) 92 { 93 tarjan(i); 94 } 95 } 96 //printf("%d\n",scc); 97 int inde[N]; 98 int outde[N]; 99 memset(inde,0,sizeof(inde)); 100 memset(outde,0,sizeof(outde)); 101 for(int i=0;i<tot;i++) 102 { 103 int a=edge[i].from; 104 int b=edge[i].to; 105 if(col[a]!=col[b]) 106 { 107 inde[col[b]]++; 108 outde[col[a]]++; 109 } 110 } 111 112 int ans1=0; 113 int ans2=0; 114 for(int i=1;i<=scc;i++) 115 { 116 if(inde[i]==0) 117 { 118 ans1++; 119 } 120 if(outde[i]==0) 121 { 122 ans2++; 123 } 124 } 125 126 printf("%d\n",ans1); 127 128 if(scc==1) 129 { 130 printf("0\n"); 131 continue; 132 } 133 printf("%d\n",max(ans1,ans2)); 134 135 136 } 137 return 0; 138 }
poj 1236 Network of Schools(tarjan+缩点)
标签:
原文地址:http://www.cnblogs.com/UniqueColor/p/4737822.html