結果

問題 No.2301 Namorientation
ユーザー noya2noya2
提出日時 2023-05-12 22:11:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 436 ms / 3,000 ms
コード長 15,494 bytes
コンパイル時間 5,364 ms
コンパイル使用メモリ 294,028 KB
実行使用メモリ 50,524 KB
最終ジャッジ日時 2024-05-06 12:13:55
合計ジャッジ時間 17,227 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 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 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 204 ms
29,732 KB
testcase_13 AC 168 ms
27,444 KB
testcase_14 AC 409 ms
47,976 KB
testcase_15 AC 238 ms
33,224 KB
testcase_16 AC 325 ms
41,676 KB
testcase_17 AC 218 ms
30,748 KB
testcase_18 AC 352 ms
42,408 KB
testcase_19 AC 159 ms
25,776 KB
testcase_20 AC 337 ms
42,012 KB
testcase_21 AC 239 ms
32,300 KB
testcase_22 AC 191 ms
50,524 KB
testcase_23 AC 187 ms
49,608 KB
testcase_24 AC 156 ms
42,248 KB
testcase_25 AC 132 ms
38,548 KB
testcase_26 AC 181 ms
49,128 KB
testcase_27 AC 428 ms
48,228 KB
testcase_28 AC 419 ms
48,092 KB
testcase_29 AC 436 ms
48,216 KB
testcase_30 AC 432 ms
48,216 KB
testcase_31 AC 417 ms
48,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "c.cpp"
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define repp(i,m,n) for (int i = m; i < int(n); ++i)
#define reb(i,n) for (int i = int(n)-1; i >= 0; --i)
#define all(v) v.begin(),v.end()
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using pdd = pair<long double, long double>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
template<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}
template<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}
template<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}
template<typename T> T rev(const T& str_or_vec){T res = str_or_vec; reverse(res.begin(),res.end()); return res; }
template<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<typename T>void uniq(vector<T> &v){sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());}
template<typename T1, typename T2>void print(pair<T1,T2> a);
template<typename T>void print(vector<T> v);
template<typename T>void print(vector<vector<T>> v);
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
void print(unsigned int a){ printf("%u", a); }
void print(unsigned long long a) { printf("%llu", a); }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
template<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}
template<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}
template<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}
void yes(){out("Yes");}
void no (){out("No");}
void yn (bool t){if(t)yes();else no();}
void fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);}
void o(){out("!?");}

namespace noya2{

const int INF = 1001001007;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long inf = 2e18;
const long double pi = 3.14159265358979323;
const long double eps = 1e-7;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

} // namespace noya2
using namespace noya2;

using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;
void out(mint a){out(a.val());}
void out(vector<mint> a){vector<ll> b(a.size()); rep(i,a.size()) b[i] = a[i].val(); out(b);}
void out(vector<vector<mint>> a){for (auto v : a) out(v);}

#line 2 "Tree-core.hpp"

#line 5 "Tree-core.hpp"

