标签:etc add class order UI ace wrap string perl
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2342 | Accepted: 1380 |
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
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <stack> using namespace std; stack<int> s; const int maxn =10010,maxm = 50010; int ans,tott,n,m,head[maxn],to[maxm],nextt[maxm],tot = 1,pre[maxn],low[maxn],dfs_clock,scc[maxn]; void add(int x,int y) { to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; } void tarjan(int x) { pre[x] = low[x] = ++dfs_clock; s.push(x); for (int i = head[x];i;i = nextt[i]) { int v = to[i]; if (!pre[v]) { tarjan(v); low[x] = min(low[x],low[v]); } else if (!scc[v]) low[x] = min(low[x],pre[v]); } if (pre[x] == low[x]) { tott++; int cnt = 0; while (1) { int t = s.top(); s.pop(); scc[t] = tott; cnt++; if (t == x) break; } if (cnt >= 2) ans++; } } int main() { scanf("%d%d",&n,&m); for (int i = 1; i <= m; i++) { int a,b; scanf("%d%d",&a,&b); add(a,b); } for (int i = 1; i <= n; i++) if (!scc[i]) tarjan(i); printf("%d\n",ans); return 0; }
标签:etc add class order UI ace wrap string perl
原文地址:http://www.cnblogs.com/zbtrs/p/7520232.html