結果

問題 No.2301 Namorientation
ユーザー 👑 emthrmemthrm
提出日時 2023-05-12 21:39:55
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 490 ms / 3,000 ms
コード長 4,307 bytes
コンパイル時間 3,218 ms
コンパイル使用メモリ 277,708 KB
実行使用メモリ 57,108 KB
最終ジャッジ日時 2023-08-19 04:12:55
合計ジャッジ時間 16,827 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 237 ms
31,472 KB
testcase_13 AC 211 ms
29,212 KB
testcase_14 AC 430 ms
52,060 KB
testcase_15 AC 256 ms
34,640 KB
testcase_16 AC 404 ms
44,376 KB
testcase_17 AC 238 ms
33,144 KB
testcase_18 AC 382 ms
44,616 KB
testcase_19 AC 182 ms
26,780 KB
testcase_20 AC 381 ms
45,752 KB
testcase_21 AC 263 ms
33,836 KB
testcase_22 AC 201 ms
57,108 KB
testcase_23 AC 196 ms
56,136 KB
testcase_24 AC 161 ms
47,544 KB
testcase_25 AC 169 ms
44,616 KB
testcase_26 AC 233 ms
56,652 KB
testcase_27 AC 490 ms
49,868 KB
testcase_28 AC 437 ms
50,416 KB
testcase_29 AC 459 ms
50,640 KB
testcase_30 AC 464 ms
50,032 KB
testcase_31 AC 474 ms
49,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int 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;

struct UnicyclicGraph {
  std::vector<bool> is_in_loop;
  std::vector<int> belong, mapping, loop;
  std::vector<std::vector<int>> invs;
  std::vector<std::vector<std::vector<int>>> forest;

  explicit UnicyclicGraph(const int n)
      : is_in_loop(n, false), belong(n, -1), mapping(n, -1), n(n), graph(n) {}

  void add_edge(const int src, const int dst) {
    const int id = srcs.size();
    srcs.emplace_back(src);
    dsts.emplace_back(dst);
    graph[src].emplace_back(id);
    if (dst != src) [[likely]] graph[dst].emplace_back(id);
  }

  void build() {
    dfs(-1, 0);
    std::queue<int> que;
    for (const int root : loop) {
      const int forest_id = forest.size();
      belong[root] = forest_id;
      mapping[root] = 0;
      std::vector<int> inv{root};
      std::vector<std::vector<int>> tree(1);
      que.emplace(root);
      while (!que.empty()) {
        const int ver = que.front();
        que.pop();
        for (const int id : graph[ver]) {
          const int dst = destination(id, ver);
          if (belong[dst] == -1 && !is_in_loop[dst]) {
            const int idx = tree.size();
            belong[dst] = forest_id;
            mapping[dst] = idx;
            inv.emplace_back(dst);
            tree[mapping[ver]].emplace_back(idx);
            tree.emplace_back(std::vector<int>{mapping[ver]});
            que.emplace(dst);
          }
        }
      }
      if (inv.size() == 1) {
        belong[root] = mapping[root] = -1;
      } else {
        invs.emplace_back(inv);
        forest.emplace_back(tree);
      }
    }
  }

 private:
  const int n;
  std::vector<int> srcs, dsts;
  std::vector<std::vector<int>> graph;

  int destination(const int id, const int s) const {
    return (srcs[id] == s ? dsts : srcs)[id];
  }

  bool dfs(const int prev_id, const int ver) {
    is_in_loop[ver] = true;
    loop.emplace_back(ver);
    for (const int id : graph[ver]) {
      if (id == prev_id) continue;
      const int dst = destination(id, ver);
      if (is_in_loop[dst]) {
        for (int i = loop.size() - 1; i >= 0; --i) {
          if (loop[i] == dst) {
            for (int j = 0; j < i; ++j) {
              is_in_loop[loop[j]] = false;
            }
            loop.erase(loop.begin(), std::next(loop.begin(), i));
            return true;
          }
        }
        assert(false);
      }
      if (dfs(id, dst)) return true;
    }
    loop.pop_back();
    is_in_loop[ver] = false;
    return false;
  }
};

int main() {
  int n; cin >> n;
  UnicyclicGraph yuruyuri(n);
  map<pair<int, int>, int> mp;
  REP(id, n) {
    int a, b; cin >> a >> b; --a; --b;
    yuruyuri.add_edge(a, b);
    mp[{a, b}] = id;
  }
  yuruyuri.build();
  vector<pair<int, int>> edges;
  edges.reserve(n);
  const int m = yuruyuri.invs.size();
  REP(tr, m) {
    const auto dfs = [&](auto dfs, const int par, const int ver) -> void {
      for (const int e : yuruyuri.forest[tr][ver]) {
        if (e != par) {
          edges.emplace_back(yuruyuri.invs[tr][e], yuruyuri.invs[tr][ver]);
          dfs(dfs, ver, e);
        }
      }
    };
    dfs(dfs, -1, 0);
  }
  const int lp = yuruyuri.loop.size();
  REP(i, lp) edges.emplace_back(yuruyuri.loop[i], yuruyuri.loop[(i + 1) % lp]);
  vector<string> ans(n);
  for (const auto& [u, v] : edges) {
    if (mp.contains({u, v})) {
      ans[mp[{u, v}]] = "->";
    } else {
      ans[mp[{v, u}]] = "<-";
    }
  }
  REP(i, n) cout << ans[i] << '\n';
  return 0;
}
0