标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27820 | Accepted: 11208 |
Description
Input
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
Source
/* ID: LinKArftc PROG: 2186.cpp LANG: C++ */ #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <string> #include <utility> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define eps 1e-8 #define randin srand((unsigned int)time(NULL)) #define input freopen("input.txt","r",stdin) #define debug(s) cout << "s = " << s << endl; #define outstars cout << "*************" << endl; const double PI = acos(-1.0); const int inf = 0x3f3f3f3f; const int INF = 0x7fffffff; typedef long long ll; const int maxn = 10010; const int maxm = 50010; int n, m; struct Edge { int v, next; } edge[maxm]; int head[maxn], tot; void init() { tot = 0; memset(head, -1, sizeof(head)); } void addedge(int u, int v) { edge[tot].v = v; edge[tot].next = head[u]; head[u] = tot ++; } int dfn[maxn], low[maxn], belong[maxn];//每个点属于哪个连通分量 int scc;//连通分量总数,下标 1~scc int Time; int ins[maxn]; stack <int> s; vector <int> vec[maxn];//记录每个连通分量内的点 void tarjan(int u) { int v; low[u] = dfn[u] = ++ Time; s.push(u); ins[u] = true; for (int i = head[u]; i + 1; i = edge[i].next) { v = edge[i].v; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (ins[v]) low[u] = min(low[u], dfn[v]);//写出low[u] = min(low[u], low[v]);也可以 } if (low[u] == dfn[u]) { scc ++; do { v = s.top(); s.pop(); ins[v] = false; belong[v] = scc; vec[scc].push_back(v); } while (u != v); } } int outdeg[maxn]; int main() { //input; while (~scanf("%d %d", &n, &m)) { init(); int u, v; for (int i = 1; i <= m; i ++) { scanf("%d %d", &u, &v); addedge(u, v); } while (!s.empty()) s.pop(); for (int i = 1; i <= maxn; i ++) vec[i].clear(); memset(dfn, 0, sizeof(dfn)); memset(ins, 0, sizeof(ins)); Time = 0; scc = 0; for (int i = 1; i <= n; i ++) { if (!dfn[i]) tarjan(i); } memset(outdeg, 0, sizeof(outdeg)); for (int u = 1; u <= n; u ++) { for (int i = head[u]; i + 1; i = edge[i].next) { int v = edge[i].v; if (belong[u] == belong[v]) continue; outdeg[belong[u]] ++; } } int ans = 0; bool has = false; bool flag = false; int ii; for (int i = 1; i <= scc; i ++) { if (outdeg[i] == 0) { ii = i; if (has) { flag = false; break; } else { has = true; flag = true; } } } if (flag) printf("%d\n", vec[ii].size()); else printf("0\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/LinKArftc/p/4905916.html