namespace noya2 {

using namespace std;

struct naiveTree { // undirected unweighted tree
    naiveTree (int _n = 0) : n(_n){ init();}
    void add_edge(int u, int v, int id = -1){
        es0[u].emplace_back(v);
        es0[v].emplace_back(u);
        es1[u].emplace_back(v,id);
        es1[v].emplace_back(u,id);
    }
    void remake(int new_n){
        es0.clear(); es1.clear(); vis.clear();
        n = new_n;
        init();
    }
    bool yet(int v){ return vis[v] == 0;}
    void visit(int v) { vis[v]++;}
    void reset(int v = -1){ 
        if (v == -1) fill(vis.begin(),vis.end(),0);
        else vis[v] = 0;
    }
    const vector<int>& operator[](int idx) const { return es0[idx];}
    const vector<pair<int,int>>& operator()(int idx) const {return es1[idx];}
  private:
    int n;
    vector<vector<int>> es0;
    vector<vector<pair<int,int>>> es1;
    vector<int> vis;
    void init(){
        es0.resize(n);
        es1.resize(n);
        vis.resize(n,0);
    }
};

struct usefulTree { // rooted tree
    usefulTree (int _n = 0, int _root = 0) : n(_n), root(_root) { init();}
    void add_edge(int u, int v){
        es[u].emplace_back(v);
        es[v].emplace_back(u);
    }
    void remake(int new_n, int new_root = 0){
        es.clear(); vis.clear();
        n = new_n, root = new_root;
        init();
    }
    bool yet(int v){ return vis[v] == 0;}
    void visit(int v) { vis[v]++;}
    void reset(int v = -1){ 
        if (v == -1) fill(vis.begin(),vis.end(),0);
        else vis[v] = 0;
    }
    const vector<int>& operator[](int idx) const { return es[idx];}
    int parent(int v){ return par[0][v];}
    int subtree_size(int v){
        if (sub[v] != -1) return sub[v];
        sub[v] = 1;
        for (int child : es[v]){
            if (child != par[0][v]) sub[v] += subtree_size(child);
        }
        return sub[v];
    }
    int depth(int v){ return dep[v];}
    int lca(int u, int v){
        if (dep[u] > dep[v]) swap(u,v);
        for (int i = 0; i < p2size; i++) if ((dep[v] - dep[u]) >> i & 1) v = par[i][v];
        if (u == v) return u;
        for (int i = p2size-1; i >= 0; i--){
            if (par[i][u] != par[i][v]){
                u = par[i][u];
                v = par[i][v];
            }
        }
        return par[0][u];
    }
    int jump_to_root(int from, int d){
        for (int i = 0; i < p2size; i++){
            if ((d >> i & 1) == 1 && from != -1) from = par[i][from];
        }
        return from;
    }
    int jump(int from, int to, int d){
        int l = lca(from,to);
        if (d <= dep[from] - dep[l]){
            return jump_to_root(from,d);
        }
        d -= dep[from] - dep[l];
        if (d <= dep[to] - dep[l]){
            return jump_to_root(to,dep[to]-dep[l]-d);
        }
        return -1;
    }
    vector<int> path(int from, int to){
        int l = lca(from,to);
        int nf = from, nt = to;
        vector<int> pf = {from}, pt = {to};
        while (nf != l){
            nf = par[0][nf];
            pf.emplace_back(nf);
        }
        while (nt != l){
            nt = par[0][nt];
            pt.emplace_back(nt);
        }
        for (int i = pt.size()-2; i >= 0; i--) pf.emplace_back(pt[i]);
        return pf;
    }
    int dist(int u, int v){ return dep[u] + dep[v] - 2 * dep[lca(u,v)];}
    void build(){
        par.clear();
        dep.clear();
        sub.clear();
        p2size = 1;
        int _ni = 1; // _ni = 2^(p2size - 1), n-1 <= 2^(p2size - 1) must be holded
        while (_ni < n-1) p2size++, _ni <<= 1;
        par.resize(p2size,vector<int>(n,-1));
        dep.resize(n,-1);
        sub.resize(n,-1);
        queue<int> que;
        que.push(root);
        dep[root] = 0;
        while (!que.empty()){
            int p = que.front(); que.pop();
            for (int to : es[p]){
                if (dep[to] == -1){
                    par[0][to] = p;
                    dep[to] = dep[p] + 1;
                    que.push(to);
                }
            }
        }
        for (int i = 1; i < p2size; i++){
            for (int v = 0; v < n; v++){
                if (par[i-1][v] == -1) continue;
                par[i][v] = par[i-1][par[i-1][v]];
            }
        }
    }
  private:
    int n, root;
    vector<vector<int>> es;
    vector<int> vis;
    int p2size;
    vector<vector<int>> par;
    vector<int> dep, sub;
    void init(){
        es.resize(n);
        vis.resize(n,0);
    }
};

/* point hld (commutative)

vector<S> a(n); // vertex v has a[v]
hldTree g(n);
segtree<S,op,e> seg(n);
rep(i,n) seg.set(i,a[g.ord(i)]); // pre <-> ord (pre(v) = i, ord(i) = v)

update query :
    int v; S x; cin >> v >> x;
    seg.set(g.pre(v),x);

product query :
    S ans = e();
    auto f = [&](int l, int r){
        ans = op(ans,seg.prod(l,r));
    };
    int u, v; cin >> u >> v;
    ans = e();
    g.path_query(u, v, true, f);
    cout << ans << endl;

*/

/* edge hld (commutative)

vector<S> b(n-1); // edge i has b[i]
vector<S> a(n); // vertex v has a[v]
rep(v,n) a[v] = (v == root ? e() : b[g.edge(v)]);

update query :
    int id; S x; cin >> id >> x; // edge id
    seg.set(g.pre(who(id)),x); // edge <-> who (edge(v) = i, who(i) = v)

*/

struct hldTree {
    hldTree (int _n = 0, int _root = 0) : n(_n), root(_root) { init();}
    void add_edge(int u, int v, int id){ // id must be 0 <= id < n
        es[u].emplace_back(v,id);
        es[v].emplace_back(u,id);
    }
    void remake(int new_n, int new_root = 0){
        es.clear(); size.clear(); par.clear(); dep.clear(); up.clear(); down.clear();
        nxt.clear(); order.clear(); edges.clear(); whose.clear();
        n = new_n, root = new_root;
        init();
    }
    void build(){
        dfs_init(root);
        int t = 0;
        dfs_hld(root,t);
    }
    int lca(int u, int v){
        while (nxt[u] != nxt[v]){
            if (down[u] < down[v]) swap(u,v);
            u = par[nxt[u]];
        }
        return dep[u] < dep[v] ? u : v;
    }
    int dist(int u, int v){
        return dep[u] + dep[v] - 2 * dep[lca(u,v)];
    }
    int parent(int v){ return par[v];}
    int depth(int v){ return dep[v];}
    int subtree_size(int v){ return size[v];}
    int pre(int v){ return down[v];}
    int post(int v){ return up[v];}
    int ord(int i){ return order[i];}
    int who(int i){ return whose[i];}
    int edge(int v){ return edges[v];}
    template<typename F>
    void path_query(int u, int v, bool vertex, const F &f){ // f is function takes (left, right) as argument, range = [left,right).
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            s > t ? f(t,s) : f(s,t);
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            s > t ? f(t,s) : f(s,t);
        }
    }
    template<typename F>
    void path_noncommutative_query(int u, int v, bool vertex, const F &f){ // op(l,r) != op(r,l), so prod[u->...->v] != prod[v->...->u]
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            f(s,t); // le > ri ok
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            f(s,t); // le > ri ok
        }
    }
    template<typename F>
    void subtree_query(int v, bool vertex, const F &f){
        f(down[v] + (vertex ? 0 : 1), up[v]);
    }
    const vector<pair<int,int>>& operator()(int idx) const { return es[idx];}
  private:
    int n, root;
    vector<vector<pair<int,int>>> es;
    vector<int> size, par, dep, up, down, nxt; // nxt[i] : most shallow vertex in connected component of vertex i
    vector<int> order, edges, whose; // order[i] is ith vertex visited on Euler tour, vertex v has edges[v] (root has no edge), edges^-1 = whose
    void init(){
        es.resize(n);
        size.resize(n,0);
        par.resize(n,root);
        dep.resize(n,0);
        up.resize(n,-1);
        down.resize(n,-1);
        nxt.resize(n,root);
        order.resize(n,-1);
        edges.resize(n,-1);
        whose.resize(n,-1);
    }
    void dfs_init(int cur){
        size[cur] = 1;
        for (auto &e : es[cur]){
            if (e.first == par[cur]){
                if (es[cur].size() >= 2 && e.first == es[cur][0].first){
                    swap(es[cur][0],es[cur][1]); // if cur is not leaf, vs[cur][0] is not cur's parent
                }
                else continue;
            }
            par[e.first] = cur;
            edges[e.first] = e.second;
            whose[e.second] = e.first;
            dep[e.first] = dep[cur] + 1;
            dfs_init(e.first);
            size[cur] += size[e.first];
            if (size[e.first] > size[es[cur][0].first]){
                swap(e,es[cur][0]); // to maximize vs[cur][0]'s subtree_size
            }
        }
    }
    void dfs_hld(int cur, int &tnow){
        down[cur] = tnow++; // down[0,...,n-1] is permutation of 0,...,n-1
        order[down[cur]] = cur; 
        for (auto e : es[cur]){
            if (e.first == par[cur]) continue;
            nxt[e.first] = (e.first == es[cur][0].first ? nxt[cur] : e.first);
            dfs_hld(e.first,tnow);
        }
        up[cur] = tnow; // up[0,...,n-1] is NOT permutation, up[*] <= n
    }
    vector<pair<int,int>> ascend(int u, int v) const { // [u,v), depth[u] > depth[v]
        vector<pair<int,int>> res;
        while (nxt[u] != nxt[v]){
            res.emplace_back(down[u],down[nxt[u]]); // [s1,t1], [s2,t2], ...
            u = par[nxt[u]];
        }
        if (u != v) res.emplace_back(down[u],down[v]+1); // [s,t). v is not in the range (down[] is ordered opposite direction of depth)
        return res;
    }
    vector<pair<int,int>> descend(int u, int v) const { // (u,v], depth[u] < depth[v]
        if (u == v) return {};
        if (nxt[u] == nxt[v]){
            return {pair<int,int>(down[u]+1,down[v])}; // (s,t]. u is not in the range
        }
        vector<pair<int,int>> res = descend(u,par[nxt[v]]);
        res.emplace_back(down[nxt[v]],down[v]); // [s1,t1], [s2,t2], ...
        return res;
    }
};

} // namespace noya2
#line 77 "c.cpp"

