CodeForces
来源:互联网 发布:python获取当前函数名 编辑:程序博客网 时间:2024/06/03 09:50
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 into2 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
把所有点染成两个颜色,个数分别是s1,s2所以总连线共有s1*s2条,需要再减去已连的。其中数太大需要用vector存
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#include<vector>#include<cmath>#include<algorithm>using namespace std;int n;int vis[100005];vector<int>line[100005];void isTwo(){ memset(vis,0,sizeof(vis)); queue<int >q; vis[1]=1; q.push(1); while(!q.empty()) { int p=q.front(); q.pop(); for(int i=0;i<line[p].size();i++) { if(vis[line[p][i]]==0)//未染色 {//开始染色 if(vis[p]==1) vis[line[p][i]]=2; else vis[line[p][i]]=1; q.push(line[p][i]); } } } return;}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) line[i].clear(); for(int i=0;i<n-1;i++) { int x,y; scanf("%d%d",&x,&y); line[x].push_back(y);//把与x相连的点存进line[x]里,line[x][i]表示与X相连的第i个点 line[y].push_back(x); } isTwo(); long long s1=0,s2=0; for(int i=1;i<=n;i++) { if(vis[i]==1) s1++; if(vis[i]==2) s2++; } printf("%lld\n",s1*s2-n+1); }}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- equals与==区别
- 每天一道算法题——树的子结构
- 我的Kotlin For Android之路基础篇(二)
- 测试应该知道的Nginx知识
- 练习题 一
- CodeForces
- bzoj1875 [SDOI2009]HH去散步 偏移+化边+矩乘
- How Long Does It Take
- 京东的热搜界面
- HDU-1695 GCD(容斥定理+欧拉函数)
- 【Scikit-Learn 中文文档】流形学习
- 12.Python入门之集合
- python的reportlab安装及调用的问题
- 统计英文文章中出现最多的词汇,并用turtle函数绘制柱状图