HDU 1829 A Bug ' s Life (and set)

Source: Internet
Author: User

Topic Link: please poke here.

Questions and ideas: given n Bugs (shen me gui) and m to bugs relationship, assuming that the relationship is two bugs of the relationship between men and women, then there is no homosexual situation.

So if A and b are male and female, B and C are male and female, then the gender of a and c we can think of as the same. We can build two and check sets, a class of male-male relations, a class of female relations.

So if there is a ring in the male-male relationship (that is, there is a common root), then the homosexual relationship appears.

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define N 2000+10using namespace Std;int ok;int F[n],p[n]; Use p[] to express the relationship between men and women.        void Init (int n) {for (int i=1;i<=n;i++) {f[i]=i;    p[i]=0; }}int Find (int x) {return x==f[x]?x:f[x]=find (f[x]);} void Merge (int a,int b)//a and B have male and female relations.    {int Ra=find (a), rb=find (b);        if (RA==RB) {//presence of homosexuality.        ok=0;    Return } if (P[ra]) f[p[ra]]=rb; P[ra] and RB are the same relationship (male male) if (P[RB]) F[p[rb]]=ra;    P[RB] and RA are the same relationship (female) P[RA]=RB; P[rb]=ra;}    int main () {int t,c=0;    scanf ("%d", &t);        while (t--) {int n,m;        scanf ("%d%d", &n,&m);  Init (n);        ok=1;            for (int i=0;i<m;i++) {int A, B;            scanf ("%d%d", &a,&b);        Merge (A, b);        } printf ("Scenario #%d:\n", ++c);        if (OK) puts ("No suspicious Bugs found!");        Else puts ("suspicious bugs found!");    Puts (""); } return 0;}


HDU 1829 A Bug ' s Life (and set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.