标签:style 其他 The from 连通 open class hide long
题目大意:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; #define cls(s,h) memset(s,h,sizeof s) const int maxn = 1e5 + 7; int n , m ; int tot; struct edge { int to,from,nxt; }e[maxn << 1]; int head[maxn]; void add_edge(int u , int v ){ e[tot].from = u ; e[tot].to = v; e[tot].nxt = head[u]; head[u] = tot++; } int stack[maxn << 1],low[maxn << 1],dfn[maxn << 1]; int scc,sz[maxn << 1],c[maxn << 1],st,num; void init(){ cls(head,-1); cls(e,0); cls(sz,0); scc = 0; st = 0; } void tanjan(int u){ stack[++st] = u; dfn[u] = low[u] = ++ num; for(int i = head[u],v; ~i ;i = e[i].nxt){ if(c[v = e[i].to]) continue; if(dfn[v]) low[u] = min(low[u],dfn[v]); else tanjan(v),low[u] = min(low[u],low[v]); } if(low[u] == dfn[u]){ c[u] = ++ scc; sz[scc] = 1; while(st && u != stack[st]) //SCC number and the scc ge SCC c[stack[st--]] = scc,sz[scc] ++; st--; } } int out[maxn << 1]; int main(int argc, char const *argv[]) { init(); scanf("%d %d",&n,&m); for(int i = 1,u,v;i <= m ;i ++) scanf("%d %d",&u,&v),add_edge(u,v); for(int i = 1;i <= n;i ++) if(!dfn[i]) tanjan(i); for(int i = 0;i < tot;i ++) if(c[e[i].from] != c[e[i].to]) out[c[e[i].from]] ++; int ans = 0; for(int i = 1;i <= scc;i ++) if(!out[i]) ans = ans ? -1: i; if(~ans) ans = sz[ans]; else ans = 0; printf("%d\n", ans); return 0; }
标签:style 其他 The from 连通 open class hide long
原文地址:https://www.cnblogs.com/DWVictor/p/11332838.html