結果

問題 No.2301 Namorientation
ユーザー KKT89
提出日時 2023-05-12 21:43:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 433 ms / 3,000 ms
コード長 2,019 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 226,228 KB
最終ジャッジ日時 2025-02-12 22:24:50
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> x(n),y(n);
    vector<vector<int>> g(n);
    vector<int> deg(n);
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
        g[x[i]].push_back(y[i]);
        g[y[i]].push_back(x[i]);
        deg[x[i]]++;
        deg[y[i]]++;
    }
    vector<int> uo(n, -1);
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (deg[i] == 1) {
            q.push(i);
        }
    }
    set<pair<int,int>> st;
    while (q.size()) {
        int s = q.front(); q.pop();
        for (int t:g[s]) {
            if (st.find({s, t}) == st.end()) {
                uo[s] = t;
                st.insert({s,t});
                st.insert({t,s});
                deg[t]--;
                if (deg[t] == 1 and uo[t] == -1) {
                    q.push(t);
                }
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        if (uo[i] == -1) {
            int s = i;
            while (1) {
                for (int t:g[s]) {
                    if (st.find({s, t}) == st.end()) {
                        uo[s] = t;
                        st.insert({s,t});
                        st.insert({t,s});
                        s = t;
                        break;
                    }
                }
                if (uo[s] != -1) break;
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        if (uo[x[i]] == y[i]) {
            cout << "->" << "\n";
        }
        else {
            cout << "<-" << "\n";
        }
    }
}
0