【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness

【链接】h在这里写链接


【题意】


让你在一棵树上,加入尽可能多的边。
使得这棵树依然是一张二分图。

【题解】


让每个节点的度数,都变成二分图的对方集合中的点的个数就好。

【错的次数】


0

【反思】


在这了写反思

【代码】

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;

vector <int> G[N + 10];
int n,color[N+10],cnt[2];

void dfs(int x, int tmp) {
	color[x] = tmp;
	cnt[tmp]++;
	for (int y : G[x]) {
		if (color[y] == -1)
			dfs(y, 1 - tmp);
	}
}

int main() {
	//freopen("F:\rush.txt", "r", stdin);
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n - 1; i++) {
		int x, y;
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	memset(color, 255, sizeof color);
	dfs(1, 0);
	long long ans = 0;
	for (int i = 1;i <= n;i++)
		if (color[i] == 1) {
			ans += cnt[0] - (int)G[i].size();
		}
	cout << ans << endl;
	return 0;
}


原文地址:https://www.cnblogs.com/AWCXV/p/7625998.html