답안 #415948

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415948 2021-06-01T17:58:11 Z nicolaalexandra Village (BOI20_village) C++14
0 / 100
700 ms 5324 KB
#include <bits/stdc++.h>
#define DIM 100010
#define INF 2000000000
using namespace std;

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


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){
    s.insert(nod);
    w[idx].push_back(nod);
    viz[nod] = 1;
    for (auto vecin : L[nod])
        if (!viz[vecin])
            dfs2 (vecin,idx);
}

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;
    int mini = n, poz;
    for (auto vecin : L[centroid]){
        dfs2 (vecin,vecin);
        if (w[vecin].size() < mini)
            mini = w[vecin].size(), poz = vecin;
    }

    w[poz].push_back(centroid);
    s.insert(centroid);
    for (auto vecin : L[centroid]){

        /// mai intai scot din set elemente care se afla in subarborele lui vecin
        aux.clear();
        for (auto it : w[vecin]){
            if (s.find(it) != s.end())
                aux.push_back(it); /// dupa trb sa le adaug la loc
            s.erase(it);
        }

        for (auto it : w[vecin]){
            v[it] = *s.begin();
            s.erase(s.begin());
        }


        for (auto it : aux)
            s.insert(it);
    }

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


    return 0;
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:96:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   96 |         if (w[vecin].size() < mini)
      |             ~~~~~~~~~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Execution timed out 1094 ms 5324 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1080 ms 5324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Execution timed out 1094 ms 5324 KB Time limit exceeded
3 Halted 0 ms 0 KB -