#define _CRT_NONSTDC_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #define ENABLE_DUMP //#define ENABLE_PERF #include #include #include #include //#include //#include //#include //using u128 = boost::multiprecision::uint128_t; #ifdef _MSC_VER #include #include #include #include #include #include #include int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; } int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; } namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } } #else #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #endif /** compro_io **/ #ifdef ATCODER_MODINT_HPP using mint = atcoder::modint998244353; std::ostream& operator<<(std::ostream& os, const mint& m) { os << m.val(); return os; } std::istream& operator>>(std::istream& is, mint& m) { uint64_t x; is >> x; m = x; return is; } #endif namespace aux { template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v) << ", "; tp::output(os, v); } }; template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v); } }; } template std::ostream& operator<<(std::ostream& os, const std::tuple& t) { os << '['; aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); return os << ']'; } // tuple out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x); // container out (fwd decl) template std::ostream& operator<<(std::ostream& os, const std::pair& p) { return os << "[" << p.first << ", " << p.second << "]"; } // pair out template std::istream& operator>>(std::istream& is, std::pair& p) { return is >> p.first >> p.second; } // pair in std::ostream& operator<<(std::ostream& os, const std::vector::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out std::ostream& operator<<(std::ostream& os, const std::vector& v) { bool f = true; os << "["; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << "]"; return os; } // vector out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x) { bool f = true; os << "["; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << "]"; } // container out template())), class = typename std::enable_if::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in template auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out /** io setup **/ struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } } iosetup(true); // set false when solving interective problems /** string formatter **/ template std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); } /** dump **/ #ifdef ENABLE_DUMP #define DUMPOUT std::cerr std::ostringstream DUMPBUF; #define dump(...) do{DUMPBUF<<" ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__PRETTY_FUNCTION__<<"]"< void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); } #else #define dump(...) void(0); #endif /* timer */ class Timer { double t = 0, paused = 0, tmp; public: Timer() { reset(); } static double time() { #ifdef _MSC_VER return __rdtsc() / 3.0e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 3.0e9; #endif } void reset() { t = time(); } void pause() { tmp = time(); } void restart() { paused += time() - tmp; } double elapsed_ms() const { return (time() - t - paused) * 1000.0; } } timer; /** perf counter **/ #if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__) #define __PRETTY_FUNCTION__ __FUNCSIG__ #endif struct PerfCounter { std::string name; Timer timer; PerfCounter(const std::string& name_) : name(name_) {} ~PerfCounter() { #ifdef ENABLE_PERF std::cerr << format("[PerfCounter] %6d ms <%s>.\n", (int)timer.elapsed_ms(), name.c_str()); #endif } }; /** rand **/ struct Xorshift { static constexpr uint64_t M = INT_MAX; static constexpr double e = 1.0 / M; uint64_t x = 88172645463325252LL; Xorshift() {} Xorshift(uint64_t seed) { reseed(seed); } inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next(); } inline uint64_t next() { x = x ^ (x << 7); return x = x ^ (x >> 9); } inline int next_int() { return next() & M; } inline int next_int(int mod) { return next() % mod; } inline int next_int(int l, int r) { return l + next_int(r - l + 1); } inline double next_double() { return next_int() * e; } }; /** shuffle **/ template void shuffle_vector(std::vector& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } } /** split **/ std::vector split(std::string str, const std::string& delim) { for (char& c : str) if (delim.find(c) != std::string::npos) c = ' '; std::istringstream iss(str); std::vector parsed; std::string buf; while (iss >> buf) parsed.push_back(buf); return parsed; } /** misc **/ template inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array template auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector(arg, x); else return std::vector(arg, make_vector(x, args...)); } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } /** using **/ using ll = long long; using ull = unsigned long long; using ld = double; //using ld = boost::multiprecision::cpp_bin_float_quad; using pii = std::pair; using pll = std::pair; template using PQ = std::priority_queue; template using MPQ = std::priority_queue, std::greater>; using namespace std; template void print_vector(const std::vector& v) { for (int i = 0; i < v.size(); i++) { std::cout << (i ? " " : "") << v[i]; } std::cout << '\n'; } /** * @brief Graph Template(グラフテンプレート) */ template< typename T = int > struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } string stringify() const { return format("Edge [from=%d, to=%d, cost=%lld, idx=%d]", from, to, cost, idx); } }; template< typename T = int > struct Graph { vector< vector< Edge< T > > > g; int es; Graph() = default; explicit Graph(int n) : g(n), es(0) {} size_t size() const { return g.size(); } void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); } void add_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void read(int M, int padding = -1, bool weighted = false, bool directed = false) { for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a += padding; b += padding; T c = T(1); if (weighted) cin >> c; if (directed) add_directed_edge(a, b, c); else add_edge(a, b, c); } } inline vector< Edge< T > >& operator[](const int& k) { return g[k]; } inline const vector< Edge< T > >& operator[](const int& k) const { return g[k]; } }; template< typename T = int > using Edges = vector< Edge< T > >; /** * @brief Namori Graph * @docs docs/namori-graph.md */ template< typename T = int > struct NamoriGraph : Graph< T > { public: using Graph< T >::Graph; using Graph< T >::g; vector< Graph< T > > forest; Edges< T > loop_edges; struct Info { int tree_id, id; }; Info operator[](const int& k) const { return Info{ mark_id[k], id[k] }; } int inv(int tree_id, int k) { return iv[tree_id][k]; } void build() { int n = (int)g.size(); vector< int > deg(n), used(n); queue< int > que; for (int i = 0; i < n; i++) { deg[i] = (int)g[i].size(); if (deg[i] == 1) { que.emplace(i); used[i] = true; } } while (not que.empty()) { int idx = que.front(); que.pop(); for (auto& e : g[idx]) { if (used[e.to]) { continue; } --deg[e.to]; if (deg[e.to] == 1) { que.emplace(e.to); used[e.to] = true; } } } int mx = 0; for (auto& edges : g) { for (auto& e : edges) mx = max(mx, e.idx); } vector< int > edge_used(mx + 1); vector< int > loop; for (int v = 0; v < n; v++) { if (!used[v]) { for (bool update = true; update;) { update = false; loop.emplace_back(v); for (auto& e : g[v]) { if (used[e.to] or edge_used[e.idx]) { continue; } edge_used[e.idx] = true; loop_edges.emplace_back(v, e.to, e.cost, e.idx); v = e.to; update = true; break; } } break; } } loop.pop_back(); mark_id.resize(n); id.resize(n); for (int i = 0; i < (int)loop.size(); i++) { int pre = loop[(i + loop.size() - 1) % loop.size()]; int nxt = loop[(i + 1) % loop.size()]; int sz = 0; mark_id[loop[i]] = i; iv.emplace_back(); id[loop[i]] = sz++; iv.back().emplace_back(loop[i]); for (auto& e : g[loop[i]]) { if (e.to != pre and e.to != nxt) { mark_dfs(e.to, loop[i], i, sz); } } Graph< T > tree(sz); for (auto& e : g[loop[i]]) { if (e.to != pre and e.to != nxt) { tree.g[id[loop[i]]].emplace_back(id[loop[i]], id[e.to], e.cost, e.idx); tree.g[id[e.to]].emplace_back(id[e.to], id[loop[i]], e.cost, e.idx); build_dfs(e.to, loop[i], tree); } } forest.emplace_back(tree); } } private: vector< vector< int > > iv; vector< int > mark_id, id; void mark_dfs(int idx, int par, int k, int& l) { mark_id[idx] = k; id[idx] = l++; iv.back().emplace_back(idx); for (auto& e : g[idx]) { if (e.to != par) { mark_dfs(e.to, idx, k, l); } } } void build_dfs(int idx, int par, Graph< T >& tree) { for (auto& e : g[idx]) { if (e.to != par) { tree.g[id[idx]].emplace_back(id[idx], id[e.to], e.cost, e.idx); tree.g[id[e.to]].emplace_back(id[e.to], id[idx], e.cost, e.idx); build_dfs(e.to, idx, tree); } } } }; void solve() { int N; cin >> N; NamoriGraph namori(N); vector> G(N); vector edges; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; a--; b--; edges.emplace_back(a, b); namori.add_edge(a, b); G[a].push_back(b); G[b].push_back(a); } namori.build(); int fsize = namori.forest.size(); vector cycle; vector used(N); for (int i = 0; i < fsize; i++) { cycle.push_back(namori.inv(i, 0)); used[cycle.back()] = true; } set directions; for (int i = 0; i < fsize; i++) { directions.emplace(cycle[i], cycle[(i + 1) % fsize]); } // bfs for (int s : cycle) { queue qu; qu.push(s); while (!qu.empty()) { int u = qu.front(); qu.pop(); for (int v : G[u]) if (!used[v]) { used[v] = true; qu.push(v); directions.emplace(v, u); } } } for (auto [u, v] : edges) { if (directions.count({ u, v })) { cout << "->" << '\n'; } else { cout << "<-" << '\n'; } } } int main() { int T = 1; //cin >> T; for (int t = 0; t < T; t++) { solve(); } return 0; }