题目大意:求无向图中,有多少个割点
解题思路:模版题了
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;
#define max(a,b)((a)>(b)?(a):(b))
#define min(a,b)((a)<(b)?(a):(b))
const int MAXNODE = 10005;
const int MAXEDGE = 100010;
const int INF = 0x3f3f3f3f;
struct Edge{
int v, id, next;
bool bridge;
Edge() {}
Edge(int v, int id, int next): v(v), id(id), next(next), bridge(false){}
}E[MAXEDGE * 2], cut[MAXEDGE];
struct Node {
int u, v;
Node() {}
Node(int u, int v): u(u), v(v) {}
};
//bcc表示的是一个bcc里面的点
vector<int> bcc[MAXNODE];
stack<Node> Stack;
//pre纪录的是时间戳,lowlink纪录的是该点及其该子孙节点所能返回的最早时间戳是多少,bccno纪录的是该点当前是属于哪个bcc的
int head[MAXNODE], pre[MAXNODE], lowlink[MAXNODE], bccno[MAXNODE];
int n, m, tot, bcc_cnt, dfs_clock, cut_cnt;
bool iscut[MAXNODE];
void AddEdge(int u, int v, int id) {
E[tot] = Edge(v, id, head[u]);
head[u] = tot++;
u = u ^ v; v = u ^ v; u = u ^ v;
E[tot] = Edge(v, id, head[u]);
head[u] = tot++;
}
void init() {
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
tot = 0;
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
AddEdge(u, v, 0);
}
}
//点双连通
void dfs(int u, int fa) {
pre[u] = lowlink[u] = ++dfs_clock;
int child = 0;//纪录当前节点有多少个子节点
for (int i = head[u]; ~i; i = E[i].next) {
int v = E[i].v;
if (!pre[v]) {
Stack.push(Node(u, v));
child++;
dfs(v, u);
lowlink[u] = min(lowlink[u], lowlink[v]);//更新
//子节点最多返回到该点
if (lowlink[v] >= pre[u]) {
//该边为桥
if (lowlink[v] > pre[u]) {
E[i].bridge = E[i ^ 1].bridge = true;
cut[cut_cnt++] = E[i];
}
iscut[u] = true;
bcc_cnt++; bcc[bcc_cnt].clear();
while (1) {
Node x = Stack.top(); Stack.pop();
if (bccno[x.u] != bcc_cnt) {
bcc[bcc_cnt].push_back(x.u);
bccno[x.u] = bcc_cnt;
}
if (bccno[x.v] != bcc_cnt) {
bcc[bcc_cnt].push_back(x.v);
bccno[x.v] = bcc_cnt;
}
if (x.u == u && x.v == v) break;
}
}
}
else if (v != fa && pre[v] < pre[u]) {//反向边
Stack.push(Node(u, v));
lowlink[u] = min(lowlink[u], pre[v]);
}
}
//u是根结点,且只有一个孩子,那就不是割点了
if (fa < 0 && child == 1) iscut[u] = 0;
}
/*
//边双连通
int belong[MAXNODE];
int Stack[MAXNODE];
void find_bcc(int u, int fa) {
pre[u] = lowlink[u] = ++dfs_clock;
Stack[++top] = u;
for (int i = head[u]; ~i; i = E[i].next) {
int v = E[i].v;
if (!pre[v]) {
find_bcc(v, u);
lowlink[u] = min(lowlink[u], lowlink[v]);
if (lowlink[v] > pre[u]) {
E[i].bridge = E[i ^ 1].bridge = true;
cut[cut_cnt++] = E[i];
bcc_cnt++;
while (1) {
int x = Stack[top--];
belong[x] = bcc_cnt;
if (x == v) break;
}
}
}
else if (pre[v] < pre[u] && v != fa) lowlink[u] = min(lowlink[u], pre[v]);
}
}
*/
void find_bcc() {
memset(pre, 0, sizeof(pre));
memset(iscut, 0, sizeof(iscut));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = cut_cnt = 0;
for (int i = 1; i <= n; i++)
if (!pre[i]) dfs(i, -1);
}
int cas = 1;
void solve() {
find_bcc();
int ans = 0;
for (int i = 1; i <= n; i++)
if (iscut[i]) ans++;
printf("Case %d: %d\n", cas++, ans);
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
init();
solve();
}
return 0;
}