zoukankan      html  css  js  c++  java
  • HDU 1829

    Problem 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!
    Hint
    Huge input,scanf is recommended.
     
     
    大致题意:
      检查一堆数据中是否有同性恋,找出主要矛盾是如果1交配2,2交配3,而1又交配3,则矛盾。找到一组就行。
    解题思路:
      用并查集做元素关系判断,主要建立余数体系;
     
     1 #include <cstdio>
     2 using namespace std;
     3 int f[2000+5],num[2000+5],t,n,m,a,b,fa,fb;
     4 int sf(int x){
     5     if(x==f[x]) return x;
     6     else {
     7         int fx=sf(f[x]);
     8         num[x]=(num[x]+num[f[x]])%2;
     9         return f[x]=fx;
    10     }
    11 }
    12 int main(){
    13     scanf("%d",&t);
    14     for(int k=1;k<=t;k++){
    15         printf("Scenario #%d:
    ",k);
    16         scanf("%d%d",&n,&m);    bool flag=1;
    17         for(int i=0;i<=n;i++) f[i]=i,num[i]=0;
    18         for(int i=1;i<=m;i++){
    19             scanf("%d%d",&a,&b);
    20             if(flag){
    21                 fa=sf(a); fb=sf(b); 
    22                 if(fa==fb){ if((num[a]-num[b]+2)%2!=1) flag=0;}//避免负数 
    23                 else f[fb]=fa,num[fb]=(num[a]-num[b]+1+2)%2;
    24             }
    25         }
    26         if(flag) puts("No suspicious bugs found!
    ");
    27         else  puts("Suspicious bugs found!
    ");
    28     } return 0;
    29 }
    我自倾杯,君且随意
  • 相关阅读:
    【爬虫】对新笔趣阁小说进行爬取,保存和下载
    第二次学习记录(Python)
    大厂Redis高并发场景设计,面试问的都在这!
    自定义注解!绝对是程序员装逼的利器!!
    利用Python将多张图片合成视频
    理解Python闭包,这应该是最好的例子
    爬取某知名网站的数据
    用Python爬取日向、樱坂成员blog中的JPG文件的url并将其下载到本地
    爬虫入门经典(二十二) | 破解base64加密之爬取安居客
    现实世界的Windows Azure:采访AppPoint的技术总监Rajesekar Shanmugam
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5164380.html
Copyright © 2011-2022 走看看