#54# 悬赏关注-_-
查看原帖
#54# 悬赏关注-_-
637788
kimi0705楼主2023/1/15 17:39
#include<bits/stdc++.h>
using namespace std;
int head[5001], cnt;
int n, r;
int dfn[5001], low[5001], bridge[5001], tim;
int scc[5001], du[5001], color_cnt, leaf;
struct {
	int to;
	int next;
} edge[20001];
void add(int x, int y) {
	cnt++;
	edge[cnt] = {y, head[x]};
	head[x] = cnt;
	return;
}
void tarjan(int x, int Edge) {
	dfn[x] = low[x] = ++tim;
	for (int i = head[x]; i; i = edge[i].next) {
		int y = edge[x].to;
		if (!dfn[y]) {
			tarjan(y, i);
			low[x] = min(low[x], low[y]);
			if (dfn[x] < low[y]) bridge[i] = bridge[i^1] = 1;
		} else if (i !=Edge^1) low[x] = min(low[x], dfn[y]);
	}
}
void dfs(int x) {
	scc[x] = color_cnt;
	for (int i = head[x]; i; i = edge[i].next) {
		int v = edge[i].to;
		if (bridge[i] || scc[x]) continue;
		dfs(v);
	}
}
int main() {
	cin >> n >> r;
	for (int i = 1; i <= n; i++) {
		int x, y;
		cin >> x >> y;
		add(x, y);
		add(y, x);
	}
	for (int i = 2; i <= cnt; i += 2) {
		if (!dfn[edge[i].to]) tarjan(edge[i].to, i);
	}
	for (int i = 1; i <= n; i++) {
		if (!scc[i]) {
			color_cnt++;
			dfs(i);
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = head[i]; j; j = edge[j].next) {
			if (scc[i] != scc[edge[j].to]) {
				du[edge[j].to]++;
			}
		}
	}
	for (int i = 1; i <= color_cnt; i++) if (du[i]==1) leaf++;
	cout << (leaf + 1)/2;
	return 0;
}
2023/1/15 17:39
加载中...