結果

問題 No.1752 Up-Down Tree
ユーザー rianoriano
提出日時 2021-10-11 23:44:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 6,278 bytes
コンパイル時間 3,090 ms
コンパイル使用メモリ 181,824 KB
実行使用メモリ 24,936 KB
最終ジャッジ日時 2023-08-30 06:21:35
合計ジャッジ時間 5,161 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 165 ms
24,932 KB
testcase_01 AC 169 ms
24,936 KB
testcase_02 AC 105 ms
16,116 KB
testcase_03 AC 105 ms
16,104 KB
testcase_04 AC 128 ms
20,496 KB
testcase_05 AC 140 ms
21,968 KB
testcase_06 AC 128 ms
18,992 KB
testcase_07 AC 122 ms
19,008 KB
testcase_08 AC 78 ms
13,280 KB
testcase_09 AC 118 ms
15,616 KB
testcase_10 AC 4 ms
7,104 KB
testcase_11 AC 4 ms
7,276 KB
testcase_12 AC 4 ms
7,120 KB
testcase_13 AC 48 ms
11,104 KB
testcase_14 AC 59 ms
11,968 KB
testcase_15 AC 15 ms
8,264 KB
testcase_16 AC 56 ms
11,708 KB
testcase_17 AC 46 ms
10,912 KB
testcase_18 AC 123 ms
15,876 KB
testcase_19 AC 130 ms
15,924 KB
testcase_20 AC 4 ms
7,096 KB
testcase_21 AC 4 ms
7,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘void dfs_dp(Graph&, int, int)’ 内:
main.cpp:154:9: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  154 |     auto[st,en] = euler_time[s];
      |         ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Pr pair<ll,ll>
#define Tp tuple<int,int,int>
#define all(v) v.begin(),v.end()
#define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr);typedef modint<mod> mint
using Graph = vector<vector<ll>>;

