题解:并查集判断是否连通,然后判断是否为欧拉路,是不是所有的结点度数都是偶数,注意R == 0的时候输出NOT
#include <stdio.h>
#include <string.h>
const int N = 200 + 5;
int du[N], flag, n, r, pa[N], vis[N];
void init() {
memset(du, 0, sizeof(du));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
pa[i] = i;
flag = 0;
}
int get_parent(int x) {
return pa[x] == x ? x : get_parent(pa[x]);
}
void Union(int x, int y) {
vis[x] = vis[y] = 1;
int px = get_parent(x);
int py = get_parent(y);
if (px != py)
pa[px] = pa[py];
}
void judge() {
int num = 0;
for (int i = 0; i < n; i++)
if (vis[i] && pa[i] == i)
num++;
if (num > 1)
flag = 1;
}
void judge1() {
for (int i = 0; i < n; i++)
if (vis[i] && du[i] % 2 != 0) {
flag = 1;
break;
}
}
int main() {
int v1, v2;
while (scanf("%d %d", &n, &r) != EOF) {
if (r == 0) {
printf("Not Possible\n");
continue;
}
init();
while (r--) {
scanf("%d%d", &v1, &v2);
du[v1]++;
du[v2]++;
Union(v1, v2);
}
judge();
if (flag)
printf("Not Possible\n");
else {
judge1();
if (flag)
printf("Not Possible\n");
else
printf("Possible\n");
}
}
return 0;
}