codeforces 862B B. Mahmoud and Ehab and the bipartiteness

http://codeforces.com/problemset/problem/862/B

题意:

给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图。

思路:

想得比较复杂了。。。。其实既然已经给出了二分图并且有n-1条边,那么我们就一定可以用染色法对每一个点进行染色,从而将点划分为两个集合,然后从两个集合中分别任意选择一个点连边就行了。

一开始考虑二分图的基本属性是不存在奇数条边的环。。。并不需要这样,因为两个集合是分开的,从两个中分别任意选择一个连边,是肯定不会造成同一集合中的两点有边相连的。

代码:

 1 #include <stdio.h>
 2 #include <vector>
 3 using namespace std;
 4
 5 vector<int> v[100005];
 6 bool vis[100005];
 7
 8 int color[100005];
 9
10 void dfs(int s)
11 {
12     vis[s] = 1;
13
14     if (color[s] == 0) color[s] = 1;
15
16     for (int i = 0;i < v[s].size();i++)
17     {
18         int to = v[s][i];
19
20         if (!vis[to])
21         {
22             if (color[s] == 1) color[to] = 2;
23             else color[to] = 1;
24
25             vis[to] = 1;
26             dfs(to);
27         }
28     }
29 }
30
31 int main()
32 {
33     int n;
34
35     scanf("%d",&n);
36
37     for (int i = 0;i < n - 1;i++)
38     {
39         int x,y;
40
41         scanf("%d%d",&x,&y);
42
43         v[x].push_back(y);
44         v[y].push_back(x);
45     }
46
47     dfs(1);
48
49     long long cnt1 = 0,cnt2 = 0;
50
51     for (int i = 1;i <= n;i++)
52     {
53         if (color[i] == 1) cnt1++;
54         else cnt2++;
55     }
56
57     printf("%I64d\n",cnt1 * cnt2 - (n - 1));
58
59     return 0;
60 }

PS:记得要用long long,要不会wa。

时间: 2024-11-05 06:03:06

codeforces 862B B. Mahmoud and Ehab and the bipartiteness的相关文章

Codeforces 862B - Mahmoud and Ehab and the bipartiteness

862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+5; bool color[N]; vector<i

E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees. A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in su

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

题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集合间总共可以连cnt1*cnt2条边,给定的是一个树,因此已经连了n-1条边,所以最多能连cnt1*cnt2-(n-1)条边. 3.注意输出. #include<cstdio> #include<cstring> #include<cstdlib> #include<

Codeforces 959 D Mahmoud and Ehab and another array construction task

Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of the same length such that: b is lexicographically greater than or equal to a. bi?≥?2. b is pairwise coprime: for every 1?≤?i?<?j?≤?n, bi and bj are c

Codeforces 862C - Mahmoud and Ehab and the xor

862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了,0异或x等于x.所以只要把剩下的异或起来变成x就可以了.如果剩下来有3个,那么,这3个数可以是x^i^j,i,j. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back

CF 862C Mahmoud and Ehab and the xor(异或)

题目链接:http://codeforces.com/problemset/problem/862/C 题目: Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a n

Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

题目:http://codeforces.com/problemset/problem/862/D 题意:交互题,询问15次以内Hamming distance,输出一个二进制串里任意一个0或1的位置 题解:极简单的二分,从最后一位先判断一个,然后二分 根据上次和本次的距离差是否等于二分长度判断在左端还是右端有需要寻找的值寻找另一个. 1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,10

D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/D 询问题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <se

E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #includ