POJ 2492 (and check set)

Source: Internet
Author: User

Test instructions: One scientist put forward the hypothesis that a worm is heterosexual, not homosexual, and then starts experimenting to verify conjecture, gives n bugs, numbers from 1 to N, gives the number of the Q Group of love worms, and asks whether to verify conjecture.
Puzzle: Array multiplication, give each worm a heterosexual object collection, and then each time the input of the two worms if not within the same set, placed in the other heterosexual set, or can not verify the conjecture.

#include <stdio.h>ConstintN =4005;intNQ, Pa[n];intGet_parent (int x) {return x= = pa[x] ?x: pa[x] = Get_parent (pa[x]);}intMain () {intT, CAS =1; scanf"%d", &t); while(t--) {scanf ("%d%d", &n, &Q);intnn =2N for(inti =1; I <= nn; i++) Pa[i] = i;intFlag =0; while(Q--) {intA, B; scanf"%d%d", &a, &b);intpx = Get_parent (a);intPY = get_parent (b);if(px = = PY) flag =1;Else{intT1 = get_parent (a + N);intt2 = get_parent (b + N);                PA[PX] = T2;            Pa[py] = T1; }        }if(CAs >1)printf("\ n");printf("Scenario #%d: \ n", cas++);if(flag)printf("Suspicious bugs found!\n");Else            printf("No suspicious bugs found!\n"); }return 0;}

Another approach, with the right and check set, plus an array rel[i] indicates whether I and parent node is same-sex, looking for root node update rel[i], if found that the input of the two worms is the same root node and the same root node relationship, the two insects are homosexual, otherwise put in a set, and update the rel of the root node of one of the worms.

#include <stdio.h>ConstintN =4005;intNQ, Pa[n], rel[n];intGet_parent (int x,int&r) {inttemp =x; R =1; while(x! = pa[x]) {if(rel[x] ==0) R =!r;x= pa[x]; } Pa[temp] =x; Rel[temp] = r;returnpa[x];}intMain () {intT, CAS =1; scanf"%d", &t); while(t--) {scanf ("%d%d", &n, &Q); for(inti =1; I <= N;            i++) {Pa[i] = i; Rel[i] =1; }intFlag =0; while(Q--) {intA, B, R1, R2; scanf"%d%d", &a, &b);intpx = Get_parent (A, R1);intPY = Get_parent (b, R2);if(px = = Py && R1 = = r2) flag =1;Else{Pa[py] = px; Rel[py] = R1 = = r2?0:1; }        }if(CAs >1)printf("\ n");printf("Scenario #%d: \ n", cas++);if(flag)printf("Suspicious bugs found!\n");Else            printf("No suspicious bugs found!\n"); }return 0;}

POJ 2492 (and check 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.