90pts贪心求调,悬一关
查看原帖
90pts贪心求调,悬一关
459188
zrt090604楼主2024/11/22 11:03
#include<bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
int n, p, sz[305], tot, fa[305], mid[305];
vector<int> t[305]; 
vector<pii> g[305];
priority_queue<pii> q;
void pre_solve(int u, int pre) {
	sz[u] = 1;
	fa[u] = pre;
	for(auto v : t[u]) {
		if(v == pre) continue;
		pre_solve(v, u);
		sz[u] += sz[v];
	}
}
bool cmp(pii a, pii b) {
	return a.second > b.second;
}
void dfs(int u, int fa) {
	bool x = false;
	for(auto v : g[u]) {
		if(v.first == fa) continue;
		if(!x) tot -= v.second, x = true;
		else dfs(v.first, u);
	}
	printf("%d", tot), exit(0);
}
int main () {
    scanf("%d%d", &n, &p);
    tot = n;
    for(int i = 1, x, y;i <= p;++i) {
    	scanf("%d%d", &x, &y);
    	t[x].push_back(y), t[y].push_back(x);
	}
	for(int i = 1;i <= n;++i) 
		if(!t[i].size()) --tot;
	pre_solve(1, 0);
	for(int i = 1;i <= n;++i)
		for(auto v : t[i])
			g[i].push_back({sz[v], v});
	for(auto v : g[1]) q.push(v);
	while(!q.empty()) {
		bool f = true;
		int cnt = 0;
		tot -= q.top().first; q.pop();
		while(!q.empty()) {
			mid[++cnt] = q.top().second; q.pop();
		}
		for(int i = 1;i <= cnt;++i)
			for(auto v : g[mid[i]])
				if(v.second != fa[mid[i]]) q.push(v);
	}
	printf("%d", tot);
    return 0;
}
2024/11/22 11:03
加载中...