POJ 1703 Find Them, Catch them: species and collection

Source: Internet
Author: User

Link:

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

Topic:

Find Them, Catch them

Time limit:1000ms Memory limit:10000k
Total submissions:22289 accepted:6648

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the two gangs in the city, G Ang Dragon and Gang Snake. However, the police I needs to identify which gang a criminal to. The present question is, given two criminals; Does they belong to a same clan? You are must give your judgment based on incomplete information. (Since The gangsters are always acting secretly.)

Assume N (n <= 10^5) Criminals are currently in Tadu City, numbered from 1 to N. Course, at least one of them belongs to Gang Dragon, and the same for Gang. You are given m (m <= 10^5) messages in sequence, which are in the following two:

1. D [A] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different.

2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This is requires to decide whether a and B belong to a same gang.

Input

The ' The ' input contains a single integer t (1 <= t <=), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one as described Above.

Output

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

For each message "a [a] [b]", your program should give the judgment based on the information got. The answers might be one of ' in the same gang. ', ' in different gangs. ' and ' not sure yet. '

Sample Input

1
5 5
a 1 2
D 1 2
a 1 2
D 2 4
a 1 4

Sample Output

Not sure yet.
In different gangs.
In the same gang.

Source

POJ monthly--2004.07.18

Analysis and Summary:

Have done some of the right and check the collection, and then do the so-called "category and check the collection," it seems that the epiphany.

The difference between the kind and the right and the collection is not very large, the key difference is that the kind and the collection is only with the right and check the set again to get a% of the operation, and then the remainder of which means he belongs to which kind.

This question only two kinds, namely only 0 and 12 kinds, for two different kinds, then the weight value is the difference 1, therefore according to take the right and the collection method to add 1, then takes the surplus 2 can.

Code:

#include <cstdio> const int N = 100005;  
    
int n, M, F[n], rank[n];  
inline void init () {for (int i=1; i<=n; ++i) f[i]=i,rank[i]=0;  
    int find (int x) {if (x==f[x]) return f[x];  
    int fa=f[x];  
    F[X] = find (f[x]);  
    RANK[X] = (RANK[X]+RANK[FA]) &1;  
return f[x];  
    } inline bool Union (int x,int y) {int a=find (x), B=find (y);  
    if (a==b) return false;  
    F[B] = A;  
RANK[B] = (rank[x]-rank[y]+1) &1;  
    int main () {int t,a,b,fa,fb;  
    Char ch;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d%d%*c", &n,&m);  
        Init ();  
            for (int i=0; i<m; ++i) {scanf ("%c%d%d%*c", &ch,&a,&b);  
            if (ch== ' D ') {Union (a,b);  
                else{FA = Find (a), fb=find (b);  
         if (FA==FB) {if (Rank[a]==rank[b]) puts ("in the same gang.");           Else puts ("in different gangs.");  
            Else puts ("not sure yet.");  
}} return 0; }

Author: csdn Blog shuangde800

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.