zoukankan      html  css  js  c++  java
  • 【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;
    }


  • 相关阅读:
    HashMap与Hashtable的区别
    List集合、泛型、装箱拆箱
    关于集合
    统一建模语言
    自定义栈
    学习笔记
    如何优化limit
    mysql五大存储引擎
    [离散数学]2016.12.15周四作业
    [离散数学]2016.12.9周四作业
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7625998.html
Copyright © 2011-2022 走看看