标签:eve txt space lse push define for mod ide
Input
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d\n", a); #define plld(a) printf("%lld\n", a); #define pc(a) printf("%c\n", a); #define ps(a) printf("%s\n", a); #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 1e5 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m; vector<int> G[maxn]; int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt, out[maxn]; stack<int> S; int vis[maxn]; void dfs(int u) { pre[u] = lowlink[u] = ++dfs_clock; S.push(u); for(int i=0; i<G[u].size(); i++) { int v = G[u][i]; if(!pre[v]) { dfs(v); lowlink[u] = min(lowlink[u], lowlink[v]); } else if(!sccno[v]) lowlink[u] = min(pre[v], lowlink[u]); } if(lowlink[u] == pre[u]) { scc_cnt++; for(;;) { int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } } } int main() { int u, v; rd(n), rd(m); for(int i=0; i<m; i++) { rd(u), rd(v); G[u].push_back(v); } for(int i=1; i<=n; i++) if(!pre[i]) dfs(i); // cout << 111 << endl; if(scc_cnt == 1) { pd(n); return 0; } for(int i=1; i<=n; i++) for(int j=0; j<G[i].size(); j++) { int v = G[i][j]; if(sccno[i] != sccno[v]) out[sccno[i]]++; } int res = 0; // cout << scc_cnt << endl; // for(int i=1; i<=scc_cnt; i++) // cout << in[i] << endl; int cnt = 0; for(int i=1; i<=scc_cnt; i++) if(out[i] == 0) cnt++; if(cnt != 1) return puts("0"), 0; for(int i=1; i<=n; i++) if(out[sccno[i]] == 0) res++; pd(res); return 0; }
Popular Cows POJ - 2186(强连通分量)
标签:eve txt space lse push define for mod ide
原文地址:https://www.cnblogs.com/WTSRUVF/p/9700385.html