您的位置:首页 > 其它

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

2017-10-04 18:44 411 查看

【链接】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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