poj1236 Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11433 | Accepted: 4551 |
Description
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
Output
Sample Input
5 2 4 3 0 4 5 0 0 0 1 0
Sample Output
1 2
Source
1 /* 2 * Author: Joshua 3 * Created Time: 2014年09月29日 星期一 10时32分59秒 4 * File Name: poj1236.cpp 5 */ 6 #include<cstdio> 7 #define maxn 101 8 #define maxm 10001 9 int n,top,tot,c,num; 10 int head[maxn],color[maxn],rd[maxn],cd[maxn],s[maxn],dfn[maxn],low[maxn]; 11 bool mark[maxn]; 12 struct node 13 { 14 int d,next; 15 } e[maxm]; 16 17 void init() 18 { 19 int x,y; 20 num=c=tot=top=0; 21 for (int i=1;i<=n;++i) 22 { 23 scanf("%d",&y); 24 while (y) 25 { 26 e[++tot].d=y; 27 e[tot].next=head[i]; 28 head[i]=tot; 29 scanf("%d",&y); 30 } 31 } 32 } 33 34 void tarjan(int x) 35 { 36 int y; 37 dfn[x]=low[x]=++num; 38 s[++top]=x; 39 mark[x]=true; 40 for (int i=head[x];i;i=e[i].next) 41 { 42 y=e[i].d; 43 if (!dfn[y]) 44 { 45 tarjan(y); 46 if (low[y]<low[x]) 47 low[x]=low[y]; 48 } 49 else if (mark[y] && low[x]>low[y]) 50 low[x]=low[y]; 51 } 52 if (low[x]==dfn[x]) 53 { 54 ++c; 55 do 56 { 57 y=s[top--]; 58 color[y]=c; 59 mark[y]=false; 60 } 61 while (y!=x); 62 } 63 } 64 65 void solve() 66 { 67 int x,y,csum,rsum,ans; 68 for (int i=1;i<=n;++i) 69 if (!dfn[i]) 70 tarjan(i); 71 if (c==1) 72 { 73 printf("1\n0\n"); 74 return; 75 } 76 for (int i=1;i<=n;++i) 77 for (int j=head[i];j;j=e[j].next) 78 { 79 y=e[j].d; 80 if (color[y]!=color[i]) 81 { 82 rd[color[y]]++; 83 cd[color[i]]++; 84 } 85 } 86 csum=rsum=0; 87 for (int i=1;i<=c;++i) 88 { 89 if (!rd[i]) ++rsum; 90 if (!cd[i]) ++csum; 91 } 92 if (rsum>csum) ans=rsum; 93 else ans=csum; 94 printf("%d\n",rsum); 95 printf("%d\n",ans); 96 } 97 98 int main() 99 { 100 while (scanf("%d",&n)>0) 101 { 102 init(); 103 solve(); 104 } 105 return 0; 106 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。