poj 2492A Bug's Life

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

一开始用函数写,结果错了,没改出来,又换成公式法才过

错误:

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100005
using namespace std;
int father[maxn];
int r[maxn];
int rev(int val)
{
    return val;
}
int real(int v1,int v2)
{
    if(v1==0)
    return v2;
    if(v2==0)
    return v1;
    if(v1==1&&v2==1)
    return 0;
}
int real3(int v1,int v2,int v3)
{
    return real(real(v1,v2),v3);
}
int find(int x)
{
    if(father[x]==x)
    return x;
    int root=find(father[x]);
    r[x]=real(r[x],r[father[x]]);
    return father[x]=root;
}
void unionn(int u,int v,int relation)
{
    int fu=find(u);
    int fv=find(v);
    if(fu!=fv)
    {
        r[fu]=real3(rev(r[u]),relation,r[v]);
        father[fu]=fv;

    }

}
int getreal(int u,int v)
{
    int fu=find(u);
    int fv=find(v);

    return real(r[u],rev(r[v]));
}
int main()
{
    int t;
    scanf("%d",&t);
    int w=0;
   while(t--)
   {w++;
       int n,m;

       scanf("%d%d",&n,&m);
       for(int i=0;i<=n;i++)
       father[i]=i;
       int flag=1;
       while(m--)
       {
           int a,b;
           scanf("%d%d",&a,&b);
           int fa=find(a);
           int fb=find(b);
           if(fa!=fb)
           unionn(a,b,1);
           else if(fa==fb)
          {int v=getreal(a,b);
          if(v==0)
          {flag=0;

          }
       }
       }
       if(flag==1)
        printf("Scenario #%d:\nNo suspicious bugs found!\n\n",w);
        else
          printf("Scenario #%d:\nSuspicious bugs found!\n\n",w);
   }
   return 0;
}

ac

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 2500
int father[maxn];
int v[maxn];
using namespace std;
int find(int x)
{
    if(father[x]==x)
    return x;
    int root=find(father[x]);
    v[x]=(v[x]+v[father[x]])%2;
    return father[x]=root;

}
void unionn(int x,int y)
{
    int fa=find(x);
    int fb=find(y);
    if(fa!=fb)
    {
        father[fa]=fb;
        v[fa]=(v[x]+v[y]+1)%2;
    }
}
int main()
{
    int t,w=0;
    int n,m;
    scanf("%d",&t);
    while(t--)
    {w++;
        int n,m;
        int ok=1;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        father[i]=i;
        memset(v,0,sizeof(v));
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            int fa=find(a);
            int fb=find(b);
            if(fa!=fb)
            unionn(a,b);
            else
            {
                if(v[a]==v[b])
                ok=0;
            }
        }
         if(ok==false)
            printf("Scenario #%d:\nSuspicious bugs found!\n\n",w);
        else if(ok==true)
            printf("Scenario #%d:\nNo suspicious bugs found!\n\n",w);


    }
    return 0;
}

这样写也行

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 2500
int father[maxn];
int v[maxn];
using namespace std;
int find(int x)
{
    if(father[x]==x)
    return x;
    int root=find(father[x]);
    v[x]=(v[x]+v[father[x]])%2;
    return father[x]=root;

}
void unionn(int x,int y)
{
    int fa=find(x);
    int fb=find(y);
    if(fa!=fb)
    {
        father[fa]=fb;
        v[fa]=(v[x]-v[y]+1)%2;
    }
}
int main()
{
    int t,w=0;
    int n,m;
    scanf("%d",&t);
    while(t--)
    {w++;
        int n,m;
        int ok=1;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        father[i]=i;
        memset(v,0,sizeof(v));
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            int fa=find(a);
            int fb=find(b);
            if(fa!=fb)
            unionn(a,b);
            else
            {
                if(v[a]-v[b]==0)
                ok=0;
            }
        }
         if(ok==false)
            printf("Scenario #%d:\nSuspicious bugs found!\n\n",w);
        else if(ok==true)
            printf("Scenario #%d:\nNo suspicious bugs found!\n\n",w);


    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/83211656