Submission #971993

# Submission time Handle Problem Language Result Execution time Memory
971993 2024-04-29T16:29:56 Z jadai007 Village (BOI20_village) C++17
0 / 100
2 ms 2652 KB
#include<bits/stdc++.h>

using namespace std;

int n, dp[100100], vln[100100], vlx[100100], ansn, ansx, cen, depth[100100];
vector<int> vc[100100], ans;

void dfs(int u, int pa){
    for(auto v:vc[u]){
        if(v == pa) continue;
        dfs(v, u);
        if(vln[v] == v){
            ansn+=2;
            swap(vln[v], vln[u]);
        }
        dp[u]+=dp[v];
    }
}

void dfs2(int u, int pa){
    ans.emplace_back(u);
    for(auto v:vc[u]){
        if(v == pa) continue;
        depth[v] = depth[u] + 1;
        dfs2(v, u);
    }
}

int centroid(int u, int pa){
    for(auto v:vc[u]){
        if(v == pa) continue;
        if(dp[v] > n / 2) return centroid(v, u);
    }
    return u;
}

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n;
    for(int i = 1; i<=n; ++i){
        vln[i] = i; vlx[i] = i;
        dp[i] = 1;
    }
    for(int i = 1; i<n; ++i){
        int a, b; cin >> a >> b;
        vc[a].push_back(b);
        vc[b].push_back(a);
    }
    dfs(1, -1);
    if(vln[1] == 1){
        ansn+=2;
        swap(vln[1], vln[vc[1][0]]);
    }
    cout << ansn << '\n';
    for(int i = 1; i<=n; ++i) cout << vln[i] << ' ';
    cout << '\n';
    cen = centroid(1, -1);
    dfs2(cen, -1);
    for(int i = 1; i<=n; ++i) ansx+=depth[i]*2;
    cout << ansx << '\n';
    for(int i = 0; i<n; ++i) vlx[ans[(i+n/2)%n]] = ans[i];
    for(int i = 1; i<=n; ++i) cout << vlx[i] << ' ';
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2648 KB Integer parameter [name=vi] equals to 8, violates the range [1, 4]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2652 KB Integer parameter [name=vi] equals to 2186, violates the range [1, 256]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2648 KB Integer parameter [name=vi] equals to 8, violates the range [1, 4]
2 Halted 0 ms 0 KB -