답안 #447885

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447885 2021-07-28T00:39:41 Z Ozy Village (BOI20_village) C++17
100 / 100
111 ms 25452 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "

#define INF 1000000000000
#define MAX 100000

lli n,a,b,M,cent, summin;
lli nh[MAX+2],des[MAX+2],dir[MAX+2],desmin[MAX+2];
vector<lli> hijos[MAX+2],orden,nuevo,grande;
lli dp[2][MAX+2];

lli Find(lli pos, lli padre,lli mitad) {

    lli grande = -1;
    for(auto h : hijos[pos]) {
        if (padre == h) continue;
        if (nh[h] > mitad) grande = h;
    }

    if (grande == -1) return pos;
    else return Find(grande,pos,mitad);

}

void DFS(lli pos, lli padre) {

    nh[pos] = 1;
    vector<lli> unos;
    lli MIN = INF;
    lli nodo;
    lli sum = 0;

    for (auto h : hijos[pos]) {
        if (h == padre) continue;

        DFS(h,pos);
        nh[pos] += nh[h];

        sum += min(dp[0][h], dp[1][h]);
        if (dp[0][h] == INF || desmin[h] == h){
            swap(desmin[h], desmin[pos]);
            summin += 2;
        }
        nodo = h;

    }

    if (nh[pos] == 1) {dp[1][pos]=2;dp[0][pos]=INF;}
    else {
        dp[0][pos] = sum;
        dp[1][pos] = sum+2;
        if (desmin[pos] == pos && pos == 1){
            swap(desmin[nodo], desmin[pos]);
            summin += 2;
            dp[0][pos] += 2;
        }
    }

    lli x = min(nh[pos],n-nh[pos]);
    M += x*2;
}

void agrega(lli pos, lli padre) {
    nuevo.push_back(pos);
    for (auto h : hijos[pos]) if (h != padre) agrega(h,pos);
}

void solve() {

    lli menor,mayor;
    menor = mayor = -1;
    orden.push_back(cent);

    for (auto h : hijos[cent]) {
        nuevo.clear();
        agrega(h,cent);

        if (menor == -1 || nuevo.size() < menor) menor = nuevo.size();
        if (mayor == -1 || nuevo.size() > mayor) {
            swap(nuevo,grande);
            mayor = grande.size();
        }

        if (nuevo.empty()) continue;
        for (auto act : nuevo) orden.push_back(act);
    }

    for (auto act : grande) orden.push_back(act);
    lli cont = 0;

    rep(i,orden.size()-mayor,orden.size()-1) des[orden[i]] = orden[cont++];
    rep(i,0,orden.size()-mayor-1) des[orden[i]] = orden[cont++];
}

void swapea(lli pos, lli padre, lli actual) {

    lli nodo,MIN = INF;
    vector<lli> unos;

    if (pos != 1 && hijos[pos].size() == 1) return;

    for (auto h : hijos[pos]) {
        if (h == padre) continue;

        if (dp[1][h] <= dp[0][h]) {unos.push_back(h); swapea(h,pos,1);}
        else if (dp[1][h]-dp[0][h] < MIN) {MIN = dp[1][h]-dp[0][h]; nodo = h; swapea(h,pos,0);}
    }

    if (actual == 0 && unos.empty()) {unos.push_back(nodo);}
    unos.push_back(pos);
    if (unos.size() == 1) return;

    rep(i,0,unos.size()-2) swap(dir[unos[i]],dir[unos[i+1]]);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    rep(i,1,n-1) {
        cin >> a >> b;
        hijos[a].push_back(b);
        hijos[b].push_back(a);
        dir[i] = i;
    }
    dir[n] = n;
    rep(i, 1, n) desmin[i] = i;
    rep(i, 1, n) dp[0][i] = INF;

    DFS(1,0);
    cent = Find(1,0,n/2);

    solve();
    ///swapea(1,0,0);

    cout << summin << ' '<< M <<endl;
    rep(i,1,n) cout << desmin[i] << ' ';
    cout << "\n";
    rep(i,1,n) cout << des[i] << ' ';

}

Compilation message

Village.cpp: In function 'void DFS(long long int, long long int)':
Village.cpp:35:9: warning: unused variable 'MIN' [-Wunused-variable]
   35 |     lli MIN = INF;
      |         ^~~
