标签:des com http blog class style div img code size tar
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1132 | Accepted: 713 |
Description
Input
Output
Sample Input
5 4 2 4 3 5 1 2 4 1
Sample Output
1
Hint
_1___Cows 1, 2, and 4 are properly connected and form a complete Round Dance group. Cows 3 and 5 don‘t have the second rope they‘d need to be able to pull both ways, thus they can not properly perform the Round Dance.
/****
5 /****** 2
/ /**TANK**|
\ \********/
\ \******/ 3
\ 4____/ /
\_______/
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <stack> 6 7 using namespace std; 8 9 const int MAX_N = 10005; 10 const int edge = 50005; 11 int N,M; 12 int first[MAX_N],Next[edge],v[edge]; 13 int low[MAX_N],pre[MAX_N],cmp[MAX_N]; 14 int num[MAX_N]; 15 int dfs_clock,scc_cnt; 16 stack<int> S; 17 18 19 void add_edge(int id,int u) { 20 int e = first[u]; 21 Next[id] = e; 22 first[u] = id; 23 } 24 25 void dfs(int u) { 26 pre[u] = low[u] = ++dfs_clock; 27 S.push(u); 28 for(int e = first[u]; e != -1; e = Next[e]) { 29 if(!pre[ v[e] ]) { 30 dfs( v[e] ); 31 low[u] = min(low[u],low[ v[e] ]); 32 } else if(!cmp[ v[e] ]) { 33 low[u] = min(low[u],pre[ v[e] ]); 34 } 35 } 36 37 if(low[u] == pre[u]) { 38 ++scc_cnt; 39 for(;;) { 40 int x = S.top(); S.pop(); 41 cmp[x] = scc_cnt; 42 num[scc_cnt]++; 43 if(x == u) break; 44 } 45 } 46 } 47 48 void scc() { 49 dfs_clock = scc_cnt = 0; 50 memset(cmp,0,sizeof(cmp)); 51 memset(pre,0,sizeof(pre)); 52 53 for(int i = 1; i <= N; ++i) if(!pre[i]) dfs(i); 54 } 55 56 void solve() { 57 int ans = 0; 58 scc(); 59 for(int i = 1; i <= scc_cnt; ++i) { 60 if(num[i] >= 2) ++ans; 61 } 62 63 printf("%d\n",ans); 64 } 65 int main() 66 { 67 、、freopen("sw.in","r",stdin); 68 scanf("%d%d",&N,&M); 69 for(int i = 1; i <= N; ++i) first[i] = -1; 70 for(int i = 0; i < M; ++i) { 71 int u; 72 scanf("%d%d",&u,&v[i]); 73 add_edge(i,u); 74 } 75 76 solve(); 77 return 0; 78 }
标签:des com http blog class style div img code size tar
原文地址:http://www.cnblogs.com/hyxsolitude/p/3697073.html