結果

問題 No.1718 Random Squirrel
ユーザー pockynypockyny
提出日時 2021-12-17 23:53:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,288 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 84,520 KB
実行使用メモリ 814,900 KB
最終ジャッジ日時 2023-10-13 03:14:09
合計ジャッジ時間 7,209 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 MLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘ll dfs3(int, int)’ 内:
main.cpp:36:1: 警告: 非 void を戻す関数内に return 文がありません [-Wreturn-type]
   36 | }
      | ^

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;
vector<int> G[200010];
ll sum[200010],mx1[200010],mx2[200010],a[200010];
void dfs1(int s,int p){
    sum[s] += a[s];
    for(int v:G[s]){
        if(v==p) continue;
        dfs1(v,s);
        sum[s] += sum[v];
    }
}

void dfs2(int s,int p){
    ll ret = -100000000;
    for(int v:G[s]){
        if(v==p) continue;
        dfs2(v,s);
        ret = max(ret,mx1[v]);
    }
    mx1[s] = ret + 1;
    if(a[s]) mx1[s] = max(mx1[s],0LL);
}

int init = 0;
ll dfs3(int s,int p){
    init++;
    for(int v:G[s]){
        if(v==p || sum[v]==0) continue;
        dfs3(v,s);
    }
}

ll ans[200010];
void reroot1(int s,int p,ll now,ll cnt){
    ans[s] = now;
    for(int v:G[s]){
        if(v==p) continue;
        if(sum[v]==0) reroot1(v,s,now + 2,cnt);
        else if(sum[v]==cnt) reroot1(v,s,now - 2,cnt);
        else reroot1(v,s,now,cnt);
    }
}

void reroot2(int s,int p,ll now){
    mx2[s] = now;
    if(a[s]) mx2[s] = max(mx2[s],0LL);
    vector<pair<ll,int>> vec;
    for(int v:G[s]){
        if(v==p) continue;
        vec.push_back({mx1[v],v});
    }
    sort(vec.begin(),vec.end());
    if(vec.size()==0) return;
    if(vec.size()==1){
        for(int v:G[s]){
            if(v==p) continue;
            reroot2(v,s,mx2[s] + 1);
        }
    }else{
        for(int v:G[s]){
            if(v==p) continue;
            if(v==vec.back().second) reroot2(v,s,max(mx2[s] + 1,vec[vec.size() - 2].first + 2));
            else reroot2(v,s,max(mx2[s] + 1,vec.back().first + 2));
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int i,n,k; cin >> n >> k;
    for(i=0;i<n - 1;i++){
        int u,v; cin >> u >> v; u--; v--;
        G[u].push_back(v); G[v].push_back(u);
    }
    for(i=0;i<k;i++){
        int x; cin >> x; x--; a[x]++;
    }
    dfs1(0,-1); dfs2(0,-1); dfs3(0,-1);
    init--; init *= 2;
    reroot1(0,-1,init,k);
    reroot2(0,-1,-1000000000);
    /*for(i=0;i<n;i++) cout << ans[i] << " ";
    cout << endl;
    for(i=0;i<n;i++) cout << mx1[i] << " ";
    cout << endl;
    for(i=0;i<n;i++) cout << mx2[i] << " ";
    cout << endl;*/
    for(i=0;i<n;i++) ans[i] -= max(mx1[i],mx2[i]);
    for(i=0;i<n;i++) cout << ans[i] << "\n";
}
0