点连通分量 - 缩点
作者:互联网
\(\href{https://www.acwing.com/solution/content/24931/}{点连通分量的缩点}\)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 1010, M = 1010;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int stk[N], top;
int dcc_cnt;
vector<int> dcc[N];
bool cut[N];
int root;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++timestamp;
stk[++top] = u;
if (u == root && h[u] == -1) {
dcc_cnt++;
dcc[dcc_cnt].pb(u);
return;
}
int cnt = 0;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!dfn[j]) {
tarjan(j);
low[u] = min(low[u], low[j]);
if (dfn[u] <= low[j]) {
cnt++;
if (u != root || cnt > 1) cut[u] = 1;
++dcc_cnt;
int y;
do {
y = stk[top--];
dcc[dcc_cnt].pb(y);
} while (y != j);
dcc[dcc_cnt].pb(u);
}
}
else low[u] = min(low[u], dfn[j]);
}
}
int main() {
IO;
int T = 1;
while (cin >> m, m) {
for (int i = 1; i <= dcc_cnt; ++i) dcc[i].clear();
idx = n = timestamp = top = dcc_cnt = 0;
memset(h, -1, sizeof h);
memset(dfn, 0, sizeof dfn);
memset(cut, 0, sizeof cut);
while (m--) {
int a, b;
cin >> a >> b;
n = max(a, n), n = max(n, b);
add(a, b), add(b, a);
}
for (root = 1; root <= n; ++root)
if (!dfn[root])
tarjan(root);
int res = 0;
ull num = 1;
for (int i = 1; i <= dcc_cnt; ++i) {
int cnt = 0;
for (int j = 0; j < dcc[i].size(); ++j)
if (cut[dcc[i][j]]) cnt++;
if (cnt == 0) {
if (dcc[i].size() > 1) res += 2, num *= dcc[i].size() * (dcc[i].size() - 1) / 2;
else res++;
} else if (cnt == 1) res++, num *= dcc[i].size() - 1;
}
cout << "Case " << T++ << ": " << res << " " << num << '\n';
}
return 0;
}
标签:缩点,连通,int,cnt,dcc,++,low,define,分量 来源: https://www.cnblogs.com/phr2000/p/14427144.html