并查集;
/* 下面的问题已经找到原因:dev c++使用c++编译器,变量end的命名可能出现冲突,使用gcc编译不会出现报错*/
遇到了一个诡异的问题:全局变量放在main外报错。
# include <stdio.h>
# define MAXN 105
int father[MAXN];
int main()
{
int T, sta, end, n, m, x, y;
int i;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &sta,&end,&n);
for (i = 0; i < MAXN; ++i)
father[i] = i;
while (n--)
{
scanf("%d%d", &m, &x);
while (x != father[x]) x = father[x];
for (i = 2; i <= m; ++i)
{
scanf("%d", &y);
while (father[y] != y) y = father[y];
if (y != x) father[y] = x;
}
}
while (sta != father[sta]) sta = father[sta];
while (end != father[end]) end = father[end];
printf(sta==end ? "Yes\n":"No\n");
}
return 0;
}