Submission #3803734


Source Code Expand

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author majk
 */

#ifndef MAJK_LIB
#define MAJK_LIB

#include <vector>
#include <stack>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
#include <array>
#include <bitset>
using namespace std;

#define x first
#define y second
typedef std::pair<int,int> pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<ui,ui> puu;

template <typename T, typename U> std::istream&operator>>(std::istream&i, pair<T,U>&p) {i >> p.x >> p.y; return i;}
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template <typename T, typename U> std::ostream&operator<<(std::ostream&o, const pair<T,U>&p) {o << p.x << ' ' << p.y; return o;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {if(t.empty())o<<'\n';for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; }
ui logceil(ll x) { return x?8*sizeof(ll)-__builtin_clzll(x):0; }

namespace std { template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}}; }
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename F> double bshd(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){l=m;}else{h=m;}}return (l+h)/2;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename F> double bsld(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){h=m;}else{l=m;}}return (l+h)/2;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector2<T>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector2<T>>(a,vector2<T>(b,c,t)){}};
template<typename T>class vector4:public vector<vector3<T>>{public:vector4(){} vector4(size_t a,size_t b,size_t c,size_t d,T t=T()):vector<vector3<T>>(a,vector3<T>(b,c,d,t)){}};
template<typename T>class vector5:public vector<vector4<T>>{public:vector5(){} vector5(size_t a,size_t b,size_t c,size_t d,size_t e,T t=T()):vector<vector4<T>>(a,vector4<T>(b,c,d,e,t)){}};


#endif


template<typename F>struct NoOp{void setup(ui, F){}void op(F&p,F n,ui,ui){p=n;}void down(F&,F&,F&,ui,ui) {}};

template<typename F,typename SetOp,typename PowerOp>struct Lazy{
    void setup(ui s,F def){this->def=def;this->s=s;L=new F[s]();fill(L,L+s,def);}
    void down(F&u,F&l,F&r,ui i,ui s){op(l,L[i],i<<1,s>>1);op(r,L[i],i<<1|1,s>>1);L[i]=def;}
    void op(F&p,F n,ui i,ui s){p=sop(p,pop(n,s));if(i<this->s)this->L[i]=sop(this->L[i],n);}
    SetOp sop;PowerOp pop;F*L;ui s;F def;
};

template <typename F, typename CombineOp, typename ModifyOp = NoOp<F>> struct SegTree {
	void setup(ui s, F def) {
		n = 1<<logceil(s);
		T = vector<F>(2*n, def);
		for (ui i = n-1; i > 0; i--) T[i] = cop(T[i<<1],T[i<<1|1]);
		mop.setup(2*n,def);
	}

	void setup(vector<F> & data, F def = F()) {
		n = 1<<logceil(data.size());
		T = vector<F>(2*n, def);
		copy(data.begin(), data.end(), T.begin() + n);
		for (ui i = n-1; i > 0; i--) T[i] = cop(T[i<<1],T[i<<1|1]);
		mop.setup(2*n,def);
	}

	inline void put(ui x, F n) { put(x, x, n); }
	inline void put(ui from, ui to, F v) { put2(from, to+1, v, 1, n); }
	inline F get(ui x) { return get(x, x); }
	inline F get(ui from, ui to) { return get2(from, to+1, 1, n); }

	void put2(ui from, ui to, F v, ui i, ui s) {
		if (from == 0 && to == s) { mop.op(T[i], v, i, s); return; }
		mop.down(T[i], T[i<<1], T[i<<1|1], i, s);
        s>>=1;i<<=1;
        if (to <= s) { put2(from, to, v, i, s); }
        else if (from >= s) { put2(from-s, to-s, v, i|1, s); }
        else {
            put2(from, s, v, i, s);
            put2(0, to-s, v, i|1, s);
        }
		T[i>>1] = cop(T[i], T[i|1]);
	}

	F get2(ui from, ui to, ui i, ui s) {
        while (true) {
            if (from == 0 && to == s) return T[i];
            mop.down(T[i], T[i << 1], T[i << 1 | 1], i, s);
            s >>= 1;i <<= 1;
            if (to > s) {
                to -= s;
                if (from >= s) { from -= s; i|=1; }
                else return cop(get2(from, s, i, s), get2(0, to, i|1, s));
            }
        }
    }

	ui n;
	vector<F> T;
	CombineOp cop;
    ModifyOp mop;
};


template <typename F> struct AddOp { F operator()(F a, F b) { return a+b; }};
template <typename F> struct MinOp { F operator()(F a, F b) { return std::min(a,b); }};
template <typename F> struct MaxOp { F operator()(F a, F b) { return std::max(a,b); }};
template <typename F> struct MultiplyOp { F operator()(F a, F b) { return a*b; }};
template <typename F> struct MultOp { F operator()(F a, ui b) { return a*b; }};
template <typename F> struct IdempOp { F operator()(F a, ui b) { return a; }};
template <typename F> struct InverseOp { F operator()(F a, F b) { return b?b-a:a; }};

