zoukankan      html  css  js  c++  java
  • poj 2492

    A Bug's Life
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 41952   Accepted: 13619

    Description

    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.

    Input

    The 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.

    Output

    The 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!

    感觉像套模板似的qwq。
     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 const int maxn=2006;
     5 int fa[maxn],val[maxn];
     6 
     7 void init()
     8 {
     9     for(int i=0;i<maxn;i++){
    10         fa[i]=i;
    11         val[i]=0;
    12     }
    13 }
    14 
    15 int find_fa(int x)
    16 {
    17     if(fa[x]==x) return x;
    18     int tmp=fa[x];
    19     fa[x]=find_fa(tmp);
    20     val[x]=val[x]^val[tmp];
    21     return fa[x];
    22 }
    23 
    24 int main()
    25 {
    26     int T;
    27     scanf("%d",&T);
    28     for(int kkk=1;kkk<=T;kkk++){
    29         int n,m;
    30         scanf("%d%d",&n,&m);
    31         init();
    32         int ans=0;
    33         for(int i=1;i<=m;i++){
    34             int x,y;
    35             scanf("%d%d",&x,&y);
    36             int opx,opy,tmp=1;
    37             opx=find_fa(x);
    38             opy=find_fa(y);
    39             if(opx!=opy){
    40                 fa[opy]=opx;
    41                 val[opy]=val[x]^val[y]^tmp;
    42             }
    43             if(opx==opy&&val[x]^val[y]!=tmp){
    44                 ans=1;
    45             }
    46         }
    47         printf("Scenario #%d:
    ",kkk);
    48         if(ans==1) printf("Suspicious bugs found!
    
    ");
    49         else printf("No suspicious bugs found!
    
    ");
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    2-sat问题,输出方案,几种方法(赵爽的论文染色解法+其完全改进版)浅析 / POJ3683
    hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。
    最小费用最大流粗解 poj2516
    hdu3078 建层次树+在线LCA算法+排序
    hdu 3594 Cactus /uva 10510 仙人掌图判定
    有向图最小路径覆盖方法浅析、证明 //hdu 3861
    hdu 1827 有向图缩点看度数
    条件转化,2-sat BZOJ 1997
    2-sat基础题 BZOJ 1823
    2-sat 分类讨论 UVALIVE 3713
  • 原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9108737.html
Copyright © 2011-2022 走看看