結果

問題 No.1605 Matrix Shape
ユーザー 👑 emthrmemthrm
提出日時 2021-07-16 21:55:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 2,578 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 207,024 KB
実行使用メモリ 36,924 KB
最終ジャッジ日時 2023-09-20 13:47:29
合計ジャッジ時間 5,972 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
13,908 KB
testcase_01 AC 10 ms
13,904 KB
testcase_02 AC 9 ms
13,904 KB
testcase_03 AC 10 ms
13,856 KB
testcase_04 AC 10 ms
13,968 KB
testcase_05 AC 9 ms
13,936 KB
testcase_06 AC 10 ms
13,960 KB
testcase_07 AC 9 ms
13,888 KB
testcase_08 AC 9 ms
13,900 KB
testcase_09 AC 9 ms
13,996 KB
testcase_10 AC 9 ms
13,896 KB
testcase_11 AC 9 ms
13,868 KB
testcase_12 AC 10 ms
13,864 KB
testcase_13 AC 9 ms
13,988 KB
testcase_14 AC 9 ms
13,988 KB
testcase_15 AC 10 ms
13,892 KB
testcase_16 AC 9 ms
13,884 KB
testcase_17 AC 10 ms
13,960 KB
testcase_18 AC 9 ms
13,888 KB
testcase_19 AC 171 ms
36,924 KB
testcase_20 AC 111 ms
26,184 KB
testcase_21 AC 113 ms
26,168 KB
testcase_22 AC 137 ms
31,784 KB
testcase_23 AC 159 ms
36,784 KB
testcase_24 AC 160 ms
36,880 KB
testcase_25 AC 53 ms
26,056 KB
testcase_26 AC 54 ms
26,276 KB
testcase_27 AC 54 ms
26,304 KB
testcase_28 AC 54 ms
26,180 KB
testcase_29 AC 53 ms
26,036 KB
testcase_30 AC 109 ms
21,508 KB
testcase_31 AC 105 ms
21,440 KB
testcase_32 AC 95 ms
25,272 KB
testcase_33 AC 70 ms
17,596 KB
testcase_34 AC 54 ms
25,964 KB
testcase_35 AC 90 ms
24,096 KB
testcase_36 AC 53 ms
20,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

std::vector<int> eulerian_trail_in_directed_graph(const std::vector<std::vector<int>> &graph, int s = -1) {
  int n = graph.size(), edges = 0;
  std::vector<std::vector<int>> tmp(graph);
  std::vector<int> deg(n, 0);
  for (int i = 0; i < n; ++i) {
    deg[i] += tmp[i].size();
    for (int e : tmp[i]) --deg[e];
    edges += tmp[i].size();
  }
  int not0 = n - std::count(deg.begin(), deg.end(), 0);
  if (s == -1) {
    for (int i = 0; i < n; ++i) {
      if (not0 == 0) {
        if (!tmp[i].empty()) {
          s = i;
          break;
        }
      } else if (not0 == 2) {
        if (deg[i] == 1) {
          s = i;
          break;
        }
      }
    }
    if (s == -1) return {};
  }
  if (not0 == 0 || (not0 == 2 && deg[s] == 1)) {
    std::vector<int> res;
    std::function<void(int)> dfs = [&tmp, &res, &dfs](int ver) {
      while (!tmp[ver].empty()) {
        int nx = tmp[ver].back();
        tmp[ver].pop_back();
        dfs(nx);
      }
      res.emplace_back(ver);
    };
    dfs(s);
    if (res.size() == edges + 1) {
      std::reverse(res.begin(), res.end());
      return res;
    }
  }
  return {};
}

int main() {
  constexpr int L = 200000;
  int n; cin >> n;
  vector<vector<int>> graph(L + 1);
  int deg[L + 1]{};
  REP(i, n) {
    int h, w; cin >> h >> w;
    graph[h].emplace_back(w);
    ++deg[h];
    --deg[w];
  }
  bool has_loop = true;
  REP(i, L + 1) has_loop &= deg[i] == 0;
  if (!eulerian_trail_in_directed_graph(graph).empty()) {
    if (has_loop) {
      int ans = 0;
      REP(i, L + 1) ans += !graph[i].empty();
      cout << ans << '\n';
    } else {
      cout << 1 << '\n';
    }
  } else {
    cout << 0 << '\n';
  }
  return 0;
}
0