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;
    }
     
  • 相关阅读:
    scanf()常犯错误
    C语言,链表反转
    c语言: 冒泡排序
    判断一个数是否为2的若干次幂
    计算一个整数中含1的个数
    ELF文件的加载过程(load_elf_binary函数详解)--Linux进程的管理与调度(十三)
    Linux进程启动过程分析do_execve(可执行程序的加载和运行)---Linux进程的管理与调度(十一)
    Linux内核线程kernel thread详解--Linux进程的管理与调度(十)
    Linux下进程的创建过程分析(_do_fork do_fork详解)--Linux进程的管理与调度(八)
    Linux下2号进程的kthreadd--Linux进程的管理与调度(七)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5390711.html
Copyright © 2011-2022 走看看