poj1703 Find them, Catch them

Source: Internet
Author: User

and check the set.

This problem has been wrong many times before.

See Code for analysis.

http://poj.org/problem?id=1703

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intMAXN = 1e5 +Ten;6 Const CharStr1[] ="Not sure yet.";7 Const CharStr2[] ="In different gangs.";8 Const CharStr3[] ="In the same gang.";9 intFA[MAXN], BELONG[MAXN], NEX[MAXN];Ten intN, M, U, V, K; One Charop; A  - intFatherintu) { -     //What would cause an INFINITE LOOP the     if(Fa[u] = =-1)returnu; -     intTEM =father (Fa[u]); -Belong[u] =Belong[tem]; -Fa[u] =tem; +     returntem; - } +  A voidsolve () { at     if(OP = ='D'){ -         if(Belong[u] = =-1&& Belong[v] = =-1){ -             //The simplest case -Belong[u] = k++; -BELONG[V] = k++; -Nex[u] =v; inNEX[V] =u; -             return; to         } +         if(Belong[u]! =-1&& Belong[v] = =-1){ -             //Notice that's never mentioned, but u are already processed the             //since is U-vistied, u got its partner *             intFu = Father (u), FU1 =father (Nex[u]); $             //draw a line from V to U1Panax NotoginsengFA[V] =FU1; -NEX[V] =Fu; theBELONG[V] =BELONG[FU1]; +             return; A             //match a virtue partner for node U the         } +         if(Belong[u] = =-1&& Belong[v]! =-1){ -             intFV = Father (v), Fv1 =father (Nex[v]); $Fa[u] =Fv1; $Nex[u] =FV; -Belong[u] =BELONG[FV1]; -             return; the         } -         if(Belong[u]! =-1&& Belong[v]! =-1){Wuyi             //notice that both U and V are already visited the             intFu = Father (u), FU1 =father (Nex[u]); -             intFV = Father (v), Fv1 =father (Nex[v]); Wu             intBU = Belong[fu] >>1; -             intBV = BELONG[FV] >>1; About             if(Bu >BV) { $                 //V is relatively primitive -FA[FU] =Fv1; -FA[FU1] =FV; -                 return; A             } +             if(Bu <BV) { theFA[FV] =FU1; -FA[FV1] =Fu; $                 return; the             } the         } the     } the     if(OP = ='A'){ -         intFu =father (U); in         intFV =father (v); the         if(Belong[fu] = =-1|| BELONG[FV] = =-1) puts (str1); the         Else if(Belong[fu] = =BELONG[FV]) puts (STR3); About         Else if(Belong[fu] = = (1^BELONG[FV])) Puts (STR2); the         Elseputs (str1); the     } the } +  - intMain () { theFreopen ("In.txt","R", stdin);Bayi     intT; thescanf"%d", &T); the      while(t--){ -scanf"%d%d", &n, &m); -Memset (Belong,-1,sizeofbelong); thememset (FA,-1,sizeofFA); theK =0; the          for(inti =0; I < m; i++){ thescanf"%c%d%d", &op, &u, &v); - solve (); the         } the     } the}
View Code

poj1703 Find them, Catch them

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.