CF #133(DIV.2) B
2014-03-12 21:16
495 查看
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=42070#problem/B
Description
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.
Input
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.
Output
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
Sample Input
Input
Output
Input
Output
Input
Output
Description
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.
Input
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.
Output
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
Sample Input
Input
5 4 1 2 2 4 5 3 1 4
Output
1
Input
6 2 1 4 3 4
Output
0
Input
6 6 1 2 2 3 3 1 4 5 5 6 6 4
Output
2
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; int m,n,sum[1005],a[1005]; int find(int x) { if(x==a[x]) return x; return a[x]=find(a[x]); } void isis() { for(int i=0;i<=n;i++) { a[i]=i; sum[i]=1; } } int main() { int x,y; while(~scanf("%d%d",&n,&m)) { isis(); int ans=n; for(int i=0;i<m;i++) { scanf("%d%d",&x,&y); x=find(x); y=find(y); if(x!=y) { a[y]=x; sum[x]+=sum[y]; } else { if(sum[x]%2) ans--; } } if(ans%2) ans--; printf("%d\n",n-ans); } return 0; }
相关文章推荐
- codeforces Round #117 Div.2 182E. Wooden Fence
- codeforces Round #184 Div.2 - B Continued Fractions
- Codeforces Round#194(Div.2) A Candy Bag
- CodeForces#203(Div.2)- A,B,C
- codeforces round#235 (div.2) A,B,C
- Codeforces Round #237 Div.2 A
- codeforces Div.2 B.Suffix Structures
- 独角兽排序问题 Codeforces Round #259(Div.2)
- TopCoder SRM 633 Div.2 500 Jumping
- Topcoder SRM 639 (Div.2)
- coderforces #284(Div.2)
- Codeforces Round#288(Div.2)
- CodeForces #292 div.2 题解
- CodeForces Round #295 Div.2
- #290 (div.2) B. Fox And Two Dots
- CodeForces Round #297 Div.2 E (中途相遇法)
- #308 (div.2) C. Vanya and Scales
- codeforces Round 314 div.2
- BestCoder Round #50 (div.2) HDU 5365 Run(简单几何)
- BestCoder Round #51 (div.2)