电影节

题目描写叙述

某届电影节评选电影。共同拥有两部电影进入最后评选环节,有n名观众。每一个人有一次投票的机会,每一个人都依照规则投给当中一部电影。为了了解情况,记者随机询问了一些人。一共询问了m次,特别奇妙的是,记者每次都询问两个人。并且这两个人都把票投给了同一部电影,观众编号为1~n。

输入

多组输入。每组第一行是两个整数n,m (2 <= n <=100000。0 <= m < n/2),接下来m行数据,表示m次询问,每行数据有两个整数a,b代表观众的编号(1 <= a,b <= n)。观众a和观众b投票给了同一部电影,接下来一行是两个整数c。d(1 <= c,d <= n)。

输出

对于每一组输入,输出一行。假设观众c和观众d投票给同一部电影。输出”same”,假设不能确定,输出”not sure”。

演示样例输入

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

演示样例输出

same
not sure
not sure

///用并查集的方法。用于解决依据已知条件,推断所问的是否相关
#include<stdio.h>
#include<string.h>
#define N 100001
int bin[N];
int find(int x)
{
  int r,i,j;
  r=x;
  while(r!=bin[r])
  {
   r=bin[r];
  }
///压缩路径
 i=x;
  while(i!=r)
  {
    j=bin[i];
    bin[i]=r;
    i=j;
  }
  return r;
}
void merge(int x,int y)
{
  int fx,fy;
  fx=find(x);
  fy=find(y);
  if(fx!=fy)
  bin[fx]=fy;
}
int main()
{
  int n,m,i,a,b,c,d;
  while(~scanf("%d%d",&n,&m))
  {
    for(i=1;i<=n;i++)
    {
      bin[i]=i;
    }
    for(i=0;i<=m-1;i++)
    {
       scanf("%d%d",&a,&b);
       merge(a,b);
    }
    scanf("%d%d",&c,&d);
    if(find(c)==find(d))
    {
       printf("same
");
    }
    else
    {
      printf("not sure
");
    }
  }
  return 0;
}




原文地址:https://www.cnblogs.com/gavanwanggw/p/7063406.html