POJ 2492(权值向量并查集)

Posted jrfr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2492(权值向量并查集)相关的知识,希望对你有一定的参考价值。

A Bug‘s Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 49835   Accepted: 16094

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!

A Bug‘s Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 49835   Accepted: 16094

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!

题目:T个样例,每个样例,第一行n代表n个点,m个询问。接下来m行每行两个数x,y。表示x,y异性,问当前测试样例询问有没有错误。
思路:感觉属于向量的水题了,比食物链简单,这个题的d只表示两个值,0和1,0代表同性,1代表异性,注意更改d的时候加2.
   输出格式每个测试结束多输出一个空格,d别忘了清零(wa1发)
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
ll gcd(ll a,ll b)return b?gcd(b,a%b):a;
ll lcm(ll a,ll b)return a/gcd(a,b)*b;

#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
using namespace std;
int fa[41000],d[41000];
int Find(int x)

    if(fa[x] == x) return x;
    int root = Find(fa[x]);
    d[x] = (d[x] + d[fa[x]] + 2) % 2;
    return fa[x] = root;


int main()


    int T;
    scanf("%d",&T);
    for(int t = 1;t<=T;++t)
    
        int flag = 0;
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i =1;i<=n;++i) fa[i] = i;
        memset(d,0,sizeof(d));
        for(int i=1;i<=m;++i)
        
            int x,y;
            scanf("%d%d",&x,&y);
            if(flag == 1) continue;
            int fx = Find(x),fy = Find(y);
            if(fx == fy)
                if((d[x] + d[y] +2) % 2 != 1) flag = 1;
            
            else 
                fa[fy] = fx;
                d[fy] = (d[x] + d[y] + 1 + 2) % 2;
            
        
        if(flag == 1) printf("Scenario #%d:\nSuspicious bugs found!\n\n",t);
        else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",t);
    

 

 

以上是关于POJ 2492(权值向量并查集)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2492 A Bug's Life (带权并查集 && 向量偏移)

poj 2492 a bug's life 简单种类并查集

poj2492 A Bug's Life (并查集拓展)

poj2492(种类并查集)

POJ 2492 A Bug's Life (并查集)

A Bug's Life POJ - 2492 (带权并查集)