# 交朋友## 题目描述朋友关系具有传递性:如果 $a$ 和 $b$ 是朋友、$b$ 和 $c$ 是朋友那么 $a$ 和 $c$ 也是朋友。$n$ 个人一开始两两都不是朋友接下来的时间里他们中的一些人会不断成为朋友。两个人一旦成为朋友他们就永远是朋友了。现在有 $m$ 个操作或询问每个操作或询问用 $tij$ 表示:- $t = 0$表示 $i$ 和 $j$ 成为了朋友。- $t = 1$表示询问
#include <iostream>
using namespace std;
const int N = 1010;
int p[N];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) p[i] = i;
while (m -- )
{
int t, a, b;
cin >> t >> a >> b;
if (t == 0) p[find(a)] = find(b);
else
{
if (find(a) == find(b)) puts("1");
else puts("0");
}
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/cZtX 著作权归作者所有。请勿转载和采集!