template<typename T> using AddSumTree = SegTree<T, AddOp<T>, Lazy<T, AddOp<T>, MultOp<T>>>;
template<typename T> using AddMaxTree = SegTree<T, MaxOp<T>, Lazy<T, AddOp<T>, IdempOp<T>>>;
template<typename T> using AddMinTree = SegTree<T, MinOp<T>, Lazy<T, AddOp<T>, IdempOp<T>>>;
template<typename T> using AssignMinTree = SegTree<T, MinOp<T>, Lazy<T, MinOp<T>, IdempOp<T>>>;
template<typename T> using AssignMaxTree = SegTree<T, MaxOp<T>, Lazy<T, MaxOp<T>, IdempOp<T>>>;
template<typename T> using XorTree = SegTree<T, AddOp<T>, Lazy<T, InverseOp<T>, MultOp<T>>>;

template<typename T> using SetMinTree = SegTree<T, MinOp<T>>;
template<typename T> using SetMaxTree = SegTree<T, MaxOp<T>>;
template<typename T> using SetMulTree = SegTree<T, MultiplyOp<T>>;


class EWanderingTKHS {
public:
    vector<vector<int>> E,R;
    int N, T;
    vector<int> Enter, Exit, P;
    AddSumTree<int> G, H;
    vector<int> W, V;

    void dfs(int u, int p) {
        Enter[u] = T++;
        P[u] = p;
        for (int v : E[u]) if (v != p) dfs(v, u);
        Exit[u] = T;
    }

    void dfsClear(int u) {
        if (G.get(Enter[u]) == 1) {
            G.put(Enter[u], -1);
            for (int v : E[u]) if (v != P[u]) dfsClear(v);
        }
    }

    void dfs2(int u, int mx, int d) {
        mx = max(mx, u);
        V[d] = u;
        W[d] = mx;
        for (int v : E[u]) {
            if (v != P[u]) {
                dfs2(v, mx, d+1);
            }
        }


        if (u != 0) {
            G.put(Enter[u], 1);
            int j = bsh(0, d, [&](int x) { return W[x] < u; });
            R[V[min(d,j+2)]].push_back(u);
        }

        cerr << "clear " << u << ' ' << R[u] << endl;
        for (int r : R[u]) {
            int tot = G.get(Enter[r], Exit[r]-1);
            H.put(Enter[u], Exit[u]-1, tot);
            dfsClear(r);
        }
    }


    void solve(istream& cin, ostream& cout) {
        cin >> N;
        R.resize(N);
        E.resize(N); Enter.resize(N); Exit.resize(N); P.resize(N);
        T = 0;
        for (int i = 0; i < N - 1; ++i) {
            int u, v;
            cin >> u >> v;
            --u;
            --v;
            E[u].push_back(v);
            E[v].push_back(u);
        }
        W.assign(N, 0);
        V.assign(N, 0);
        G.setup(N, 0);
        H.setup(N, 0);
        dfs(0, -1);
        dfs2(0, 0, 0);


        for (int i = 1; i < N; ++i) {
            cout << H.get(Enter[i]) << ' ';
        }
        cout << endl;
    }
};


int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	EWanderingTKHS solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
    return 0;
}

Submission Info

Submission Time
Task E - Wandering TKHS
User majk
Language C++14 (GCC 5.4.1)
Score 1200
Code Size 8628 Byte
Status AC
Exec Time 999 ms
Memory 74104 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1200 / 1200
Status
AC × 3
AC × 32
Set Name Test Cases
Sample sample01.txt, sample02.txt, sample03.txt
All sample01.txt, sample02.txt, sample03.txt, in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, sample01.txt, sample02.txt, sample03.txt
Case Name Status Exec Time Memory
in01.txt AC 962 ms 30324 KiB
in02.txt AC 953 ms 30476 KiB
in03.txt AC 956 ms 30324 KiB
in04.txt AC 957 ms 30452 KiB
in05.txt AC 960 ms 30544 KiB
in06.txt AC 980 ms 30196 KiB
in07.txt AC 2 ms 256 KiB
in08.txt AC 1 ms 256 KiB
in09.txt AC 1 ms 256 KiB
in10.txt AC 1 ms 256 KiB
in11.txt AC 1 ms 256 KiB
in12.txt AC 21 ms 1024 KiB
in13.txt AC 27 ms 1280 KiB
in14.txt AC 15 ms 768 KiB
in15.txt AC 15 ms 768 KiB
in16.txt AC 20 ms 1024 KiB
in17.txt AC 999 ms 54772 KiB
in18.txt AC 718 ms 35444 KiB
in19.txt AC 740 ms 35444 KiB
in20.txt AC 916 ms 74104 KiB
in21.txt AC 852 ms 34560 KiB
in22.txt AC 881 ms 34856 KiB
in23.txt AC 914 ms 35068 KiB
in24.txt AC 941 ms 34800 KiB
in25.txt AC 939 ms 34804 KiB
in26.txt AC 1 ms 256 KiB
sample01.txt AC 1 ms 256 KiB
sample02.txt AC 1 ms 256 KiB
sample03.txt AC 1 ms 256 KiB