結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,444 KB
testcase_01 AC 4 ms
7,624 KB
testcase_02 AC 4 ms
7,668 KB
testcase_03 AC 4 ms
7,624 KB
testcase_04 AC 4 ms
7,504 KB
testcase_05 AC 4 ms
7,516 KB
testcase_06 AC 4 ms
7,684 KB
testcase_07 AC 3 ms
7,496 KB
testcase_08 AC 3 ms
7,616 KB
testcase_09 AC 4 ms
7,636 KB
testcase_10 AC 4 ms
7,628 KB
testcase_11 AC 3 ms
7,524 KB
testcase_12 AC 3 ms
7,436 KB
testcase_13 AC 5 ms
7,668 KB
testcase_14 AC 3 ms
7,548 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 4 ms
7,548 KB
testcase_18 AC 4 ms
7,524 KB
testcase_19 AC 265 ms
21,372 KB
testcase_20 AC 201 ms
18,480 KB
testcase_21 AC 199 ms
19,012 KB
testcase_22 AC 244 ms
21,336 KB
testcase_23 AC 262 ms
21,564 KB
testcase_24 AC 259 ms
21,760 KB
testcase_25 AC 144 ms
16,096 KB
testcase_26 AC 145 ms
16,136 KB
testcase_27 AC 146 ms
17,092 KB
testcase_28 AC 147 ms
16,748 KB
testcase_29 AC 144 ms
16,492 KB
testcase_30 AC 215 ms
18,780 KB
testcase_31 AC 210 ms
18,520 KB
testcase_32 AC 189 ms
17,380 KB
testcase_33 AC 192 ms
17,544 KB
testcase_34 AC 148 ms
17,752 KB
testcase_35 WA -
testcase_36 AC 102 ms
14,764 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(200020);
int h[200020], w[200020];
bool bo[200020] = { false }, bo1 = false;
int co[200020] = {};
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