zoukankan      html  css  js  c++  java
  • HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

    Background 
    Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. 

    Problem 
    Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it. 

    InputThe first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.OutputThe output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.Sample Input

    2
    3 3
    1 2
    2 3
    1 3
    4 2
    1 2
    3 4

    Sample Output

    Scenario #1:
    Suspicious bugs found!
    
    Scenario #2:
    No suspicious bugs found!
    
    
            
     

    Hint

    Huge input,scanf is recommended.
    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 1000000+20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int t,n,m,d,x,y;
    int fa[maxn*2];
    inline int read()
    {
        int sum=0;
        char ch=getchar();
        while(ch>'9'||ch<'0') ch=getchar();
        while(ch>='0'&&ch<='9') sum=sum*10+ch-48,ch=getchar();
        return sum;
    }
    int Find(int x)
    {
        return fa[x]==x ? x:fa[x]=Find(fa[x]);
    }
    void join(int x,int y)
    {
        int fx=Find(x);
        int fy=Find(y);
        if(fx!=fy) fa[fx]=fy;
    }
    bool same(int x,int y)
    {
        return Find(x)==Find(y);
    }
    int main()
    {
        int cas=1;
        scanf("%d",&t);
        while(t--)
        {
            int f=0;
            n=read(),m=read();
            int ans=0;
            for(int i=1;i<=2*n;i++) fa[i]=i;
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d",&x,&y);
                if(x>n||y>n){f=0;continue;}
                if(same(x,y) || same(x+n,y+n))
                    f=1;
                else
                {
                    join(x+n,y);
                    join(x,y+n);
                }
            }
            printf("Scenario #%d:
    ",cas++);
            if(f) printf("Suspicious bugs found!
    "); //1 3
            else printf("No suspicious bugs found!
    ");
            cout<<endl;
        }
        return 0;
    }
    
    /*
    【题意】
    略
    
    【类型】
    带权并查集
    
    【分析】
    首先考虑如何用并查集解决问题:我们可以设置数组[1,2n],其中[a]表示男,[a+n]表示女
    从头扫描每一对关系,将a和b+n,a+n和b合并(两人异性才可以合并在一个圈子)
    合并之前检查a和b,a+n和b+n是否在同一集合,如果是,则为同性恋
    
    【时间复杂度&&优化】
    
    
    【trick】
    
    
    【数据】
    */
  • 相关阅读:
    Springboot 报错 Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    layui时间戳转日期踩坑
    前端 传入Date 为空的解决办法
    解决springdatajpa设置默认值保存null无效的问题
    2020-04-12工作记录
    js原型链继承的傻瓜式详解
    一个关于python装饰器参数的问题
    【转】Unicode utf8等编码类型的原理
    c/c++内存泄露的检测方法
    【转】什么是动态规划?动态规划的意义是什么
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9435256.html
Copyright © 2011-2022 走看看