zoukankan      html  css  js  c++  java
  • lightoj-1009

    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
    ",t,ans);
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    mysql索引需要了解的几个注意
    apache配置--虚拟目录
    简化LINUX的命令输入 简化linux命令 快捷键 短路径
    apache vhost 访问权限配置
    php中常用设置
    win7配置nginx + php
    restful php
    php 笔记 汇总 学习
    mysql修改表结构
    mysql 优化工具
  • 原文地址:https://www.cnblogs.com/yuanshixingdan/p/5559778.html
Copyright © 2011-2022 走看看