void solve(){
    int n; cin >> n;
    dsu d(n);
    vector<P> es(n);
    P ng(-1,-1);
    usefulTree g(n);
    map<P,int> mp;
    rep(i,n){
        int u, v; cin >> u >> v; u--, v--;
        if (d.same(u,v)){
            ng = P(u,v);
        }
        else {
            d.merge(u,v);
            g.add_edge(u,v);
        }
        es[i] = P(u,v);
        mp[es[i]] = i;
    }
    g.build();
    vector<int> ans(n,-1);
    auto add = [&](int u, int v){
        if (u > v) ans[mp[P(v,u)]] = 1;
        else ans[mp[P(u,v)]] = 0;
    };
    vector<int> dist(n,INF);
    queue<int> que;
    vector<int> path = g.path(ng.first,ng.second);
    int siz = path.size();
    rep(i,siz){
        add(path[i],path[(i+1)%siz]);
        dist[path[i]] = 0;
        que.push(path[i]);
    }
    while (!que.empty()){
        int p = que.front(); que.pop();
        for (int q : g[p]){
            if (chmin(dist[q],dist[p]+1)){
                que.push(q);
            }
        }
    }
    rep(i,n){
        int u = es[i].first, v = es[i].second;
        if (dist[u] == dist[v]) continue;
        if (dist[u] > dist[v]) add(u,v);
        else add(v,u);
    }
    rep(i,n) out(ans[i] == 1 ? "<-" : "->");
}

int main(){
    fast_io();
    int t = 1; //cin >> t;
    while(t--) solve();
}
0