整理的算法模板合集: ACM模板
判断一个图是否为树
1. 无向图(hdu 1272 小希的迷宫)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 200007, M = 5000007, INF = 0x3f3f3f3f;
int fa[N], n, m;
bool vis[N];
int Find(int x)
{
if(fa[x] == x)return x;
return fa[x] = Find(fa[x]);
}
bool unions(int x, int y)
{
int fx = Find(x);
int fy = Find(y);
if(fx != fy){
fa[fy] = fx;//注意合并的是原来的点
return true