图论中有一个基本的问题,那就是一个无向图的连通性判别问题,今天我们就来讨论这个问题,我们知道,在计算机中一张图可以有两种表示方法,一是邻接矩阵二是邻接表,其中的邻接矩阵表示方法,我们已经在课堂上介绍最小生成树问题时讨论过,今天我们就来讨论用邻接表表示的图的连通性问题。要求不能用DFS、BFS、并查集三种方法求解。
Input
本问题有多组测试数据,每组测试数据有两部分,第一部分只有一行,是两个正整数,分别表示图的节点数N(节点
编号从1到N,1<=N<=100)和图的边数E;第二部分共有E行,每行也是两个整数N1,N2(1<=N1,N2<=N),分
别表示N1和N2之间有一条边。
Output
对于每一组测试数据,输出只有一行,如果是连通图,那么输出“Yes”,否则输出“No”。
Sample Input
6 10
1 2
1 3
1 4
1 5
1 6
2 3
2 4
3 4
3 5
3 6
4 3
1 2
1 3
2 3
Sample Output
Yes
No
#include<bits/stdc++.h>
using namespace std;
const int MAXN=500005;
bool fir_in[MAXN];
int n,m,d,a1,b1,v[MAXN],u[MAXN],U[MAXN],V[MAXN],now;
char q[10];
struct tree
{
int fa,ch[2],lazy;
} t[MAXN];
int stk[MAXN],top;
void pushdown(int root)
{
if(t[root].lazy)
{
t[root].lazy=0;
swap(t[root].ch[0],t[root].ch[1]);
t[t[root].ch[0]].lazy^=1;
t[t[root].ch[1]].lazy^=1;
}
return;
}
bool isroot(int root)
{
return t[t[root].fa].ch[0]!=root&&t[t[root].fa].ch[1]!=root;
}
void rot(int root)
{
int fa=t[root].fa;
int gfa=t[fa].fa;
int t1=(root!=t[fa].ch[0]);
int t2=(fa!=t[gfa].ch[0]);
int ch=t[root].ch[1^t1];
if(!isroot(fa))t[gfa].ch[0^t2]=root;
t[ch]