結果

問題 No.2301 Namorientation
ユーザー milanis48663220milanis48663220
提出日時 2023-05-12 21:44:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 841 ms / 3,000 ms
コード長 3,148 bytes
コンパイル時間 2,541 ms
コンパイル使用メモリ 149,140 KB
実行使用メモリ 94,492 KB
最終ジャッジ日時 2023-08-19 04:20:00
合計ジャッジ時間 22,299 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 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 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 442 ms
50,524 KB
testcase_13 AC 421 ms
46,420 KB
testcase_14 AC 835 ms
82,436 KB
testcase_15 AC 530 ms
56,052 KB
testcase_16 AC 722 ms
71,200 KB
testcase_17 AC 488 ms
52,672 KB
testcase_18 AC 753 ms
72,500 KB
testcase_19 AC 398 ms
43,816 KB
testcase_20 AC 760 ms
71,704 KB
testcase_21 AC 504 ms
55,360 KB
testcase_22 AC 297 ms
94,492 KB
testcase_23 AC 298 ms
93,156 KB
testcase_24 AC 248 ms
78,452 KB
testcase_25 AC 298 ms
64,540 KB
testcase_26 AC 408 ms
82,792 KB
testcase_27 AC 836 ms
82,668 KB
testcase_28 AC 841 ms
82,668 KB
testcase_29 AC 823 ms
82,680 KB
testcase_30 AC 836 ms
82,676 KB
testcase_31 AC 836 ms
82,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

struct edge{
    int to, idx;
    edge(int to, int idx): to(to), idx(idx) {}
};

using P = pair<int, int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<vector<edge>> g(n);
    vector<set<int>> h(n);
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++){
        cin >> a[i] >> b[i]; a[i]--;b[i]--;
        g[a[i]].push_back(edge(b[i], i));
        g[b[i]].push_back(edge(a[i], i));
        h[a[i]].insert(b[i]);
        h[b[i]].insert(a[i]);
    }
    map<P, int> mp;
    for(int i = 0; i < n; i++){
        mp[{a[i], b[i]}] = i;
        mp[{b[i], a[i]}] = i;
    }
    vector<string> ans(n);
    queue<int> que;
    for(int i = 0; i < n; i++){
        if(h[i].size() == 1) {
            que.push(i);
        }
    }
    auto del_edge = [&](int u, int v){
        h[u].erase(v);
        h[v].erase(u);
        if(h[u].size() == 1) {
            que.push(u);
        }
        if(h[v].size() == 1) {
            que.push(v);
        }
    };
    while(!que.empty()){
        auto u = que.front(); que.pop();
        int v = *h[u].begin();
        int idx = mp[{u, v}];
        if(a[idx] == u){
            ans[idx] = "->";
        }else{
            ans[idx] = "<-";
        }
        del_edge(u, v);
    }
    int s = -1;
    for(int i = 0; i < n; i++){
        if(!h[i].empty()){
            s = i;
            break;
        }
    }
    vector<bool> seen(n);
    vector<int> cycle;
    function<void(int)> dfs = [&](int v){
        seen[v] = true;
        cycle.push_back(v);
        for(int to: h[v]){
            if(!seen[to]) dfs(to);
        }
    };
    dfs(s);
    for(int i = 0; i < cycle.size(); i++){
        int j = (i+1)%(cycle.size());
        int u = cycle[i];
        int v = cycle[j];
        int idx = mp[{u, v}];
        if(a[idx] == u){
            ans[idx] = "->";
        }else{
            ans[idx] = "<-";
        }
    }

    print_vector(ans, '\n');
}
0