結果

問題 No.1605 Matrix Shape
ユーザー tnakao0123tnakao0123
提出日時 2021-07-20 15:53:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 1,936 bytes
コンパイル時間 961 ms
コンパイル使用メモリ 100,124 KB
実行使用メモリ 19,520 KB
最終ジャッジ日時 2023-09-24 12:48:38
合計ジャッジ時間 4,756 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,704 KB
testcase_01 AC 3 ms
9,932 KB
testcase_02 AC 3 ms
9,584 KB
testcase_03 AC 3 ms
9,736 KB
testcase_04 AC 3 ms
9,564 KB
testcase_05 AC 3 ms
9,660 KB
testcase_06 AC 3 ms
9,732 KB
testcase_07 AC 3 ms
9,564 KB
testcase_08 AC 3 ms
9,628 KB
testcase_09 AC 3 ms
9,736 KB
testcase_10 AC 3 ms
9,764 KB
testcase_11 AC 3 ms
9,596 KB
testcase_12 AC 3 ms
9,860 KB
testcase_13 AC 3 ms
9,732 KB
testcase_14 AC 3 ms
9,664 KB
testcase_15 AC 3 ms
9,732 KB
testcase_16 AC 3 ms
9,796 KB
testcase_17 AC 3 ms
9,576 KB
testcase_18 AC 3 ms
9,664 KB
testcase_19 AC 187 ms
19,288 KB
testcase_20 AC 149 ms
15,400 KB
testcase_21 AC 143 ms
15,392 KB
testcase_22 AC 170 ms
19,128 KB
testcase_23 AC 182 ms
19,520 KB
testcase_24 AC 172 ms
19,396 KB
testcase_25 AC 75 ms
12,548 KB
testcase_26 AC 78 ms
12,420 KB
testcase_27 AC 75 ms
12,372 KB
testcase_28 AC 79 ms
12,308 KB
testcase_29 AC 78 ms
12,372 KB
testcase_30 AC 148 ms
15,588 KB
testcase_31 AC 154 ms
15,560 KB
testcase_32 AC 134 ms
14,040 KB
testcase_33 AC 127 ms
13,812 KB
testcase_34 AC 48 ms
12,136 KB
testcase_35 AC 123 ms
17,264 KB
testcase_36 AC 73 ms
14,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1605.cc:  No.1605 Matrix Shape - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_M = 200000;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;

/* global variables */

pii ps[MAX_N];
vi nbrs[MAX_M];
int xs[MAX_N * 2], ies[MAX_M], oes[MAX_M];
bool used[MAX_M];

/* subroutines */

int bfd(int st) {
  used[st] = true;
  int c = 1;

  qi q;
  q.push(st);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    for (auto v: nbrs[u])
      if (! used[v]) {
	used[v] = true;
	c++;
	q.push(v);
      }
  }

  return c;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  int m = 0;
  for (int i = 0; i < n; i++) {
    int h, w;
    scanf("%d%d", &h, &w);
    h--, w--;
    ps[i] = pii(h, w);
    xs[m++] = h, xs[m++] = w;
  }
  sort(xs, xs + m);
  m = unique(xs, xs + m) - xs;
  //printf("m=%d\n", m);

  for (int i = 0; i < n; i++) {
    int hi = lower_bound(xs, xs + m, ps[i].first) - xs;
    int wi = lower_bound(xs, xs + m, ps[i].second) - xs;
    ps[i] = pii(hi, wi);
    ies[wi]++, oes[hi]++;
    nbrs[hi].push_back(wi);
  }

  int stc = 0, glc = 0, sti = -1, gli = -1;;
  for (int i = 0; i < m; i++) {
    if (ies[i] + 1 == oes[i]) stc++, sti = i;
    else if (oes[i] + 1 == ies[i]) glc++, gli = i;
    else if (ies[i] != oes[i]) {
      puts("0"); return 0;
    }
  }

  if (stc > 1 || glc > 1 || stc != glc) { puts("0"); return 0; }

  int c = bfd((stc > 0) ? sti : 0);
  //printf("c=%d, m=%d\n", c, m);
  if (c < m) { puts("0"); return 0; }

  printf("%d\n", (stc > 0) ? 1 : m);
  return 0;
}
0