码迷,mamicode.com
首页 > 其他好文 > 详细

lightoj-1009 - Back to Underworld(dfs+仿二分图染色)

时间:2016-06-04 23:28:19      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

1009 - Back to Underworld
PDF (English) Statistics Forum
Time Limit: 4 second(s) Memory Limit: 32 MB
The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none knows who will win. The humans want to know who will survive finally. But humans are afraid of going to the battlefield.

So, they made a plan. They collected the information from the newspapers of Vampires and Lykans. They found the information about all the dual fights. Dual fight means a fight between a Lykan and a Vampire. They know the name of the dual fighters, but don‘t know which one of them is a Vampire or a Lykan.

So, the humans listed all the rivals. They want to find the maximum possible number of Vampires or Lykans.

Input
Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 105), denoting the number of dual fights. Each of the next n lines will contain two different integers u v (1 ≤ u, v ≤ 20000) denoting there was a fight between u and v. No rival will be reported more than once.

Output
For each case, print the case number and the maximum possible members of any race.

Sample Input
Output for Sample Input
2
2
1 2
2 3
3
1 2
2 3
4 2
Case 1: 2
Case 2: 3

题目大意:给出N组对战的军队编号,求属于同个国家军队数的最大数量

 

解题思路: 每个连通的二分图中,只要有一个点确定是哪个国家的,那么整个连通图的节点所属的国家也就确定了。然后把每个连通图的计算出来较多的军队数相加起来。就搞定了。

技术分享
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

const int N = 20010;
vector<int> vv[N];
bool uu[N],vis[N];

int T,n,u,v,maxn,minn,sum,sum1;

void init(){
    for(int i=0;i<N;i++) vv[i].clear();
    memset(uu,false,sizeof(uu));
    memset(vis,false,sizeof(vis));
    maxn = 0;
    minn = N;
}

void dfs(int u,bool flag){// flag 用来实现类似染色的功能,因为每次的染色不需要多次用到,所以直接传参过去就行了。 
    
    sum++;if(flag==1) sum1++;
    vis[u] = true;
    for(int i=0;i<vv[u].size();i++){
        
        if(vis[vv[u][i]]) continue;
        
        dfs(vv[u][i],!flag);
        
    }
    
    return ;
}

int main(){
    
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            
            scanf("%d%d",&u,&v);
            uu[u] = uu[v] = true;
            vv[u].push_back(v);
            vv[v].push_back(u);
            maxn = max(maxn,max(u,v));
            minn = min(minn,min(u,v));
        }
        int ans = 0;
        for(int i=minn;i<=maxn;i++){
            
            if(uu[i]==false||vis[i]==true) continue; // 不存在或者访问过的点 直接continue; 
            sum = sum1 = 0;
            dfs(i,0);
            ans += max(sum1,sum-sum1);
            
        }
        printf("Case %d: %d\n",t,ans);
    }
    
    return 0;
}
View Code

 

lightoj-1009 - Back to Underworld(dfs+仿二分图染色)

标签:

原文地址:http://www.cnblogs.com/yuanshixingdan/p/5559778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!