A Bug's Life_poj2492_并查集

来源:互联网 发布:最强淘宝系统虾兵蟹将 编辑:程序博客网 时间:2024/05/22 02:08

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.

Source

TUD Programming Contest 2005, Darmstadt, Germany

题目大意

有n只分性别的虫子m个关♂系,求是否有至少一只虫子是同性恋?

思路

为题目中具有钻xia研gao精神的砖家鼓掌,啪啪啪啪(雾)

对于这种查询类的问题,我们想到用并查集解决,于是就有:

  • 如果x,y与y,z发生关♂系,则y,z为同性,属于同一集合,记录他们的性别
  • 如果x,y性别有过关♂系,且性别相同,则有同性恋
  • 注意不能提前退出循环(如果像我一样边输入边判断的话)

于是就有了如下瞎搞
实测c++快过pascal,快很多很多很多很多,表示不爽
说白了还是很久以前没有刷到的题目

代码/pas:

var  f,r:array[1..2010]of longint;  n,m,g,p:longint;function find(x:longint):longint;var  t:longint;begin  t:=f[x];  if t=x then exit(x);  f[x]:=find(t);  r[x]:=(r[x]+r[t])and 1;  exit(f[x]);end;procedure union(x,y:longint);var  i,j:longint;begin  i:=find(x);  j:=find(y);  if i=j then exit;  f[i]:=j;  r[i]:=(r[x]+1-r[y])and 1;end;procedure main;var  i,x,y:longint;  flag:boolean;begin  dec(p);  inc(g);  readln(n,m);  flag:=false;  fillchar(r,sizeof(r),0);  for i:=1 to n do f[i]:=i;  for i:=1 to m do  begin    readln(x,y);    if (find(x)=find(y))and(r[x]=r[y]) then    flag:=true    else    union(x,y);  end;  writeln('Scenario #',g,':');  if flag then  writeln('Suspicious bugs found!')  else  writeln('No suspicious bugs found!');  writeln;end;begin  readln(p);  while p>0 do  main;end.

代码/c++:

#include <stdio.h>#include <cstring>int f[2001]={0};int r[2001]={0};int find(int x){    int tmp=f[x];    if (tmp==x)        return x;    f[x]=find(tmp);    r[x]=(r[x]+r[tmp])& 1;    return f[x];}void add(int x,int y){    int i=find(x);    int j=find(y);    if (i!=j)     {        f[i]=j;        r[i]=(r[x]+1-r[y])& 1;    }}int main(){    int cnt=0;    int n,m,t;    scanf("%d",&t);    while (t--)    {        scanf("%d%d",&n,&m);        memset(r,0,sizeof(r));        for (int i=1;i<=n;i++)            f[i]=i;        bool flag=false;        for (int i=1;i<=m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            if (find(x)==find(y)&&r[x]==r[y])                flag=true;            else                add(x,y);        }        printf("Scenario #%d:\n",++cnt);        if (flag)            printf("Suspicious bugs found!\n\n");        else            printf("No suspicious bugs found!\n\n");    }    return 0;}
1 0
原创粉丝点击