結果

問題 No.2202 贅沢てりたまチキン
ユーザー laneguelanegue
提出日時 2023-02-03 23:29:14
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 406 ms / 2,000 ms
コード長 1,571 bytes
コンパイル時間 2,034 ms
コンパイル使用メモリ 84,792 KB
実行使用メモリ 32,884 KB
最終ジャッジ日時 2023-09-04 19:45:53
合計ジャッジ時間 6,190 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 3 ms
10,540 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 277 ms
29,572 KB
testcase_15 AC 279 ms
30,392 KB
testcase_16 AC 275 ms
31,664 KB
testcase_17 AC 279 ms
31,400 KB
testcase_18 AC 137 ms
16,088 KB
testcase_19 AC 148 ms
25,252 KB
testcase_20 AC 340 ms
32,712 KB
testcase_21 AC 337 ms
32,884 KB
testcase_22 AC 258 ms
20,976 KB
testcase_23 AC 290 ms
21,784 KB
testcase_24 AC 296 ms
21,568 KB
testcase_25 AC 406 ms
29,072 KB
testcase_26 AC 308 ms
29,864 KB
testcase_27 AC 270 ms
29,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.container.dlist;

class DisjointSet{
	int[] parent;
	this(int size){
		parent = new int[size];
		parent[] = -1;
	}
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if(x == y) return;
		if(parent[x] > parent[y]){
			auto t = x;
			x = y;
			y = t;
		}
		parent[x] += parent[y];
		parent[y] = x;
	}
	bool same(int x, int y){
		return root(x) == root(y);
	}
	int root(int x){
		if(parent[x] < 0) return x;
		parent[x] = root(parent[x]);
		return parent[x];
	}
	int size(int x){
		return -parent[root(x)];
	}
}
int N;
int M;
int[][] E;
bool[] used;
void main(){
  auto s = readln.chomp.split;
  N = s[0].to!int;
  M = s[1].to!int;
  E = new int[][](N * 2);
  used = new bool[N * 2];
  auto ds = new DisjointSet(N);
  for(auto m = 0; m < M; m++){
    s = readln.chomp.split;
    auto a = s[0].to!int - 1;
    auto b = s[1].to!int - 1;
    E[a] ~= b + N;
    E[a + N] ~= b;
    E[b] ~= a + N;
    E[b + N] ~= a;
    ds.unite(a, b);
  }
  auto result = true;
  bool[int] results;
  for(auto n = 0; n < N; n++){
    if(ds.root(n) in results) continue;
    auto r = bfs(n);
    if(!r){
      result = false;
      break;
    }
    results[ds.root(n)] = true;
  }
  writeln(result ? "Yes" : "No");
}

bool bfs(int n){
  auto q = DList!(int)(n);
  while(!q.empty){
    auto t = q.front;
    q.removeFront;
    if(t == n + N) return true;
    if(used[t]) continue;
    used[t] = true;
    foreach(c; E[t]){
      if(used[c]) continue;
      q.insertBack(c);
    }
  }
  return false;
}

0