Mahmoud and Ehab and the bipartiteness CodeForces
来源:互联网 发布:淘宝下架宝贝有影响吗 编辑:程序博客网 时间:2024/06/05 08:59
传送门
题目大意:给定一个整数n(1 <= n <= 10^5) , 然后给n-1行, 表示u和v有一条边链接, 所有的数分成两部分,在同一侧的数字不能链接,问除了题目给定的链接外还有多少链接。
解题思路:主要是求解两侧分别有几个数字,然后结果就是 a × b - n +1; a和b分别为两边的数量。分类用并查集很简单,可以参考经典并查集题目:食物连。分成两类后遍历数一下看看几个即可。
AC代码:
#include <iostream>#include <cstdio>using namespace std;const int max_n = 100050;int par[max_n+max_n];int Rank[max_n+max_n];void init_par(){for(int i=0; i<max_n*2; i++){par[i] = i;Rank[i] = 0;}}int find_par(int n){if(par[n] == n) return n;return par[n] = find_par(par[n]);}void unit_par(int a, int b){a = find_par(a);b = find_par(b);if(a == b) return;if(Rank[a] < Rank[b]) par[a] = b;else{par[b] = a;if(Rank[a] == Rank[b]) Rank[a]++;}}bool same(int a, int b){return find_par(a) == find_par(b);}int main(){int n, u, v;scanf("%d", &n);if(n == 1){printf("0\n");return 0;}init_par();for(int i=1; i<n; i++){scanf("%d%d", &u, &v);unit_par(u, v+max_n);unit_par(v, u+max_n);}int a = 0, b = 0;for(int i=1; i<=n; i++){if(same(u, i)) a++;else if(same(u, i+max_n)) b++;}printf("%lld\n", (long long) a*b - n+1);return 0;}
阅读全文
0 0
- Mahmoud and Ehab and the bipartiteness CodeForces
- codeforces 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】
- 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(补)
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图,染色法)
- codeforces 862B (二分图 dfs )之 Mahmoud and Ehab and the bipartiteness
- Codeforces 862 B Mahmoud and Ehab and the bipartiteness(二分图染色)
- Mahmoud and Ehab and the xor CodeForces
- Mahmoud and Ehab and the xor CodeForces
- Codeforces --- Mahmoud and Ehab and the MEX
- 862B Mahmoud and Ehab and the bipartiteness (黑白染色)
- Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图染色)
- Codeforces Round #435 A. Mahmoud and Ehab and the MEX
- Codeforces Round #435 A. Mahmoud and Ehab and the MEX
- mysql 数据库优化
- mybatis中mapper配置方式进行查询(三)
- ORA-39700: database must be opened with UPGRADE option
- 关于springmvc中使用注解的简单用法-@RequestMapping篇
- 【web】盒子模型,宽高的计算方式
- Mahmoud and Ehab and the bipartiteness CodeForces
- 聊聊js中的提升
- 有序向量的查找
- git笔记---拉仇恨啊
- Codeforces 591D Chip 'n Dale Rescue Rangers
- AllegroGraph介绍
- JVM方法区理解
- LeetCode #26
- Android.mk语法