Poj 1703 find them, catch them and query the set

Source: Internet
Author: User
/* Combine the enemy and friends in a collection, and use another array (rel [I]) to store I and the set to represent the relationship between elements (that is, saved in Ji [I. If the two elements are not in the same set, the relationship between them is unclear. If the two elements are in the same set: 1. they have the same relationship with the element represented by the set, and they are friends 2. the difference is the enemy */# include <stdio. h> int Ji [100010], rel [100010]; // Ji [] stores the set representing elements, RE; [I] int root (INT X) {int t; If (x = Ji [x]) return X; T = Ji [X]; JI [x] = root (JI [x]); rel [x] = (rel [T] + REL [x]) % 2; // The relationship between update and set representing elements return Ji [X];} int main () {int t, n, m, I; char O; int X, Y, FX, FY; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M ); for (I = 0; I <= N; I ++) {Ji [I] = I; rel [I] = 0;} If (n = 2) {Ji [2] = 1; rel [2] = 1;} for (I = 1; I <= m; I ++) {getchar (); scanf ("% C % d", & O, & X, & Y); FX = root (x); FY = root (y ); if (O = 'A') {If (FX = FY) {If (rel [x] = rel [y]) printf ("in the same gang. \ n "); else printf (" in different gangs. \ n ");} else printf (" not sure yet. \ n ") ;}else {If (FX <FY) {Ji [FY] = FX; rel [FY] =! (Rel [x] + REL [y]) % 2);} else {Ji [FX] = FY; rel [FX] =! (Rel [x] + REL [y]) % 2) ;}}} return 0 ;}

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.