const ll mod = 998244353;
template<uint64_t mod>
struct modint{
    uint64_t val;
    constexpr modint(const int64_t val_=0) noexcept:val((val_%int64_t(mod)+int64_t(mod))%int64_t(mod)){}
    constexpr modint operator-() const noexcept{
        return modint(*this)=mod-val;
    }
    constexpr modint operator+(const modint rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint operator-(const modint rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint operator*(const modint rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint operator/(const modint rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint &operator+=(const modint rhs) noexcept{
        val+=rhs.val;
        val-=((val>=mod)?mod:0);
        return (*this);
    }
    constexpr modint &operator-=(const modint rhs) noexcept{
        val+=((val<rhs.val)?mod:0);
        val-=rhs.val;
        return (*this);
    }
    constexpr modint &operator*=(const modint rhs) noexcept{
        val=val*rhs.val%mod;
        return (*this);
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        uint64_t ex=mod-2;
        modint now=1;
        while(ex){
            now*=((ex&1)?rhs:1);
            rhs*=rhs,ex>>=1;
        }
        return (*this)*=now;
    }
    constexpr bool operator==(const modint rhs) noexcept{
        return val==rhs.val;
    }
    constexpr bool operator!=(const modint rhs) noexcept{
        return val!=rhs.val;
    }
    friend constexpr ostream &operator<<(ostream& os,const modint x) noexcept{
        return os<<(x.val);
    }
    friend constexpr istream &operator>>(istream& is,modint& x) noexcept{
        uint64_t t;
        is>>t,x=t;
        return is;
    }
};


//segment tree
//1-indexed all
class segtree {
public:
    ll n;
    vector<ll> A;
    segtree(ll k){
        n = 1;
        while(n<k){ n *= 2; }
        A=vector<ll>(2*n,0);
    }

    //a[i]にxを加算する
    void add(ll i,ll x){
        int index = n-1+i;
        A[index] += x;
        while(index>1){
            index /= 2;
            A[index] = max(A[2*index],A[2*index+1]);
        }
    }

    //a[i]をにする
    void replace(ll i,ll x){
        int index = n-1+i;
        A[index] = x;
        while(index>1){
            index /= 2;
            A[index] = max(A[2*index],A[2*index+1]);
        }
    }

    //a[i]+a[i+1]+…+a[j]を求める
    ll mx(ll i,ll j){
        return rangesum(i,j,1,1,n);
    }

    // a,b求める区間 k区間番号 c,d区間の始終点(k=1,c=1,d=nで入力する)
    ll rangesum(ll a,ll b,ll k,ll c,ll d){
        //単位元の設定
        ll el = 0;
        if(d<a||b<c){
            return el;
        }
        else if(a<=c&&d<=b){
            return A[k];
        }
        else{
            //2区間に分割して演算を行う
            ll p = max(rangesum(a,b,k*2,c,(c+d)/2),rangesum(a,b,k*2+1,(c+d)/2+1,d));
            return p;
        }
    }

};

segtree seq(200001);
ll base = 1e6; //maxのindexを特定するために val*base+index で保持する

//dfs
//s:始点 i:dfs回数 t:始点からの距離
vector<int> vis; int t;
vector<Pr> euler_time;

void dfs(Graph &G, int s,int i){
    t++;
    ll st = t;
    for(int nx:G[s]){
        if(vis[nx]==i) continue;
        vis[nx] = i;
        dfs(G,nx,i);
    }
    t++;
    ll en = t;
    euler_time[s] = make_pair(st,en);
}

void dfs_dp(Graph &G, int s,int i){
    for(int nx:G[s]){
        if(vis[nx]==i) continue;
        vis[nx] = i;
        dfs_dp(G,nx,i);
    }
    auto[st,en] = euler_time[s];
    ll m1 = seq.mx(st,en);
    ll m1_val = m1/base; ll m1_idx = m1%base;
    seq.replace(m1_idx,0);
    ll m2 = seq.mx(st,en);
    ll m2_val = m2/base; ll m2_idx = m2%base;
    seq.replace(m2_idx,0);
    // cout << "----------" << endl;
    // cout << m1_val << " " << m2_val << endl;
    if(m1_val%2==0&&m2_val>0&&m2_val%2==0){
        m2_val--;
        //cout << m2_val << endl;
        seq.replace(m2_idx,base+m2_idx);
    }
    seq.replace(st,(m1_val+m2_val+1)*base+st);
    //cout << s << " " << (m1_val+m2_val+1) << endl;
}


//Unionfind
struct unionfind {
	//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
	//そうでなければ親のid
	  vector<long long> r;
      vector<bool> lp;
	  unionfind(long long N) {
		    r = vector<long long>(N, -1);
            lp = vector<bool>(N,false);
	  }
	  long long root(long long x) {
		    if (r[x] < 0) return x;
		    return r[x] = root(r[x]);
	  }
	  bool unite(long long x, long long y) {
		    x = root(x);
		    y = root(y);
		    if (x == y){
                lp[x] = true;
                return false;
            }
		    if (r[x] > r[y]) swap(x, y);
		    r[x] += r[y];
		    r[y] = x;
            lp[x] = (lp[x]||lp[y]);
		    return true;
	  }
	  long long size(long long x) {
		    return max(-r[root(x)],0LL);
	  }
  
  // 2つのデータx, yが属する木が同じならtrueを返す
    bool same(long long x, long long y) { 
        long long rx = root(x);
        long long ry = root(y);
        return rx == ry;
    }
 
    bool loop(ll x){
        return lp[root(x)];
    }
 
    //重みをつける場合
    void eval(ll i,ll x){
        r[i] = (-1)*x;
    }
};
    

int main() {
    riano_; ll ans = 0;
    ll N; cin >> N;
    //main関数内で 
    unionfind uf(N+1);
    assert(1<=N&&N<=100000);
    Graph G(N+1);
    rep(i,N-1){
        ll a,b; cin >> a >> b;
        assert(1<=a&&a<=N&&1<=b&&b<=N);
        G[a].push_back(b); G[b].push_back(a);
        uf.unite(a,b);
        bool loop = uf.loop(a);
        assert(!loop);
    }
    assert(uf.size(1)==N);
    //main関数内で
    vis.assign(N+1,-1);
    euler_time.assign(N+1,make_pair(0,0));
    int s = 1;
    vis[s] = 0; t = 0; //s:始点
    dfs(G,s,0); //s:始点
    vis[s] = 1;
    dfs_dp(G,s,1);
    ans = seq.mx(1,2*N)/base;    
    cout << ans << endl;
}
0