Codeforces
来源:互联网 发布:ios 蜂窝移动网络搜索 编辑:程序博客网 时间:2024/06/01 13:31
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
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).
题意:给你一颗树,问你最多可以添多少条边使得它形成的图还是Bipartite graph。
解题思路:知道什么是Bipartite graph图,这题就不难了。就是这个图的点能否被切分成两个集合,同一个集合中的点,不能有边相连。所以最后所有点都会被划分到两个集合内。所以我们就可以模拟这个过程,把点分到两个集合里。设为集合大小为a,b。那么很明显,这两个集合之间最多可以连a*b条边。所以答案就是a*b-(n-1)。那么我们可以通过深度优先搜索,把这些点分到两个集合里。
#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>#define MAXV 400005using namespace std;typedef long long int ll;struct edge{ int v1,v2,next;}e[MAXV];int n,m,edge_num;int head[MAXV];bool vis[MAXV];void insert_edge(int v1,int v2){ e[edge_num].v1=v1; e[edge_num].v2=v2; e[edge_num].next=head[v1]; head[v1]=edge_num++;}set<int> lefts,rights;//lr记录现在放的是哪个集合。void dfs(int v,int fa,bool lr){ lr=1-lr; if(vis[v]) return; vis[v]=1; for(int i=head[v];i!=-1;i=e[i].next){ int u=e[i].v2; if(u!=fa){ if(lr){ lefts.insert(u); } else { rights.insert(u); } dfs(u,v,lr); } }}int main(){ cin>>n; int a,b; memset(head,-1,sizeof(head)); for(int i=0;i<n-1;i++) { scanf("%d%d",&a,&b); insert_edge(a,b); insert_edge(b,a); } for(int i=1;i<=n;i++){ if(i==1){ lefts.insert(i); } dfs(i,0,1); } ll aaa=lefts.size();//一定要用ll ll bbb=rights.size(); cout<<aaa*bbb-n+1<<endl; return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Protocol Buffer使用实例
- 如何编写测试计划
- Json转换利器Gson之-实际开发中的特殊需求处理(手动解析-TypeToken处理)
- spring的@Transactional注解详细用法
- HGDB PG表中添加删除列
- Codeforces
- Lock使用----ReentrantRedWriteLock
- 例1.2 使用函数重载
- 关于多维数组的运算问题
- 使用react.js中遇到的问题
- redis+sentinel集群部署
- 微信支付body中文签名错误编码格式
- 9. 给 apache ,nginx 设置变量
- Spring依赖注入那些事儿(转)