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;
    }
     
  • 相关阅读:
    HTTP Header 详解
    面试题----网页/应用访问慢突然变慢,如何定位问题
    PHP实现斐波那契数列
    常见的HTTP返回状态值
    通过实例理解单列索引、多列索引以及最左前缀原则
    Btree索引和Hash索引
    遍历和删除文件夹
    面试题之----禁掉cookie的session使用方案
    面试题之----写个函数来解决多线程同时读写一个文件的问题
    heredoc
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5390711.html
Copyright © 2011-2022 走看看