zoukankan      html  css  js  c++  java
  • 【POJ】2492 A Bug's Life

    题目链接:http://poj.org/problem?id=2492

    题意:给你n个虫子,m组实验。让你帮科学家找一下有没有虫子是同性恋。

    题解:假设x是一个性别,x+n为另一个性别。如果在同性的集合里找到了其他性别的。说明有同性恋虫子。

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const int maxn = 1e6+10;
     5 
     6 int f[maxn];
     7 
     8 void init(int n){
     9     for(int i = 0 ; i <= n ;i++){
    10         f[i] = i;
    11     }
    12 }
    13 int find(int x){
    14     if( x != f[x]){
    15         f[x] = find(f[x]);
    16     }
    17     return f[x];
    18 }
    19 void join(int a,int b){
    20     int x=find(a);
    21     int y=find(b);
    22     if(x!=y)
    23        f[x]=y;
    24 }
    25 
    26 bool judge(int x,int y){
    27    x=find(x);
    28    y=find(y);
    29    if(x!=y)
    30     return true;
    31    return false;
    32 }
    33 //x 为一种性别,x+n为另一种
    34 int n,m;
    35 int main(){
    36     int T;
    37     cin>>T;
    38     for(int t = 1; t <= T; t++){
    39         scanf("%d%d",&n,&m);
    40         init(n * 2);
    41         int x,y;
    42         bool flag = true;
    43         while(m--){
    44             scanf("%d%d",&x,&y);
    45             if(judge(x,y) || judge(x + n , y + n)){
    46                 join(x,y+n);//同性
    47                 join(x+n,y);
    48             }
    49             else    flag = false;
    50 
    51         }
    52         printf("Scenario #%d:
    ",t);
    53         if(flag)
    54             printf("No suspicious bugs found!
    
    ");
    55         
    56         else
    57             printf("Suspicious bugs found!
    
    ");
    58             
    59     }
    60     
    61     return 0;
    62 } 
  • 相关阅读:
    js原型杂谈
    arguments.callee.caller
    $resource
    sql的四种匹配模式
    AMD规范
    module.ngdoc
    angularjs杂谈
    浏览器前缀
    css21规范学习
    <meta>标签
  • 原文地址:https://www.cnblogs.com/Asumi/p/9747701.html
Copyright © 2011-2022 走看看