結果

問題 No.2301 Namorientation
ユーザー take000take000
提出日時 2023-05-12 22:30:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,268 bytes
コンパイル時間 2,943 ms
コンパイル使用メモリ 223,288 KB
実行使用メモリ 47,484 KB
最終ジャッジ日時 2023-08-19 05:21:25
合計ジャッジ時間 21,725 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 264 ms
46,196 KB
testcase_23 AC 263 ms
45,384 KB
testcase_24 AC 223 ms
38,392 KB
testcase_25 AC 209 ms
37,248 KB
testcase_26 AC 279 ms
47,484 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;

struct Namori {
    vector<int> loop;
    Namori(vector<vector<int>> &G) {
        int N = G.size();
        vector<int> deg(N);
        rep(i, N) deg[i] = G[i].size();

        queue<int> q;
        vector<bool> seen(N);
        rep(i, N) {
            if (deg[i] == 1) {
                q.emplace(i);
                seen[i] = true;
            }
        }

        while (!q.empty()) {
            int u = q.front();
            q.pop();

            for (int v : G[u]) {
                if (seen[v]) continue;
                deg[v]--;

                if (deg[v] == 1) {
                    q.emplace(v);
                    seen[v] = true;
                }
            }
        }

        rep(i, N) {
            if (!seen[i])
                loop.emplace_back(i);
        }
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int N;
    cin >> N;
    map<pair<int, int>, int> edges;
    vector<vector<int>> G(N);
    rep(i, N) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        edges[{u, v}] = i;
        edges[{v, u}] = -i;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    Namori na(G);
    vector<string> ans(N);
    int n = na.loop.size();
    rep(i, n) {
        int u = na.loop[i];
        int v = na.loop[(i + 1) % n];
        int id = edges[{u, v}];

        if (id < 0)
            ans[-id] = "<-";
        else
            ans[id] = "->";
    }

    vector<bool> seen(N);
    for (int i : na.loop) seen[i] = true;
    for (int i : na.loop) {
        if (G[i].size() == 0) continue;

        stack<pair<int, int>> st;
        for (int v : G[i]) {
            if (seen[v]) continue;
            st.emplace(i, v);
        }

        while (!st.empty()) {
            auto [u, v] = st.top();
            st.pop();

            int id = edges[{u, v}];
            if (id < 0)
                ans[-id] = "->";
            else
                ans[id] = "<-";

            for (int w : G[v]) {
                if (seen[w]) continue;
                seen[w] = true;
                st.emplace(v, w);
            }
        }
    }

    for (auto &a : ans) cout << a << "\n";

    return 0;
}
0