結果

問題 No.828 全方位神童数
ユーザー koprickykopricky
提出日時 2019-04-11 09:51:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 5,016 bytes
コンパイル時間 1,513 ms
コンパイル使用メモリ 170,860 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-07-19 07:17:36
合計ジャッジ時間 11,240 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 200005;

template<typename T> class node {
public:
    int id;
    T val, lazy;
    node *left, *right, *par;
    node(int key, int value) : id(key), val(value), lazy(0),
        left(nullptr), right(nullptr), par(nullptr){}
    bool isRoot(){
        return !par;
    }
    void push(){
        if(lazy){
            val += lazy;
            if(left) left->lazy += lazy;
            if(right) right->lazy += lazy;
            lazy = 0;
        }
    }
    void rotate(bool right_){
        node<T> *p = par, *g = p->par;
        if(right_){
            if((p->left = right)) right->par = p;
            right = p, p->par = this;
        }else{
            if((p->right = left)) left->par = p;
            left = p, p->par = this;
        }
        par = g;
        if(!g) return;
        if(g->left == p) g->left = this;
        if(g->right == p) g->right = this;
    }
};

template<typename T>
node<T>* splay(node<T>* u){
    while(!(u->isRoot())){
        node<T> *p = u->par, *gp = p->par;
        if(p->isRoot()){ // zig
            p->push(), u->push();
            u->rotate((u == p->left));
        }else{
            gp->push(), p->push(), u->push();
            bool flag = (u == p->left);
            if((u == p->left) == (p == gp->left)){ // zig-zig
                p->rotate(flag), u->rotate(flag);
            }else{ // zig-zag
                u->rotate(flag), u->rotate(!flag);
            }
        }
    }
    u->push();
    return u;
}

// 存在しない key の場合何が返ってくるかは不定
template<typename T>
node<T>* find(const int key, node<T>* root)
{
    node<T> *cur = nullptr, *nx = root;
    while(nx){
        cur = nx, cur->push();
        if(cur->id == key) break;
        if(cur->id > key) nx = cur->left;
        else nx = cur->right;
    }
    return splay(cur);
}

template<typename T>
node<T>* join(node<T>* root1, node<T>* root2)
{
    if(!root1) return root2;
    node<T> *cur = nullptr, *nx = root1;
    while(nx){
        cur = nx, cur->push(), nx = cur->right;
    }
    node<T>* ver = splay(cur);
    ver->right = root2, root2->par = ver;
    return ver;
}

template<typename T>
node<T>* insert(node<T>* ver, node<T>* root)
{
    if(!root) return ver;
    node<T> *cur = nullptr, *nx = root;
    while(nx){
        cur = nx, cur->push();
        if(cur->id > ver->id) nx = cur->left;
        else nx = cur->right;
    }
    if(cur->id > ver->id) cur->left = ver, ver->par = cur;
    else cur->right = ver, ver->par = cur;
    return splay(ver);
}

template<typename T>
node<T>* erase(const int key, node<T>* root)
{
    node<T>* ver = find(key, root);
    return join(ver->left, ver->right);
}

template<typename T>
node<T>* merge(node<T>* root1, node<T>* root2)
{
    if(!root2) return root1;
    return insert(root2, root1);
}

vector<int> G[MAX_N];
int ans[MAX_N];
node<int>* ptr[MAX_N];

void dfs(node<int>* cur)
{
    cur->push();
    ans[cur->id] = cur->val;
    if(cur->left) dfs(cur->left);
    if(cur->right) dfs(cur->right);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n-1){
        int u, v;
        cin >> u >> v;
        if(u > v) swap(u, v);
        G[v-1].pb(u-1);
    }
    rep(i,n){
        ptr[i] = new node<int>(i, 1);
        each(v,G[i]){
            splay(ptr[v]), ptr[v]->lazy += 1;
			merge(ptr[v], splay(ptr[i]));
        }
    }
    dfs(ptr[n-1]);
    rep(i,n){
        cout << ans[i] << "\n";
    }
    return 0;
}
0