前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu 1829 A Bug's Life(分组并查集(偏移量))

hdu 1829 A Bug's Life(分组并查集(偏移量))

作者头像
Gxjun
发布2018-03-26 15:52:07
9890
发布2018-03-26 15:52:07
举报
文章被收录于专栏:ml

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9204    Accepted Submission(s): 2961

Problem 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

题意:

     1喜欢2,2喜欢3,而1又喜欢3,则矛盾。 (排出同性恋)

基础并查集的新应用:

      分组并查集(偏移量):开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离

    代码:

代码语言:javascript
复制
 1 /*带权值的并查集*/
 2 #include<cstdio>
 3 #define maxn 2005
 4 int father[maxn],rank[maxn];
 5 int tt,nn,mm;
 6 void  init()
 7 {
 8    for(int i=1;i<nn;i++){
 9        father[i]=i;
10        rank[i]=0;
11   }
12 }
13 
14 int fin(int x)
15 {
16   if(x==father[x])   return x;
17   int temp=fin(father[x]);
18   rank[x]=(rank[x]+rank[father[x]])%2;
19   father[x]=temp;
20   return father[x];
21 }
22 int unin(int a,int b)
23 {
24     int x=fin(a);
25     int y=fin(b);
26     if(x==y)
27     {
28         if(rank[a]==rank[b])return 1;  //说明矛盾
29         return 0;
30     }
31     father[x]=y;
32     rank[x]=(rank[a]+rank[b]+1)%2;
33     return 0;
34 }
35 int main()
36 {
37    int a,b;
38    int tag;
39   scanf("%d",&tt);
40   for(int j=1;j<=tt;j++)
41   {
42       tag=0;
43     scanf("%d%d",&nn,&mm);
44     init();
45     for(int i=0;i<mm;i++)
46     {
47        scanf("%d%d",&a,&b);
48        tag+=unin(a,b);
49     }
50     printf("Scenario #%d:\n",j);
51 
52     if(tag)puts("Suspicious bugs found!");
53     else puts("No suspicious bugs found!");
54     puts("");
55   }
56   return 0;
57 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-11-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • A Bug's Life
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档