码迷,mamicode.com
首页 > 其他好文 > 详细

A - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (二分图)(黑白染色)

时间:2020-04-19 00:30:02      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:title   lld   nod   add   深度   nta   can   red   dfs搜索   

技术图片

 

 

Examples
input
Copy
3
1 2
1 3
output
Copy
0
input
Copy
5
1 2
2 3
3 4
4 5
output
Copy
2
Note

Tree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)

Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graph

In the first test case the only edge that can be added in such a way, that graph won‘t contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.

In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).

题意:给出二分图的一部分边,问最多可以再添加多少条边,使添加后的图也是一个二分图。

思路:dfs搜索一遍,把深度为偶数的标记为一个值t1,深度为奇数的标记为另一个值t2。结果就是t1*t2-n+1。

代码:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%lld",&a)
#define pr(a) printf("%lld\n",a);
#define SC(n,m) scanf("%lld%lld",&n,&m)
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;//1e10
const int mod = 1e9 + 7;
const int Maxn = 2e5 + 9;
const int M = Maxn * 20;
const double pi = acos(-1.0);
const double eps = 1e-8;

vector<int>a[Maxn];
int vis[Maxn];
ll t1 = 0, t2 = 0, n;

void dfs(int deep, int t) {
    if (deep & 1)t1++;
    else t2++;
    for (int i = 0; i < a[t].size(); i++) {
        if (!vis[a[t][i]]) {
            vis[a[t][i]] = 1;
            dfs(deep + 1, a[t][i]);
            vis[a[t][i]] = 0;
        }
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    vis[1] = 1;
    dfs(0, 1);
    printf("%lld\n", t1* t2 - (n - 1));
    return 0;
}


黑白染色:

#include <iostream>
#include <vector>
using namespace std;
vector<int> v[100005];
long long cnt[2];
void dfs(int node,int pnode,int color)
{
    cnt[color]++;
    for (int i=0;i<v[node].size();i++)
    {
        if (v[node][i]!=pnode)
        dfs(v[node][i],node,!color);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for (int i=1;i<n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1,0,0);
    printf("%I64d",cnt[0]*cnt[1]-n+1);
}

 

 

 

 

A - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (二分图)(黑白染色)

标签:title   lld   nod   add   深度   nta   can   red   dfs搜索   

原文地址:https://www.cnblogs.com/AlexLINS/p/12729102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!