結果

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
#include <string>
#include <cmath>
#include <complex>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

using P = pair<ll, int>;

struct edge {
    int to, idx;
};

int main()
{
    int n;
    cin >> n;
    vi<int> a(n), b(n);    
    vii<edge> to(n);
    vi<int> deg(n);
    rep(i, n) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
        to[a[i]].push_back({ b[i], i });
        to[b[i]].push_back({ a[i], i });
        deg[a[i]]++, deg[b[i]]++;
    }

    queue<int> q;
    rep(i, n) {
        if (deg[i] == 1) q.push(i);
    }

    vi<int> ans(n);
    while (!q.empty()) {
        int v = q.front();
        q.pop();

        for (edge nv : to[v]) {
            if (ans[nv.idx]) continue;
            if (v > nv.to) ans[nv.idx] = 1;
            else ans[nv.idx] = -1;
            deg[nv.to]--;
            if (deg[nv.to] == 1) q.push(nv.to);
            break;
        }
    }

    
    rep(i, n) {
        q.push(i);
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (edge nv : to[v]) {
                if (ans[nv.idx] != 0) continue;
                if (v > nv.to) ans[nv.idx] = 1;
                else ans[nv.idx] = -1;
                q.push(nv.to);
                break;
            }
        }
    }

    rep(i, n) cout << (ans[i] == 1 ? "<-" : "->") << endl;

    return 0;
}

/*
5
4 5
1 5
2 3
1 2
1 5

*/
0