用并查集做
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int par[105] = { 0 };
int rank0[105] = { 0 };
int root[105] = { 0 };
void init(int n) {
for (int i = 1; i <= n; i++)
{
par[i] = i;
rank0[i] = 0;
}
}
int find(int x) {
if (par[x]==x)
{
return x;
}
else
{
return par[x] = find(par[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x==y)
{
return;
}
if (rank0[x]<rank0[y])
{
par[x] = y;
}
else
{
par[y] = x;
if (rank0[x]==rank0[y])
{
rank0[x]++;
}
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int main() {
int n, m;
cin >> n >> m;
init(n);
int x, y;
for (int i = 0; i < m; i++)
{
cin >> x >> y;
unite(x, y);
}
for (int i = 1; i <= n; i++)
{
root[find(i)] = 1;
}
int num=0;
for (int i = 1; i <=n; i++)
{
if (root[i]==1)
{
num++;
}
}
num == 1 ? cout << "Yes" << endl : cout << "No" << endl;
return 0;
}