Codeforce 862B Mahmoud and Ehab and the bipartiteness (二分图染色 + 链式前向星)
来源:互联网 发布:数据存储安全 编辑:程序博客网 时间:2024/06/15 15:37
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 such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.
Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?
A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
The first line of input contains an integer n — the number of nodes in the tree (1 ≤ n ≤ 105).
The next n - 1 lines contain integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the description of the edges of the tree.
It's guaranteed that the given graph is a tree.
Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
31 21 3
0
51 22 33 44 5
2
e题意:
给你 n 代表n个节点。
给你n-1条边
问你再最多再添加多少边使得二分图的性质成立;
二分图简介
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 200005struct data{ int to,next;} Line[N];int cnt = 0;int Color[N];int head[N];void add(int a,int b){ Line[cnt].to=b; Line[cnt].next=head[a]; head[a]=cnt++;}void Add_Line(int a,int b){ add(a,b); add(b,a);}int dfs(int x,int color){ Color[x]=color; for(int i=head[x]; i!=-1; i=Line[i].next) { if(Color[Line[i].to]==-1) dfs(Line[i].to,!color); }}int main(){ int n; while(~scanf("%d",&n)) { memset(head,-1,sizeof(head)); memset(Color,-1,sizeof(Color)); for(int i=1; i<n; i++) { int x,y; scanf("%d%d",&x,&y); Add_Line(x,y); } dfs(1,0); long long res=0; for(int i=1; i<=n; i++) { res+=(Color[i]); } res = res*(n-res); printf("%lld\n",res-(n-1)); }}
- Codeforce 862B Mahmoud and Ehab and the bipartiteness (二分图染色 + 链式前向星)
- Codeforces 862 B Mahmoud and Ehab and the bipartiteness(二分图染色)
- Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图染色)
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图,染色法)
- codeforces 862B (二分图 dfs )之 Mahmoud and Ehab and the bipartiteness
- 862B Mahmoud and Ehab and the bipartiteness (黑白染色)
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
- codeforces 862B. Mahmoud and Ehab and the bipartiteness
- CF 863.B Mahmoud and Ehab and the bipartiteness【DFS】
- Mahmoud and Ehab and the bipartiteness CodeForces
- codeforces Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(补)
- Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- codeforce 862C Mahmoud and Ehab and the xor(构造)
- 862A Mahmoud and Ehab and the MEX (水)
- codeforces 862E Mahmoud and Ehab and the function 二分+思维
- CF 862C Mahmoud and Ehab and the xor 构造
- 深入了解PHP【生命周期】
- Item 11:在赋值操作符中处理自我赋值【effective C++读书笔记】
- VisualSVN Server v3.7
- Android 退出登录功能
- 二进制数中返回1的个数的几种方法
- Codeforce 862B Mahmoud and Ehab and the bipartiteness (二分图染色 + 链式前向星)
- 大整数的乘法
- Codeforces 862C
- 基于Spring session 和 redis 的session共享问题
- 自己动手简单实现CountDownLatch
- 如何在Android7.0系统下通过Intent安装apk
- windows下tomcat日志输出至文件
- Microservices From Design to Deployment(中文完整版)
- linux下编译安装apr