Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
题目传送门
B. Mahmoud and Ehab and the bipartiteness
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
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).
题意:
n个点,n-1条边,其实就是给一棵树,让你求添加最多的边数,使得添加边的树构成的新图是一个二分图,当然不能有重边,有的话,可以添加无穷多条边。
晚上没有打,早上来补题。。。
思路:
我是这样想的,树肯定是二分图啦。
我就先把无根树(无向)变成以结点1为根的有根树(有向),再计算出这个树变成二分图的两个集合的任意一个集合的点数node,可以得到添加最多的边数=node*(n-node)-(n-1);因为二分图有两个集合,一个集里面的结点不能互相连边,只能向另一个集合连边,所以可以连node*(n-node)条边,再减去原来树的边数,就是最多的。
原来的无根树变成有根树,并且有向。
有根树变成二分图,我们就是求点数,没必要存储图,这样其实就很简单了,就是一个深度问题,也就是层度问题,自己可以画一画,因为树没有环。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1e5+10;
vector<int>G[maxn],Tree[maxn];
int pre[maxn];
int cnt_node;
void match_node(int u,int type)
{
int len=Tree[u].size();
if(type%2)
cnt_node++;
for(int i=0;i<len;i++)
{
match_node(Tree[u][i],type+1);
}
}
void node_tree(int u,int fu)
{
int len=G[u].size();
for(int i=0;i<len;i++)
{
if(G[u][i]==fu)
continue;
if(pre[G[u][i]]==0)
{
Tree[u].push_back(G[u][i]);
node_tree(G[u][i],u);
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(pre,0,sizeof(pre));
pre[1]=-1;
node_tree(1,-1);
cnt_node=0;
match_node(1,1);
printf("%I64d\n",(long long)cnt_node*(n-cnt_node)-(n-1));
}
推荐阅读
-
Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
-
Codeforces Round #553 (Div. 2) B. Dima and a Bad XOR(异或+思维)
-
Codeforces Round #246 (Div. 2) ?B. Football Kit_html/css_WEB-ITnose
-
Codeforces Round #261 (Div. 2)B. Pashmak and Flowers(容易)
-
Codeforces Round #266 (Div. 2) B. Wonder Room_html/css_WEB-ITnose
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #649 (Div. 2) C-Ehab and Prefix MEXs
-
Codeforces Round #461 (Div. 2) B. Magic Forest(异或的性质)
-
Codeforces Round #663 (Div. 2) B. Fix You
-
Codeforces Round #658 (Div. 2) B. Sequential Nim