結果

問題 No.2301 Namorientation
ユーザー umimelumimel
提出日時 2023-05-12 22:06:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 457 ms / 3,000 ms
コード長 6,209 bytes
コンパイル時間 2,149 ms
コンパイル使用メモリ 188,852 KB
実行使用メモリ 62,864 KB
最終ジャッジ日時 2024-05-06 12:04:24
合計ジャッジ時間 16,415 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 244 ms
28,544 KB
testcase_13 AC 224 ms
26,780 KB
testcase_14 AC 428 ms
45,524 KB
testcase_15 AC 300 ms
32,288 KB
testcase_16 AC 409 ms
39,424 KB
testcase_17 AC 285 ms
30,540 KB
testcase_18 AC 380 ms
41,140 KB
testcase_19 AC 226 ms
24,832 KB
testcase_20 AC 377 ms
39,808 KB
testcase_21 AC 269 ms
30,976 KB
testcase_22 AC 384 ms
62,864 KB
testcase_23 AC 387 ms
61,696 KB
testcase_24 AC 299 ms
52,152 KB
testcase_25 AC 248 ms
37,936 KB
testcase_26 AC 319 ms
48,256 KB
testcase_27 AC 456 ms
45,568 KB
testcase_28 AC 436 ms
45,492 KB
testcase_29 AC 446 ms
45,568 KB
testcase_30 AC 457 ms
47,012 KB
testcase_31 AC 440 ms
46,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60;
const int IINF = 1 << 30;

template<typename T> struct Edge{
    int to; T w;
    Edge(int to_, T w_=1){
        to = to_;
        w=w_;
    }
};
template<typename T> using Tree = vector<vector<Edge<T>>>;
template<typename T> using Graph = vector<vector<Edge<T>>>;
/* 容量&重み付きエッジ for Dinic */
template<typename T> struct REdge{
    int to;
    T cap;
    T cost;
    int rev;
    REdge(int to_, T cap_, T cost_=1){
        to = to_;
        cap = cap_;
        cost = cost_;
    }
    
    REdge(int to_, T cap_, T cost_, int rev_){
        to = to_;
        cap = cap_;
        cost = cost_;
        rev = rev_;
    }
};

/* 残余グラフ for Dinic */
template<typename T> using RGraph = vector<vector<REdge<T>>>;

template<long long mod>
class modint{
    long long x;
public:
    modint(long long x=0) : x((x%mod+mod)%mod) {}
    modint operator-() const { 
      return modint(-x);
    }
    bool operator==(const modint& a){
        if(x == a) return true;
        else return false;
    }
    bool operator==(long long a){
        if(x == a) return true;
        else return false;
    }
    bool operator!=(const modint& a){
        if(x != a) return true;
        else return false;
    }
    bool operator!=(long long a){
        if(x != a) return true;
        else return false;
    }
    modint& operator+=(const modint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator-=(const modint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator*=(const  modint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    modint operator+(const modint& a) const {
        modint res(*this);
        return res+=a;
    }
    modint operator-(const modint& a) const {
        modint res(*this);
        return res-=a;
    }
    modint operator*(const modint& a) const {
        modint res(*this);
        return res*=a;
    }
    modint pow(long long t) const {
        if (!t) return 1;
        modint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    modint inv() const {
        return pow(mod-2);
    }
    modint& operator/=(const modint& a) {
        return (*this) *= a.inv();
    }
    modint operator/(const modint& a) const {
        modint res(*this);
        return res/=a;
    }

    friend std::istream& operator>>(std::istream& is, modint& m) noexcept {
        is >> m.x;
        m.x %= mod;
        if (m.x < 0) m.x += mod;
        return is;
    }

    friend ostream& operator<<(ostream& os, const modint& m){
        os << m.x;
        return os;
    }
};

using mint = modint<MOD998244353>;

struct Namori{
    ll N;
    vector<vector<ll>> G;
    vector<ll> group; //goroup[閉路上の頂点]=-1, gourp[閉路外の頂点]=根の頂点

    Namori(vector<vector<ll>> G_){
        N = (ll)G_.size();
        G.resize(N);
        for(ll u=0; u<N; u++) for(ll v : G_[u]) G[u].pb(v);
        group.resize(N, -1);
        decomposition();
    }

    void dfs(ll u, ll p, ll s){
        group[u] = s;
        for(ll v : G[u]){
            if(v == p) continue;
            dfs(v, u, s);
        }
    }

    void decomposition(){
        //閉路上の頂点を決定
        vector<ll> deg(N, 0);
        for(ll u=0; u<N; u++) deg[u] = (ll)G[u].size();
        queue<ll> Q;
        for(ll u=0; u<N; u++) if(deg[u]==1) Q.push(u);

        while(!Q.empty()){
            ll u = Q.front();
            Q.pop();
            group[u] = 0;

            for(ll v : G[u]){
                deg[v]--;
                if(deg[v]==1) Q.push(v);
            }
        }

        //閉路外の頂点の根を決定
        for(ll s=0; s<N; s++){
            if(group[s]!=-1) continue;
            for(ll v : G[s]){
                if(group[v]==-1) continue;
                dfs(v, s, s);
            }
        }
    }

    ll groupNumber(ll v){return group[v];}
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll n; cin >> n;
    vector<vector<ll>> G(n);
    vector<pll> E(n);
    rep(i, n){
        ll a, b; cin >> a >> b;
        a--; b--;
        G[a].pb(b);
        G[b].pb(a);
        E[i] = {a, b};
    }

    Namori nmr(G);

    vector<ll> par(n, -1);
    function<void(ll, ll)> dfs = [&](ll v, ll p){
        par[v] = p;
        for(ll u : G[v]) if(u!=p) dfs(u, v);
    };

    rep(v, n){
        if(nmr.groupNumber(v)==-1){
            for(ll vv : G[v]) if(nmr.groupNumber(vv)!=-1) dfs(vv, v);
        }
    }

    vector<ll> ans(n, -1);
    //木の部分の辺の向きを決定
    for(ll i=0; i<n; i++){
        if(nmr.groupNumber(E[i].fi)==-1 && nmr.groupNumber(E[i].se)==-1) continue;
        if(par[E[i].fi]==E[i].se){
            ans[i] = 0;
        }else{
            ans[i] = 1;
        }
    }

    vector<vector<pll>> H(n);
    ll s = 0;
    for(ll i=0; i<n; i++){
        if(nmr.groupNumber(E[i].fi)==-1 && nmr.groupNumber(E[i].se)==-1){
            H[E[i].fi].pb({E[i].se, i});
            H[E[i].se].pb({E[i].fi, i});
            s = E[i].fi;
        }
    }

    ll t = H[s][0].fi;
    if(E[H[s][0].se].fi==s){
        ans[H[s][0].se] = 0;
    }else{
        ans[H[s][0].se] = 1;
    }

    function<void(ll, ll)> dfs2 = [&](ll v, ll p){
        if(v == s) return;
        for(pll tmp : H[v]){
            ll u = tmp.fi;
            ll idx = tmp.se;
            if(p == u) continue;
            if(E[idx].fi==v){
                ans[idx] = 0;
            }else{
                ans[idx] = 1;
            }
            dfs2(u, v);
        }
    };

    dfs2(t, s);
    rep(i, n){
        if(ans[i]==0) cout << "->" << endl;
        else cout << "<-" << endl;
    }
}
0