結果

問題 No.1605 Matrix Shape
ユーザー rtyurtyu
提出日時 2021-07-16 21:37:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 875 bytes
コンパイル時間 486 ms
コンパイル使用メモリ 67,664 KB
実行使用メモリ 5,904 KB
最終ジャッジ日時 2023-09-20 13:18:25
合計ジャッジ時間 2,904 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,236 KB
testcase_01 AC 4 ms
4,964 KB
testcase_02 AC 3 ms
4,964 KB
testcase_03 WA -
testcase_04 AC 3 ms
5,108 KB
testcase_05 WA -
testcase_06 AC 4 ms
4,908 KB
testcase_07 AC 3 ms
4,984 KB
testcase_08 WA -
testcase_09 AC 3 ms
4,976 KB
testcase_10 AC 4 ms
5,112 KB
testcase_11 AC 3 ms
4,908 KB
testcase_12 WA -
testcase_13 AC 3 ms
5,048 KB
testcase_14 AC 4 ms
5,116 KB
testcase_15 AC 4 ms
5,180 KB
testcase_16 AC 3 ms
4,956 KB
testcase_17 AC 3 ms
4,908 KB
testcase_18 AC 2 ms
4,984 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 43 ms
5,368 KB
testcase_22 AC 51 ms
5,704 KB
testcase_23 WA -
testcase_24 AC 50 ms
5,748 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 31 ms
5,044 KB
testcase_28 WA -
testcase_29 AC 31 ms
4,992 KB
testcase_30 WA -
testcase_31 AC 46 ms
5,340 KB
testcase_32 WA -
testcase_33 AC 40 ms
5,136 KB
testcase_34 WA -
testcase_35 AC 37 ms
5,628 KB
testcase_36 AC 22 ms
5,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;

int dg[200009], ds[200009], sz[200009];
const int m = 200000;

int uf(int n)
{
	if (ds[n] == n) return n;
	return ds[n] = uf(ds[n]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= m; i++) {
		ds[i] = i;
		sz[i] = 1;
	}
	for (int i = 1; i <= n; i++) {
		int a, b; cin >> a >> b;
		dg[a]++; dg[b]--;
		int ua = uf(a), ub = uf(b);
		if (ua != ub) {
			ds[ub] = ua;
			sz[ua] += sz[ub];
		}
	}
	int c = 0, s0 = 0, s1 = 0;
	for (int i = 1; i <= m; i++) {
		if (ds[i] == i && sz[i] > 1)
			c++;
		if (dg[i] == -1) s0++;
		if (dg[i] == 1) s1++;
		if (dg[i] < -1 || dg[i] > 1 || c > 1) {
			cout << 0 << '\n';
			return 0;
		}
	}
	if (s0 == 1 && s1 == 1)
		cout << 1 << '\n';
	else if (s0 == 0 && s1 == 0)
		cout << 2 << '\n';
	else 
		cout << 0 << '\n';
	return 0;
}
0