結果

問題 No.2301 Namorientation
ユーザー emthrm
提出日時 2023-05-12 21:39:55
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 552 ms / 3,000 ms
コード長 4,307 bytes
コンパイル時間 4,021 ms
コンパイル使用メモリ 279,184 KB
実行使用メモリ 57,396 KB
最終ジャッジ日時 2024-11-28 17:40:33
合計ジャッジ時間 17,123 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0