結果

問題 No.1605 Matrix Shape
ユーザー rtyurtyu
提出日時 2021-07-16 21:44:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 938 bytes
コンパイル時間 571 ms
コンパイル使用メモリ 68,668 KB
実行使用メモリ 5,916 KB
最終ジャッジ日時 2023-09-20 13:29:19
合計ジャッジ時間 3,064 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,996 KB
testcase_01 AC 3 ms
4,984 KB
testcase_02 AC 2 ms
5,148 KB
testcase_03 AC 3 ms
4,912 KB
testcase_04 AC 2 ms
5,084 KB
testcase_05 AC 4 ms
4,912 KB
testcase_06 AC 4 ms
4,936 KB
testcase_07 AC 2 ms
5,028 KB
testcase_08 AC 3 ms
4,860 KB
testcase_09 AC 3 ms
5,032 KB
testcase_10 AC 3 ms
4,968 KB
testcase_11 AC 3 ms
4,840 KB
testcase_12 AC 3 ms
4,984 KB
testcase_13 AC 3 ms
5,160 KB
testcase_14 AC 3 ms
4,912 KB
testcase_15 AC 4 ms
4,980 KB
testcase_16 AC 3 ms
4,928 KB
testcase_17 AC 3 ms
4,972 KB
testcase_18 AC 3 ms
4,920 KB
testcase_19 AC 52 ms
5,836 KB
testcase_20 AC 44 ms
5,352 KB
testcase_21 AC 44 ms
5,428 KB
testcase_22 AC 51 ms
5,836 KB
testcase_23 AC 50 ms
5,840 KB
testcase_24 AC 52 ms
5,916 KB
testcase_25 AC 31 ms
4,992 KB
testcase_26 AC 31 ms
4,940 KB
testcase_27 AC 31 ms
4,936 KB
testcase_28 AC 31 ms
5,072 KB
testcase_29 AC 31 ms
4,996 KB
testcase_30 AC 46 ms
5,392 KB
testcase_31 AC 48 ms
5,432 KB
testcase_32 AC 41 ms
5,140 KB
testcase_33 AC 40 ms
5,256 KB
testcase_34 AC 35 ms
5,092 KB
testcase_35 AC 38 ms
5,900 KB
testcase_36 AC 22 ms
5,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int dg[200009], ds[200009], sz[200009];
bool f[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;
		f[a] = f[b] = true;
		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 && f[i]) {
			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