• 并查集


    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.

    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.
     
    题意是给定n个人和m组数据,每组数据表示的是它们是异性,如果出现了矛盾就输出“Suspicious bugs found!”,或则输出“No Suspicious bugs found!”
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int maxn = 2e3;
     5 int fa[maxn*2+10];
     6 bool mark;
     7 int find(int x){
     8     if(fa[x] == x)return x;
     9     else return fa[x] = find(fa[x]);
    10 }
    11 void uni(int x, int y){
    12     int xx, yy;
    13     xx = find(x);
    14     yy = find(y-maxn);
    15     if(xx == yy){
    16         mark = false;
    17         return;
    18     }
    19     yy = find(y);
    20     if(xx != yy) fa[xx] = fa[yy];
    21 }
    22 int main(){
    23     int t, n, m, x, y, k = 1;
    24     scanf("%d",&t);
    25     while(t--){
    26         scanf("%d%d",&n,&m);
    27         for(int i = 1; i <= maxn+n; i++)fa[i] = i;
    28         mark = true;
    29         for(int i = 1; i <= m; i ++){
    30             scanf("%d%d",&x,&y);
    31             if(mark){
    32                 uni(x, y+maxn);
    33                 uni(y, x+maxn);
    34             }
    35         }
    36         printf("Scenario #%d:
    ", k++);
    37         if(mark) printf("No suspicious bugs found!
    ");
    38         else printf("Suspicious bugs found!
    ");
    39         printf("
    ");
    40     }
    41     return 0;
    42 }
     

    How Many Answers Are Wrong

     
    TT and FF are ... friends. Uh... very very good friends -________-b 

    FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored). 

    Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers. 

    Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose. 

    The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence. 

    However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed. 

    What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers. 

    But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)

    InputLine 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions. 

    Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N. 

    You can assume that any sum of subsequence is fit in 32-bit integer. 
    OutputA single line with a integer denotes how many answers are wrong.Sample Input

    10 5
    1 10 100
    7 10 28
    1 3 32
    4 6 41
    6 6 1

    Sample Output

    1
    题意是给出a到b之间的距离c,求由多少组是错误的,一开始想了好久也没想出来,看了题解才知道,写的太溜了。
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int maxn = 2e5+10;
     5 int fa[maxn], sum[maxn];
     6 
     7 int find(int x){
     8     if(fa[x] == x)return x;
     9     int t = fa[x];
    10     fa[x] = find(fa[x]);
    11     sum[x] += sum[t];
    12     return fa[x];
    13 }
    14 void uni(int x, int y, int a, int b, int c){
    15     if(x > y){
    16         fa[y] = x;
    17         sum[y] = sum[a] - sum[b] - c;
    18     }else{
    19         fa[x] = y;
    20         sum[x] = sum[b] - sum[a] + c;
    21     }
    22 }
    23 int main(){
    24     int n, m;
    25     while(~scanf("%d%d",&n,&m)){
    26         for(int i = 0; i <= 200001; i++){
    27             fa[i] = i;
    28             sum[i] = 0;
    29         }
    30         int a, b, c, ans = 0;
    31         while(m--){
    32             scanf("%d%d%d",&a,&b,&c);
    33             b++;
    34             int x = find(a);
    35             int y = find(b);
    36             if(x == y && sum[a] != sum[b] + c) ans++;
    37             else if(x != y) uni(x, y, a, b, c);
    38         }
    39         printf("%d
    ",ans);
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    viewport的故事(一)
    Laravel项目部署上线(阿里云 Ubuntu 16.04)
    Javascript数组方法总结
    html中编写js的方式
    js验证表单并提交
    html+css+js实现复选框全选与反选
    Cookie记住账号密码
    加密口令
    ASP.NET 在GridView中自动添加序号列
    ASP.NET使用递归遍历TreeView树
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/6754339.html
Copyright © 2020-2023  润新知