codeforces 862B
来源:互联网 发布:淘宝商城铂金会员 编辑:程序博客网 时间:2024/06/02 01:57
Mahmoud and Ehab and the bipartiteness
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 .
Input
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
Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
Example
Input
3
1 2
1 3
Output
0
Input
5
1 2
2 3
3 4
4 5
Output
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).
题意:给你一个图是个二分图,问最多可以加几条边还依然是个二分图。
思路:其实不太理解,看别人的博客写的说是染色,把点集分为两个集合,边的数是点的个数相乘减去给出的边。
#include<stdio.h>#include<string>#include<iostream>#include<algorithm>#include<string.h>using namespace std;struct node{ int u; struct node*next;}*h[100010];int vis[100010];void l(int v,int u){ struct node*p=(struct node*)malloc(sizeof(struct node)); p->u=u; p->next=h[v]; h[v]=p;}void dfs(int x,int flag){ // printf("%d ",flag); struct node*p; for( p=h[x]; p!=NULL; p=p->next) { int u=p->u; if(vis[u]==-1) { vis[u]=flag; dfs(u,!flag); } }}int main(){ int n; while(~scanf("%d",&n)) { memset(vis,-1,sizeof(vis)); for(int i=0;i<100010;i++) { h[i]=NULL; } for(int i=1;i<n;i++) { int a,b; scanf("%d%d",&a,&b); l(a,b); l(b,a); } dfs(1,0); // printf("flag"); long long ans1=0,ans2=0; for(int i=1;i<=n;i++) { if(vis[i]==0) { ans1++; } if(vis[i]==1) ans2++; } printf("%lld\n",(ans1*ans2)-n+1); }}
- codeforces 862b
- codeforces 862B
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
- CodeForces 626B CodeForces 626B【暴力】
- CodeForces 841B (B) 博弈
- codeforces 134B
- codeforces#98 b
- codeforces 105 div2 B
- Codeforces 166B - Polygons
- codeforces B. Coins
- codeforces----193B Xor
- codeforces----208B Solitaire
- Codeforces 1B - Spreadsheet
- codeforces 214B Hometask
- 制作任意大小的根文件系统镜像ramdisk.image.gz
- nginx源码阅读(四).创建子进程(worker和cache)
- ‘’HELLOWORD''图形用户界面
- 异常
- 翻转链表 II
- codeforces 862B
- 第一章-概述
- java 常用的类库-Object
- HTML基础
- JUnit(三)高级之Matchers and assertThat_MD
- Python高级编程——3.深浅拷贝
- Noip 2011 计算系数
- Retrofit上传文件
- MySQL5.7.20安装过程 和常用指令