Problem - 216B - Codeforces Forming Teams

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;
}










你可能感兴趣的:(Problem - 216B - Codeforces Forming Teams)