Village.cpp: In function 'void solve()':
Village.cpp:84:41: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   84 |         if (menor == -1 || nuevo.size() < menor) menor = nuevo.size();
      |                            ~~~~~~~~~~~~~^~~~~~~
Village.cpp:85:41: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   85 |         if (mayor == -1 || nuevo.size() > mayor) {
      |                            ~~~~~~~~~~~~~^~~~~~~
Village.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define rep(i,a,b) for (int i = (a); i <= (b); i++)
      |                                        ^
Village.cpp:97:5: note: in expansion of macro 'rep'
   97 |     rep(i,orden.size()-mayor,orden.size()-1) des[orden[i]] = orden[cont++];
      |     ^~~
Village.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
    4 | #define rep(i,a,b) for (int i = (a); i <= (b); i++)
      |                                        ^
Village.cpp:98:5: note: in expansion of macro 'rep'
   98 |     rep(i,0,orden.size()-mayor-1) des[orden[i]] = orden[cont++];
      |     ^~~
Village.cpp: In function 'void swapea(long long int, long long int, long long int)':
Village.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define rep(i,a,b) for (int i = (a); i <= (b); i++)
      |                                        ^
Village.cpp:119:5: note: in expansion of macro 'rep'
  119 |     rep(i,0,unos.size()-2) swap(dir[unos[i]],dir[unos[i+1]]);
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2596 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2636 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 2 ms 2764 KB Output is correct
9 Correct 2 ms 2764 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 2 ms 2764 KB Output is correct
16 Correct 2 ms 2764 KB Output is correct
17 Correct 2 ms 2764 KB Output is correct
18 Correct 2 ms 2764 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2764 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 4 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2596 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 3 ms 2636 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2764 KB Output is correct
25 Correct 2 ms 2764 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 2 ms 2764 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 2 ms 2764 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
36 Correct 2 ms 2764 KB Output is correct
37 Correct 2 ms 2764 KB Output is correct
38 Correct 2 ms 2764 KB Output is correct
39 Correct 2 ms 2764 KB Output is correct
40 Correct 2 ms 2764 KB Output is correct
41 Correct 2 ms 2764 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2764 KB Output is correct
44 Correct 2 ms 2764 KB Output is correct
45 Correct 2 ms 2764 KB Output is correct
46 Correct 2 ms 2764 KB Output is correct
47 Correct 4 ms 2764 KB Output is correct
48 Correct 78 ms 12792 KB Output is correct
49 Correct 89 ms 13508 KB Output is correct
50 Correct 93 ms 13644 KB Output is correct
51 Correct 58 ms 11304 KB Output is correct
52 Correct 79 ms 13432 KB Output is correct
53 Correct 74 ms 12676 KB Output is correct
54 Correct 42 ms 13628 KB Output is correct
55 Correct 107 ms 25452 KB Output is correct
56 Correct 111 ms 19576 KB Output is correct
57 Correct 94 ms 17552 KB Output is correct
58 Correct 94 ms 15928 KB Output is correct
59 Correct 88 ms 14140 KB Output is correct
60 Correct 65 ms 13400 KB Output is correct
61 Correct 67 ms 13692 KB Output is correct
62 Correct 72 ms 14192 KB Output is correct
63 Correct 70 ms 13472 KB Output is correct
64 Correct 81 ms 13772 KB Output is correct
65 Correct 71 ms 14232 KB Output is correct
66 Correct 67 ms 13276 KB Output is correct
67 Correct 46 ms 10984 KB Output is correct
68 Correct 58 ms 12352 KB Output is correct
69 Correct 74 ms 14780 KB Output is correct
70 Correct 65 ms 13256 KB Output is correct
71 Correct 50 ms 10708 KB Output is correct
72 Correct 53 ms 11704 KB Output is correct
73 Correct 73 ms 14264 KB Output is correct
74 Correct 66 ms 13172 KB Output is correct
75 Correct 82 ms 14132 KB Output is correct
76 Correct 77 ms 14008 KB Output is correct
77 Correct 74 ms 13760 KB Output is correct
78 Correct 46 ms 10152 KB Output is correct
79 Correct 53 ms 11288 KB Output is correct
80 Correct 83 ms 14212 KB Output is correct
81 Correct 72 ms 13624 KB Output is correct
82 Correct 79 ms 13604 KB Output is correct