結果

問題 No.2202 贅沢てりたまチキン
ユーザー AsahiAsahi
提出日時 2023-02-03 21:42:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,082 ms / 2,000 ms
コード長 2,860 bytes
コンパイル時間 2,193 ms
コンパイル使用メモリ 76,084 KB
実行使用メモリ 84,264 KB
最終ジャッジ日時 2023-09-15 17:24:12
合計ジャッジ時間 19,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,668 KB
testcase_01 AC 126 ms
55,628 KB
testcase_02 AC 129 ms
55,544 KB
testcase_03 AC 132 ms
55,800 KB
testcase_04 AC 131 ms
55,836 KB
testcase_05 AC 130 ms
55,460 KB
testcase_06 AC 128 ms
55,708 KB
testcase_07 AC 129 ms
55,660 KB
testcase_08 AC 129 ms
55,412 KB
testcase_09 AC 127 ms
55,968 KB
testcase_10 AC 158 ms
57,844 KB
testcase_11 AC 126 ms
55,828 KB
testcase_12 AC 133 ms
55,812 KB
testcase_13 AC 143 ms
55,560 KB
testcase_14 AC 984 ms
84,124 KB
testcase_15 AC 979 ms
84,264 KB
testcase_16 AC 897 ms
77,924 KB
testcase_17 AC 915 ms
78,192 KB
testcase_18 AC 717 ms
70,740 KB
testcase_19 AC 746 ms
69,056 KB
testcase_20 AC 955 ms
69,408 KB
testcase_21 AC 960 ms
69,144 KB
testcase_22 AC 925 ms
64,936 KB
testcase_23 AC 863 ms
64,804 KB
testcase_24 AC 882 ms
64,740 KB
testcase_25 AC 1,082 ms
68,980 KB
testcase_26 AC 954 ms
76,492 KB
testcase_27 AC 987 ms
76,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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];
            }
        }
    }
}
0