結果

問題 No.1752 Up-Down Tree
ユーザー blackyukiblackyuki
提出日時 2022-01-22 14:17:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,044 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 212,936 KB
実行使用メモリ 26,576 KB
最終ジャッジ日時 2023-08-18 07:22:56
合計ジャッジ時間 8,567 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 154 ms
26,576 KB
testcase_01 AC 164 ms
26,448 KB
testcase_02 AC 112 ms
15,776 KB
testcase_03 AC 110 ms
15,648 KB
testcase_04 AC 153 ms
20,696 KB
testcase_05 AC 159 ms
22,560 KB
testcase_06 AC 144 ms
18,524 KB
testcase_07 AC 148 ms
18,708 KB
testcase_08 AC 82 ms
11,484 KB
testcase_09 AC 131 ms
14,828 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
class segtree{
    vp seg;ll n;
public:
    P g=P(-inf,0);
    P f(P a,P b){
        return max(a,b);
    }
    segtree(vp v):n(v.size()){
        seg=vp(n*2,g);
        rep(i,n)seg[i+n]=v[i];
        for(ll i=n-1;i;i--)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
    void update(ll i,P x){
        i+=n;seg[i]=x;
        for(i>>=1;i;i>>=1)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
    P get(ll l,ll r){
        P resl=g,resr=g;
        for(l+=n,r+=n;l<r;l>>=1,r>>=1){
            if(l&1)resl=f(resl,seg[l++]);
            if(r&1)resr=f(seg[--r],resr);
        }
        return f(resl,resr);
    }
};
int main(){
    ll n;cin>>n;
    vvi g(n);
    rep(i,n-1){
        ll a,b;cin>>a>>b;a--;b--;
        g[a].pb(b);g[b].pb(a);
    }
    vp tmp(n);rep(i,n)tmp[i]=P(0,i);
    segtree seg(tmp);
    ll cnt=0;
    vi euler(n);
    function<void(ll,ll)> dfs=[&](ll i,ll p){
        euler[i]=cnt++;
        for(ll x:g[i])if(x!=p)dfs(x,i);
        P a,b;
        a=seg.get(euler[i],cnt);
        seg.update(a.se,P(0,a.se));
        b=seg.get(euler[i],cnt);
        seg.update(b.se,P(0,b.se));
        ll res=a.fi+b.fi+1;
        if(a.fi%2==0&&b.fi%2==0&&a.fi&&b.fi){
            res--;
            while(true){
                P p=seg.get(euler[i],cnt);
                if(p.fi==b.fi){
                    tmp.pb(p);
                    seg.update(p.se,P(0,p.se));
                }
                else{
                    if(p.fi==b.fi-1){
                        seg.update(b.se,b);
                        seg.update(p.se,P(0,p.se));
                    }
                    for(auto x:tmp)seg.update(x.se,x);
                    break;
                }
            }
        }
        seg.update(euler[i],P(res,euler[i]));
    };dfs(0,-1);
    out(seg.get(0,1).fi);
}
0