zoukankan      html  css  js  c++  java
  • Back to Underworld(搜索)

    Back to Underworld
    Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

    Description

    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

    2

    2

    1 2

    2 3

    3

    1 2

    2 3

    4 2

    Sample Output

    Case 1: 2

    Case 2: 3

    题解:两拨人打架,问最每队最大的可能人数是多少;搜索下。。。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<vector>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    #define mem(x, y) memset(x, y, sizeof(x))
    typedef long long LL;
    void SI(int &x){scanf("%d", &x);}
    void SI(LL &x){scanf("%lld", &x);}
    const int MAXN = 1e5 + 100;
    int vis[MAXN], usd[MAXN];
    int sum1, sum2;
    vector<int>vec[MAXN];
    void dfs(int i, int cur){
        if(cur)sum1++;
        else sum2++;
    //    vis[i] = 1;
        for(int j = 0; j < vec[i].size(); j++){
            int v = vec[i][j];
            if(vis[v])continue;
            vis[v] = 1;
            dfs(v, cur ^ 1);
        }
    }
    int main(){
        int T,kase = 0, n;
        scanf("%d", &T);
        while(T--){
            scanf("%d", &n);
            int x, y, tp = 0;
            for(int i = 0; i <= MAXN; i++){
                vis[i] = 0;
                usd[i] = 0;
                vec[i].clear();
            }
            for(int i = 0; i < n; i++){
                scanf("%d%d", &x, &y);
                vec[x].push_back(y);
                vec[y].push_back(x);
                usd[x] = usd[y] = 1;
                tp = max(tp, x);
                tp = max(tp, y);
            }
            int ans = 0;
            //printf("tp = %d
    ", tp);
            for(int i = 1; i <= tp; i++){
                if(vis[i] || !usd[i])continue;
                sum1 = sum2 = 0;
                vis[i] = 1;
                dfs(i, 0);
            //    printf("%d %d %d 
    ", sum1,sum2,i);
                ans += max(sum1, sum2);
            }
            printf("Case %d: %d
    ", ++kase, ans);
        }
        return 0;
    }
     
  • 相关阅读:
    输入一个nxn矩阵各元素的值,球出两条对角线元素之和
    打印杨辉三角
    编写一个函数,实现两个字符串的连接功能
    字符串置换。将字符串s中的出现的字符s1用字符s2置换
    有一行文字,要求删去其中某个字符
    自定义函数delstr()的功能是删去字符串s1中所有的"*"
    用微软的kestrel在Linux上利用Apache架设Asp.Net Core环境
    2012年8月14日 星期二 equals()方法 (冲突备份)
    jquery 操作DOM 案例
    FileUpload 控件上传图片和文件
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5390711.html
Copyright © 2011-2022 走看看