I've been debugging for two hours, and I've been crying.
Write a little bit of experience
To tell you that a herd of cows has n-head (N<10^4)
Then tell you m to envy relationship such as a bull envy B bull
And envy can pass like a envy b b envy C then a envy C
Now ask how many cows are admired by all the cows
Analysis: My first thought was to traverse each cow and then build the opposite side from each cow and then DFS to see if it could sweep all the cows, but the time complexity O (n) cannot withstand
Later thought can be like flody delivery closure, but the same time complexity withstand
And then I thought this could be strong connectivity.
The first thing we want is a question.
For a direction-free graph
Satisfy an ox to be envied by all cows is: 1 figure connected 2 The cow's out of 0 (because he is the envy of all the cows and then if he envied the other cows will form a ring)
For the subject is a ring with strong connectivity to shrink point into a direction-free graph and then according to the two conditions to determine the
But the first thing I wrote was that I had isolated points in the picture.
Then it was after the break of the brain. It hurts to think.
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intMAXN =50005;7 8 intN, M;9 inttot, CNT;Ten structNode { One intto, next; A }E[MAXN]; - intHEAD[MAXN]; - the voidAddintUintv) { -E[tot].to =v; -E[tot].next =Head[u]; -Head[u] = tot++; + } - intS[MAXN]; + intINS[MAXN]; A intSC; at intans; - intNUM[MAXN]; - - intLOW[MAXN], DFN[MAXN]; - - voidinit () { intot =1; -Memset (Head,0,sizeof(head)); tomemset (Low,0,sizeof(Low)); +memset (DFN,0,sizeof(DFN)); -memset (INS,0,sizeof(INS)); thememset (NUM,0,sizeof(num)); *CNT =1; $sc =0;Panax NotoginsengAns =0; - } the + voidDfsintu) { AS[sc++] =u; theIns[u] =1; + for(inti = Head[u]; I i =E[i].next) { - intv =e[i].to; $ if(!Dfn[v]) { $DFN[V] = low[v] = cnt++; - Dfs (v); -Low[u] =min (Low[u], low[v]); the}Else if(Ins[v] &&!Num[v]) { -Low[u] =min (Low[u], dfn[v]);Wuyi } the } - if(Low[u] = =Dfn[u]) { Wuans++; - intx; About Do { $x = s[--SC]; -NUM[X] =ans; -INS[X] =0; -} while(X! =u); A } + the } - intOUTED[MAXN]; $ the intMain () { the intu, v; the while(EOF! = scanf ("%d%d", &n, &m)) { the init (); - for(inti =1; I <= m; i++) { inscanf"%d%d", &u, &v); the Add (U, v); the } About for(inti =1; I <= N; i++) { the if(!Dfn[i]) { theDfn[i] = low[i] = cnt++; the DFS (i); + } - } the //for (int i = 1; I <= n; i++) {Bayi //printf ("%d", Num[i]); the //} puts (""); the intAns_num =-1; -memset (outed,0,sizeof(outed)); - for(inti =1; I <= N; i++) { the //printf ("i =%d\n", i); the if(Num[i] = =0) { theAns_num =0; the Break; - } the for(intj = Head[i]; J; j =E[j].next) { the intx =e[j].to; the if(Num[i]! =Num[x]) {94outed[num[i]]++; the } the } the }98 //for (int i = 1; I <= n; i++) { About //printf ("*%d", Outed[i]); - //} puts ("");101 //printf ("ans =%d\n", ans);102 intFlag =0;103 if(Ans_num! =0) {104Ans_num =0; the intAx =0;106 for(inti =1; I <= ans; i++) {107 if(Outed[i] = =0) {108flag++;109 if(Flag >=2) { theAns_num =0;111 Break; the }113Ax =i; the } the } the if(Flag <2) {117 for(inti =1; I <= N; i++) {118 if(Num[i] = =Ax) {119ans_num++; - }121 }122 }123 }124printf"%d\n", ans_num); the }126 return 0;127}
View Code
Poj2186popular cows "Strong Connect" "Debug Two Hours"