Problem - 216B - Codeforces Forming Teams

原创 2013年12月03日 15:18:19

http://codeforces.com/problemset/problem/216/B  

将敌人关系连边建图,则答案等于图中奇数环的个数(当时做题时以为只要找三角形的环就够了,唉,2了啊)。

为了保持双方人数一样,所以当(n-ans)%2==1时,还需要随便弄个人下来。


#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1005
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int vis[105];
vector<int> maps[105];
int t;
int root;
bool dfs(int k,int fa=-1)
{
    vis[k]=1;
    if(fa!=-1&&root==k) return true;
    t++;
    for(int i=0;i<maps[k].size();i++)
    {
        if(maps[k][i]!=fa)
        {
            if(dfs(maps[k][i],k)) return true;
        }
    }
    return false;
}
int main()
{
    int n,m,a,b;
    while(cin>>n>>m)
    {
        while(m--)
        {
            cin>>a>>b;
            maps[a].push_back(b);
            maps[b].push_back(a);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                t=0;
                root=i;
                if(dfs(i)&&(t&1)) ans++;
            }
        }
        if((n-ans)&1) ans++;
        cout<<ans<<endl;
    }
    return 0;
}










版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【打CF,学算法——三星级】CodeForces 216B Forming Teams (图论)

【CF简介】 提交链接:CF 216B 题面: B. Forming Teams time limit per test 2 seconds memor...

Educational Codeforces Round 9 -- B - Alice, Bob, Two Teams

B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megabytes i...

Educational Codeforces Round 9 B. Alice, Bob, Two Teams【水题】

B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megabytes i...

Educational Codeforces Round 9-B. Alice, Bob, Two Teams(前缀和)

B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megab...

Codeforces 632B:Alice, Bob, Two Teams(英文题。。。)

B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megab...

CodeForces 632B- Alice, Bob, Two Teams

Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There a...

【codeforces 632B Alice, Bob, Two Teams】

Alice, Bob, Two TeamsDescription Alice and Bob are playing a game. The game involves splitting up g...

B. Random Teams(Codeforces Round 273)

B. Random Teams time limit per test 1 second memory limit per test 256 megabytes in...
  • caduca
  • caduca
  • 2014-10-17 11:12
  • 1124

10911 - Forming Quiz Teams

/* 这题的状态用普通方法不好表示,可以用状态压缩后的一维数组来表示, d[i]表示当前配对状态为i时的最短距离和。其中i用2进制表示后第k位如果是0则代表 第k位同学已经组队,1代表第k位同学还未组...

UVa 10911 - Forming Quiz Teams 状态压缩dp

4th IIUC Inter-University Programming Contest, 2005 G Forming Quiz Teams In...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)