結果

問題 No.1605 Matrix Shape
ユーザー rtyurtyu
提出日時 2021-07-16 21:39:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 905 bytes
コンパイル時間 525 ms
コンパイル使用メモリ 68,060 KB
実行使用メモリ 5,840 KB
最終ジャッジ日時 2023-09-20 13:21:17
合計ジャッジ時間 3,032 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,940 KB
testcase_01 AC 3 ms
5,176 KB
testcase_02 AC 2 ms
4,988 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,884 KB
testcase_05 AC 3 ms
4,964 KB
testcase_06 AC 3 ms
4,968 KB
testcase_07 AC 2 ms
5,008 KB
testcase_08 AC 4 ms
5,140 KB
testcase_09 AC 3 ms
4,984 KB
testcase_10 AC 4 ms
4,948 KB
testcase_11 AC 3 ms
4,952 KB
testcase_12 AC 3 ms
4,888 KB
testcase_13 AC 3 ms
5,060 KB
testcase_14 AC 4 ms
4,944 KB
testcase_15 AC 3 ms
4,968 KB
testcase_16 AC 3 ms
4,884 KB
testcase_17 AC 3 ms
5,188 KB
testcase_18 AC 2 ms
4,984 KB
testcase_19 AC 50 ms
5,756 KB
testcase_20 AC 43 ms
5,264 KB
testcase_21 AC 43 ms
5,324 KB
testcase_22 AC 48 ms
5,672 KB
testcase_23 AC 48 ms
5,732 KB
testcase_24 AC 49 ms
5,840 KB
testcase_25 AC 31 ms
5,060 KB
testcase_26 AC 31 ms
4,944 KB
testcase_27 AC 31 ms
4,996 KB
testcase_28 WA -
testcase_29 AC 31 ms
4,896 KB
testcase_30 AC 46 ms
5,268 KB
testcase_31 AC 46 ms
5,332 KB
testcase_32 AC 40 ms
5,432 KB
testcase_33 AC 40 ms
5,156 KB
testcase_34 WA -
testcase_35 AC 37 ms
5,572 KB
testcase_36 AC 22 ms
5,280 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, sp = 0;
	for (int i = 1; i <= m; i++) {
		if (ds[i] == i && sz[i] > 1) {
			c++;
			sp = sz[i];
		}
		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 << sp << '\n';
	else 
		cout << 0 << '\n';
	return 0;
}
0