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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <bits/stdc++.h> #define ll long long
using namespace std; const int N = 5e4 + 10, M = N << 1; int h[N], e[M], ne[M], idx; int dfn[N], low[N], stk[N], cut[N]; int t, cnt, tt; vector<int> dcc[N]; int root;
inline void add(int a, int b) { e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }
inline void tarjan(int u) { dfn[u] = low[u] = ++t; stk[++tt] = u; if (h[u] == -1) { dcc[++cnt].push_back(u); return; } int kid = 0; for (int i = h[u]; ~i; i = ne[i]) { int v = e[i]; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); if (dfn[u] <= low[v]) { if (u != root || ++kid > 1) cut[u] = true; cnt++; int j; do { j = stk[tt--]; dcc[cnt].push_back(j); } while (v != j); dcc[cnt].push_back(u); } } else low[u] = min(low[u], dfn[v]); } }
int main() { int m; int tot = 0; scanf("%d", &m); while (m) { printf("Case %d: ", ++tot); int n = 0; memset(h, -1, sizeof h); memset(dfn, 0, sizeof dfn); memset(cut, 0, sizeof cut); idx = cnt = t = tt = 0; while (m--) { int a, b; scanf("%d%d", &a, &b); n = max(max(a, b), n); add(a, b); add(b, a); } for (int i = 1; i <= n; i++) dcc[i].clear(); for (root = 1; root <= n; root++) if (!dfn[root]) tarjan(root); ll res1 = 0, res2 = 1; for (int i = 1; i <= cnt; i++) { int cnt_cut = 0; for (int j = 0; j < dcc[i].size(); j++) cnt_cut += cut[dcc[i][j]]; if (!cnt_cut) { if (dcc[i].size() > 1) res1 += 2, res2 *= (ll)dcc[i].size() * (dcc[i].size() - 1) / 2; else res1++; } else if (cnt_cut == 1) res1++, res2 *= (ll)dcc[i].size() - 1; } printf("%d %lld\n", res1, res2); scanf("%d", &m); } return 0; }
|