CUC-SUMMER-6-I

I - Forming Teams
CodeForces - 216B

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.

Example
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


题意:在运动场上,要将运动员分成两队,每位运动员最多有两个死对头,分队时要保证没有死对头分到同一个队里,最少舍去几个人能成功分好队

解法:将对立关系以图的形式画出来,字图只可能出现链或环两种关系,如果是链状则不需要舍弃人,如果是奇数环状,需要舍弃一个人,偶数环不需要舍弃,可以通过看每个结点的度数是否小于2判断是链还是环。于是问题转化成有多少个奇数环,没有一个奇数环减去一个人,最后如果剩下的人仍然是奇数还要再去掉一人。

代码:

#include
using namespace std;
bool e[105][105];
bool viz[105];
int degree[105],sum,m,n;
bool flag;
void dfs(int i,int pre)
{
    viz[i]=1;
    if(degree[i]==1||degree[i]==0)
        flag=1;
    sum+=degree[i];
    for(int j=1;j<=n;j++)
        if(!viz[j]&&e[i][j]&&j!=pre)
            dfs(j,i);
}
int main()
{
    int a,b,ans=0;
    cin>>n>>m;
    for(int i=0;i>a>>b;
        e[a][b]=1;
        e[b][a]=1;
        degree[a]++;
        degree[b]++;
    }
    for(int i=1;i<=n;i++){
        if(!viz[i]){
            flag=0;
            sum=0;
            dfs(i,-1);
            if(!flag)
                if(sum/2%2==1)
                    ans++;
        }
    }
    cout<

你可能感兴趣的:(CUC-SUMMER-6-I)