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);
    }

}
 

原文地址:https://www.cnblogs.com/jrfr/p/11409912.html

时间: 2024-10-07 21:15:08

POJ 2492(权值向量并查集)的相关文章

HDU 1863:畅通工程(带权值的并查集)

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16075    Accepted Submission(s): 6677 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

HDU 1863:畅通project(带权值的并查集)

畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16075    Accepted Submission(s): 6677 Problem Description 省政府"畅通project"的目标是使全省不论什么两个村庄间都能够实现公路交通(但不一定有直接的公路相连,仅仅要能间接通过公路可达就可以).经过

带权值的并查集

博文:https://blog.csdn.net/yjr3426619/article/details/82315133 带全并查集 路径压缩,表达每个当前node与 当前node所在的并查集的root 之间的关系 并查集一定是单向连通的,所以一些node处理时 [node1,node2] weight 可能需要把 一端的闭区间变为开区间 俩个相对信息求出绝对信息   --水题 例题 : HDU 3038 //带权值并查集 #include<iostream> #include<cstd

Wikioi 2492 树状数组+并查集(单点更新区间查询)

刚开始做的时候用线段树做的,然后就跳进坑里了--因为要开方,所以区间的值都得全部变,然后想用lazy标记的,但是发现用不了,单点更新这个用不了,然后就不用了,就T了.然后实在不行了,看了别人的题解,原来是用树状数组+并查集的方法,唉--没想到啊! 因为开方之后多次那个数就会变成1了,所以是1的时候开方下去就没用了.树状数组更新的时候就把其更新的差更新即可,太机智了这题-- 昨天做了,然后出错找了好久都找不出来,原来是把s[i]写成c[i]了,然后答案一直错,晕-- #include <iostr

poj 1611:The Suspects(并查集,经典题)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

POJ 1611 The Suspects(并查集)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 22296   Accepted: 10842 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

POJ 2263 Heavy Cargo(二分+并查集)

题目地址:POJ 2263 这题是在网上的一篇关于优先队列的博文中看到的..但是实在没看出跟优先队列有什么关系..我用的二分+并查集做出来了... 二分路的载重量.然后用并查集检查是否连通. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #

POJ 2528 Mayor&#39;s posters 并查集+离散化做法

Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49767 Accepted: 14434 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters

poj 2524:Ubiquitous Religions(并查集,入门题)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted: 11807 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi