結果

問題 No.1605 Matrix Shape
ユーザー 夜
提出日時 2021-07-20 21:02:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,707 bytes
コンパイル時間 1,093 ms
コンパイル使用メモリ 107,496 KB
実行使用メモリ 26,856 KB
最終ジャッジ日時 2023-09-24 12:55:32
合計ジャッジ時間 6,377 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,192 KB
testcase_01 AC 5 ms
12,540 KB
testcase_02 AC 5 ms
12,592 KB
testcase_03 AC 6 ms
12,496 KB
testcase_04 AC 5 ms
12,336 KB
testcase_05 AC 6 ms
12,360 KB
testcase_06 AC 5 ms
12,596 KB
testcase_07 AC 5 ms
12,236 KB
testcase_08 AC 6 ms
12,460 KB
testcase_09 AC 6 ms
12,240 KB
testcase_10 AC 5 ms
12,396 KB
testcase_11 AC 6 ms
12,192 KB
testcase_12 AC 5 ms
12,284 KB
testcase_13 AC 5 ms
12,556 KB
testcase_14 AC 5 ms
12,180 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 6 ms
12,236 KB
testcase_18 AC 6 ms
12,360 KB
testcase_19 AC 254 ms
26,332 KB
testcase_20 AC 203 ms
23,416 KB
testcase_21 AC 202 ms
22,556 KB
testcase_22 AC 247 ms
26,688 KB
testcase_23 AC 259 ms
26,856 KB
testcase_24 AC 255 ms
26,016 KB
testcase_25 AC 146 ms
20,848 KB
testcase_26 AC 147 ms
20,884 KB
testcase_27 AC 148 ms
22,216 KB
testcase_28 AC 147 ms
21,688 KB
testcase_29 AC 147 ms
21,692 KB
testcase_30 AC 207 ms
23,324 KB
testcase_31 AC 208 ms
23,188 KB
testcase_32 AC 190 ms
22,472 KB
testcase_33 AC 190 ms
22,736 KB
testcase_34 AC 151 ms
22,588 KB
testcase_35 WA -
testcase_36 AC 105 ms
19,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
vector<tuple<int, int, int>> v;
vector<vector<int>> vec(400040);
int h[200020], w[200020];
bool bo[400040] = { false }, bo1 = false;
int co[400040] = {};
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> h[i] >> w[i];
		v.emplace_back(make_tuple(h[i], 0, i));
		v.emplace_back(make_tuple(w[i], 1, i));
	}
	sort(v.begin(), v.end());
	int now = 0;
	for (int i = 0; i < v.size(); i++) {
		if (i != 0 && get<0>(v[i]) != get<0>(v[i - 1])) now++;
		if (!get<1>(v[i])) {
			h[get<2>(v[i])] = now;
		}
		else {
			w[get<2>(v[i])] = now;
		}
	}
	for (int i = 0; i < n; i++) {
		co[h[i]]++;
		co[w[i]]--;
		vec[h[i]].emplace_back(w[i]);
		vec[w[i]].emplace_back(h[i]);
	}
	for (int i = 0; i <= now; i++) {
		if (!bo[i]) {
			if (bo1) {
				cout << 0 << endl;
				return 0;
			}
			bo1 = true;
			queue<int> que;
			bo[i] = true;
			que.push(i);
			while (!que.empty()) {
				int m = que.front();
				que.pop();
				for (int j = 0; j < vec[m].size(); j++) {
					if (!bo[vec[m][j]]) {
						bo[vec[m][j]] = true;
						que.push(vec[m][j]);
					}
				}
			}
		}
	}
	int a = 0, b = 0, c = 0, d = 0;
	for (int i = 0; i <= now; i++) {
		if (co[i] == 0) {
			a++;
		}
		else if (co[i] == 1) {
			b++;
		}
		else if (co[i] == -1) {
			c++;
		}
		else {
			d++;
		}
	}
	if (d != 0) {
		cout << 0 << endl;
	}
	else {
		if (b == 1 && c == 1) {
			cout << 1 << endl;
		}
		else {
			cout << now + 1 << endl;
		}
	}
}
0