답안 #415964

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415964 2021-06-01T18:32:17 Z nicolaalexandra Village (BOI20_village) C++14
12 / 100
5 ms 5384 KB
#include <bits/stdc++.h>
#define DIM 100010
#define INF 2000000000
using namespace std;

vector <int> L[DIM],w[DIM],aux,s;
int v[DIM],fth[DIM],sp[DIM],f[DIM],viz[DIM],d[DIM];
int n,i,x,y,sol_min,sol_max,k;


void dfs (int nod, int tata){
    fth[nod] = tata;
    sp[nod] = 1;
    for (auto vecin : L[nod])
        if (vecin != tata){
            dfs (vecin,nod);
            sp[nod] += sp[vecin];
        }

    if (nod != 1 && v[nod] == nod){

        sol_min += 2;
        swap (v[nod],v[fth[nod]]);

    }

    sol_max += 2 * min (sp[nod],n-sp[nod]);

}

int get_centroid (int nod, int tata){

    int maxim = 0, heavy_son = 0;
    for (auto vecin : L[nod]){
        if (vecin == tata)
            continue;
        if (sp[vecin] > maxim)
            maxim = sp[vecin], heavy_son = vecin;
    }

    if (maxim <= n/2 && n - sp[nod] <= n/2)
        return nod;
    else return get_centroid(heavy_son,nod);

}


void dfs2 (int nod, int idx){
    w[idx].push_back(nod);
    viz[nod] = 1;
    for (auto vecin : L[nod])
        if (!viz[vecin])
            dfs2 (vecin,idx);
}

inline int cmp (int a, int b){
    return w[a].size() >= w[b].size();
}

int main (){

    //ifstream cin ("date.in");
    //ofstream cout ("date.out");

    cin>>n;
    for (i=1;i<n;i++){
        cin>>x>>y;
        L[x].push_back(y);
        L[y].push_back(x);
    }

    for (i=1;i<=n;i++)
        v[i] = i;

    dfs (1,0);

    for (i=1;i<=n;i++)
        if (v[i] == i){
            sol_min += 2;
            if (i != 1)
                swap (v[i],v[fth[i]]);
            else swap (v[i],v[L[1][0]]);
        }

    cout<<sol_min<<" "<<sol_max<<"\n";
    for (i=1;i<=n;i++)
        cout<<v[i]<<" ";

    cout<<"\n";

    memset (v,0,sizeof v);
    int centroid = get_centroid(1,0);

    viz[centroid] = 1;
    w[centroid].push_back(centroid);
    d[++k] = centroid;

    int mini = n, poz;
    for (auto vecin : L[centroid]){
        dfs2 (vecin,vecin);

        d[++k] = vecin;
    }


    sort (d+1,d+k+1,cmp);

    for (auto it : w[d[1]])
        s.push_back(it);

    for (i=2;i<=k;i++){
        int nod = d[i];
        while (!s.empty() && !w[nod].empty()){
            v[s.back()] = w[nod].back();
            v[w[nod].back()] = s.back();
            s.pop_back(), w[nod].pop_back();
        }

        while (!w[nod].empty()){
            s.push_back(w[nod].back());
            w[nod].pop_back();
        }
    }

    if (!v[centroid]){
        v[centroid] = centroid;
        swap (v[centroid],v[L[centroid][0]]);
    }

    for (i=1;i<=n;i++)
        cout<<v[i]<<" ";


    return 0;
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:98:9: warning: unused variable 'mini' [-Wunused-variable]
   98 |     int mini = n, poz;
      |         ^~~~
Village.cpp:98:19: warning: unused variable 'poz' [-Wunused-variable]
   98 |     int mini = n, poz;
      |                   ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 5 ms 5380 KB Output is correct
3 Correct 5 ms 5324 KB Output is correct
4 Correct 4 ms 5324 KB Output is correct
5 Correct 5 ms 5324 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 4 ms 5372 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 5 ms 5348 KB Output is correct
12 Correct 4 ms 5324 KB Output is correct
13 Correct 5 ms 5324 KB Output is correct
14 Correct 4 ms 5384 KB Output is correct
15 Correct 5 ms 5324 KB Output is correct
16 Correct 4 ms 5324 KB Output is correct
17 Correct 4 ms 5384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5324 KB Integer parameter [name=vi] equals to 0, violates the range [1, 256]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 5 ms 5380 KB Output is correct
3 Correct 5 ms 5324 KB Output is correct
4 Correct 4 ms 5324 KB Output is correct
5 Correct 5 ms 5324 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 4 ms 5372 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 5 ms 5348 KB Output is correct
12 Correct 4 ms 5324 KB Output is correct
13 Correct 5 ms 5324 KB Output is correct
14 Correct 4 ms 5384 KB Output is correct
15 Correct 5 ms 5324 KB Output is correct
16 Correct 4 ms 5324 KB Output is correct
17 Correct 4 ms 5384 KB Output is correct
18 Incorrect 4 ms 5324 KB Integer parameter [name=vi] equals to 0, violates the range [1, 256]
19 Halted 0 ms 0 KB -