B - Film Festival disjoint-set

Description

A session of the selected film festival, a total of two films in the final adjudication process, there are n spectators, everyone has a chance to vote, everyone voted for one of the movies accordance with the rules. In order to understand the situation, the reporter asked a number of people, asked a total of m times, particularly amazing is that reporters always ask two people, and two men regarded the vote for the same movie, the audience numbered 1 ~ n .
Input

Plural sets of inputs, each line of the first two integers n, m (2 <= n <= 100000,0 <= m <n / 2), the next line data m, m th interrogation, each row of data has two integers a, b representative of the number of viewers (1 <= a, b < = n), and spectators b a viewer to vote for the same movie, the next line is the two integers c, d (1 <= c , d <= n).
Output

For each set of input and output line, if the audience c and d the audience to vote for the same movie, the output of "same", if not determine, the output "not sure".
Sample

Input

5 2
1 2
2 3
1 3
5 2
1 2
3 4
1 4
5 2
1 2
3 4
2 5
Output

same
not sure
not sure

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int f[100010];
void init(int n)
{
    for(int i=1;i<=n;i++)
        f[i] = i;
}
int getf(int v)
{
    if(f[v] == v)
        return v;
    else
    {
        f[v] = getf(f[v]);
        return f[v];
    }
}
void Merge(int u,int v)
{
    int t1,t2;
    t1 = getf(u);
    t2 = getf(v);
    if(t1 != t2)
    {
        f[t2] = t1;
    }
}
int main()
{
    int n,k;
    int u,v;
    int a,b;
     while(~scanf("%d %d",&n,&k))
    {
        
        init(n);
        for(int i=0;i<k;i++)
        {
            scanf("%d %d",&u,&v);
            Merge(u,v);
        }
        scanf("%d %d",&a,&b);
        if(getf(a) == getf(b))
            printf("same\n");
        else
            printf("not sure\n");
    }
    return 0;

}

Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104905578