1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <cstdio>
using namespace std;
const int N = 1e5 + 66;
inline void dfs(int x) { int i, y; vis[x] = 1; for (i = head[x]; i; i = e[i].nex) { y = e[i].ver; if (y == t) continue; dfs(y); } return; }
int low[N], dfn[N], in[N], sta[N], tp, tot; priority_queue<int>q;
inline void tarjan(int x) { int i, y; dfn[x] = low[x] = ++ tot, in[x] = 1, sta[++ tp] = x; for (i = head[x]; i; i = e[i].nex) { y = e[i].ver; if (! dfn[y]) { tarjan(y); low[x] = min(low[x], low[y]); } else if (in[y]) low[x] = min(low[x], dfn[y]); } if (low[x] == dfn[x]) { do { y = sta[tp --], in[y] = 0; q.push(d[y]); } while (y != x); if(q.size() > 1) res = max(res, q.top()); while(q.size()) q.pop(); } return; }
signed main() { int i, pd, x, y; n = read();
for (i = 1; i <= n; ++ i) { x = read(), y = read(); add_edge(x, y, i), d[y] = i; } for (i = 1; i <= n; ++ i) { if (deg[i] > 1) pd = 1, t = i; if (! deg[i]) st = i; }
if (pd == 1) { int number = 0; for (i = 1; i <= n; ++ i) if (e[i].ver == t) a[++ number] = i; dfs(st); if (! vis[e[a[1]].frm]) res = a[1]; else { if (! vis[e[a[2]].frm]) res = a[2]; else res = max(a[1], a[2]); } } else for (i = 1; i <= n; ++ i) if (! dfn[i]) tarjan(i); put(res); return 0; }
|