結果
| 問題 |
No.2202 贅沢てりたまチキン
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-03 21:42:46 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,324 ms / 2,000 ms |
| コード長 | 2,860 bytes |
| コンパイル時間 | 2,189 ms |
| コンパイル使用メモリ | 79,636 KB |
| 実行使用メモリ | 77,180 KB |
| 最終ジャッジ日時 | 2024-07-02 19:33:29 |
| 合計ジャッジ時間 | 20,443 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
import java.util.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
public class Main{
/* 入出力 */
static BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter output = new PrintWriter(System.out);
static Scanner sc = new Scanner(System.in);
/* 定数 */
static final int [] y4 = {0,1,0,-1};
static final int [] x4 = {1,0,-1,0};
static final int INF1 = Integer.MAX_VALUE;
static final long INF2 = Long.MAX_VALUE;
static final long MOD1 = (long)1e9+7;
static final long MOD2 = 998244353;
/* Main */
public static void main(String[] args) throws IOException{
int n = sc.nextInt();
int m = sc.nextInt();
UnionFind UF = new UnionFind(2*n+1);
boolean ok = true;
while(m-->0) {
int a = sc.nextInt();
int b = sc.nextInt();
int au = a;
int bu = b;
int ad = a+n;
int bd = b+n;
if(!UF.same(au,bd)) UF.unite(au,bd);
if(!UF.same(ad,bu)) UF.unite(ad,bu);
}
for(int i=1;i<=n;i++) {
if(!UF.same(i,i+n)) ok = false;
}
output.print(ok?"Yes":"No");
output.flush();
}
static class UnionFind {
int[] parent;
int[] rank;
int[] size;
public UnionFind(int n) {
this.parent = new int[n];
this.rank = new int[n];
this.size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
size[i] = 1;
}
}
public int groups() {
int count = 0;
for(int i=0;i<parent.length;i++) {
if(i == find(i)) count++;
}
return count;
}
public int size(int x){
return size[find(x)];
}
public int find(int x) {
if (x == parent[x]) {
return x;
} else {
parent[x] = find(parent[x]);
return parent[x];
}
}
public boolean same(int x, int y) {
return find(x) == find(y);
}
/*
* yRootを親とする
*/
public void unite(int x, int y) {
int xRoot = find(x);
int yRoot = find(y);
if (xRoot == yRoot) {
return;
}
if (rank[xRoot] > rank[yRoot]) {
parent[yRoot] = xRoot;
} else if (rank[xRoot] < rank[yRoot]) {
parent[xRoot] = yRoot;
} else {
parent[xRoot] = yRoot;
rank[xRoot]++;
size[yRoot] += size[xRoot];
}
}
}
}