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<iostream>
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<m;i++){
        cin>>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<<ans+(n-ans)%2<<endl;
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,878评论 0 23
  • 本文不是一篇告诉你如何在短时间内练就超凡记忆力的指导文章,只是帮助你认识了解记忆,以及如何提高记忆力,让我们在学习...
    Sherry晴阅读 964评论 0 4
  • 提前一个多小时就到了公司,刚到楼下,就下起了阵雨,很大。 To play it safe(为了保险起见), I w...
    柒弟阅读 541评论 0 1
  • 优雅的心态,情绪的管理,好像今天都用上了,下午快4点到了医院,没号了,怎么办呢?只能找医生加号。我知道这个时候去找...
    Rachel_aa22阅读 451评论 0 2
  • 番茄工作法 为什么需要番茄工作法? 很多人都对自己有个错觉,觉得自己一天非常努力。当你真的把自己的工作时间和注意力...
    鸭梨山大哎阅读 359评论 0 3