답안 #486835

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486835 2021-11-12T22:50:56 Z julian33 Inspection (POI11_ins) C++14
0 / 100
1372 ms 114188 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cerr<<vars<<" = ";
    string delim="";
    (...,(cerr<<delim<<values,delim=", "));
    cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif

#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);} 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN=1e6+5; //make sure this is right
const int mod=1e9+7;

vector<int> graph[mxN];
ll sub[mxN],sub2[mxN],up[mxN],down[mxN],par[mxN],dp[mxN],dp2[mxN];

void dfs(int at,int p){
    sub[at]=1;
    for(int &i:graph[at]){
        if(i==p) continue;
        par[i]=at;
        dfs(i,at);
        sub[at]+=sub[i];
        dp[at]+=dp[i]+sub[i];
        maxa(down[at],down[i]+1);
    }
}

void dfs2(int at,int p){
    vector<ll> all;
    sub2[at]++;
    for(int &i:graph[at]){
        if(i==p) continue;
        all.pb(down[i]);
        sub2[i]=sub[at]-1-sub[i]+sub2[at];
    }
    sort(all.rbegin(),all.rend());
    for(int &i:graph[at]){
        if(i==p) continue;
        up[i]=up[at]+1;
        if(all[0]!=down[i]) maxa(up[i],all[0]+2);
        else if(sz(all)>1) maxa(up[i],all[1]+2);
        dp2[i]=dp2[at]+sub2[i]+dp[at]-sub[i]-dp[i];
        dfs2(i,at);
    }
}

int main(){
    cin.sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n; cin>>n;
    for(int i=1;i<n;i++){
        int a,b; cin>>a>>b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    dfs(1,-1);
    dfs2(1,-1);
    for(int i=1;i<=n;i++){
        vector<ll> all;
        ll sum=0;
        for(int &j:graph[i]){
            if(j==par[i]) continue;
            all.pb(sub[j]);
            sum+=sub[j];
        }
        ll rem=n-1-sum;
        sum+=rem;
        all.pb(rem);
        sort(all.begin(),all.end());
        ll amt=min(sum/2,sum-all.back());
        sum-=(sum&1);
        if(2*amt!=sum) cout<<-1<<"\n";
        else cout<<2*(dp[i]+dp2[i])-max(up[i],down[i])<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23780 KB Output is correct
2 Incorrect 12 ms 23836 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 23980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 25548 KB Output is correct
2 Incorrect 24 ms 26572 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 27332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 90 ms 32836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 664 ms 68964 KB Output is correct
2 Incorrect 628 ms 92284 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1355 ms 114188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1372 ms 114080 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1345 ms 114064 KB Output isn't correct
2 Halted 0 ms 0 KB -