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;
    }
     
  • 相关阅读:
    【滚动数组】 dp poj 1036
    【多重背包】 poj 2392
    【树状数组】 poj 2352
    【矩阵压缩】 poj 1050
    【八皇后问题】 回溯算法
    【矩阵加速】 矩阵 快速幂
    【快速幂】 模板
    【二分图】 判断是否是二分图
    Struts2标签库
    Struts2之获取ServletAPI
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5390711.html
Copyright © 2011-2022 走看看