标签:des style blog http color io os ar for
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11433 | Accepted: 4551 |
Description
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 }
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/code-painter/p/4003406.html