作者:
Changersh
,
2022-07-07 14:07:31
,
所有人可见
,
阅读 7
/**
* @author Changersh
* method 并查集
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
static int[] pre = new int[1002]; // 标记数组,有没有走过
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int m = scanner.nextInt();
for (int i = 1; i <= n; i++) pre[i] = i;
for (int i = 0; i < m; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
pre[find(a)] = find(b);
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (find(i) == i) res++;
}
if (res > 1) System.out.println("NO");
else System.out.println("YES");
}
}
public static int find(int x) {
if (pre[x] != x) {
pre[x] = find(pre[x]);
}
return pre[x];
}
}