CF216B: Forming Teams(并查集)
来源:互联网 发布:mac ppt动画制作教程 编辑:程序博客网 时间:2024/06/12 20:49
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people.
We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A.
The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench.
Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly.
Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies.
You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
5 41 22 45 31 4
1
6 21 43 4
0
6 61 22 33 14 55 66 4
2
# include <stdio.h>int pre[101], num[101];void init(int n){ for(int i=0; i<=n; ++i) { pre[i] = i; num[i] = 1; }}int find(int x){ if(x != pre[x]) pre[x] = find(pre[x]); return pre[x];}int main(){ int n, m, x, y, ans; while(~scanf("%d%d",&n,&m)) { ans = 0; init(n); while(m--) { scanf("%d%d",&x,&y); int px = find(x); int py = find(y); if(px != py) { pre[px] = py; num[py] += num[px]; } else ans += num[px]&1; } ans += (n-ans)&1; printf("%d\n",ans); } return 0;}
- CF216B: Forming Teams(并查集)
- GUETOJ - 1101 Forming Teams(并查集2)
- 并查集:Forming Teams(CF#133)
- Codeforces Round #133 (Div. 2) B. Forming Teams(并查集)
- CodeForces 216B Forming Teams(并查集判断奇数环)
- scau_10317 Fans of Footbal Teams(并查集)
- UVa 10911 Forming Quiz Teams(dfs)
- 10911 - Forming Quiz Teams
- CF Forming Teams
- 10911 - Forming Quiz Teams
- UVA10911- Forming Quiz Teams
- uva10911 Forming Quiz Teams
- uva_10911 - Forming Quiz Teams ( 状态压缩DP )
- uva10911 - Forming Quiz Teams(记忆化搜索)
- UVA 10911 Forming Quiz Teams(状压DP)
- uva 10911 Forming Quiz Teams
- UVA 10911 Forming Quiz Teams
- uva 10911 Forming Quiz Teams
- 泛型的约束理解
- JS跨域调用之document.domain--相同基础域名页面之间的调用
- HDU 1290 切蛋糕 (献给杭电五十周年校庆的礼物) *
- 应用数据静态化架构高性能单页Web应用
- JavaSe总结-12- API常用对象(2)
- CF216B: Forming Teams(并查集)
- 用ProgressBar做血条
- Java常见问题分析(内存溢出、内存泄露、线程阻塞等)
- 杨辉三角
- kali linu学习笔记——更换apt rolling源
- make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
- 函数参数Variable-length Arguments
- 应知道的数据库连接池DBCP配置及JDBC超时设置
- 转载:王垠 的《完全用Linux工作》