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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容

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