답안 #946125

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
946125 2024-03-14T10:37:30 Z LucaIlie Village (BOI20_village) C++17
100 / 100
88 ms 16592 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5;
int sizee[MAX_N + 1], depth[MAX_N + 1], pairMinn[MAX_N + 1], pairMaxx[MAX_N + 1];
vector<int> adj[MAX_N + 1];
int n;

int c;
void findCentroid( int u, int p ) {
    int maxSizee = 0;
    sizee[u] = 1;
    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        findCentroid( v, u );
        sizee[u] += sizee[v];
        maxSizee = max( maxSizee, sizee[v] );
    }
    maxSizee = max( maxSizee, n - sizee[u] );
    if ( maxSizee <= n / 2 )
        c = u;
}

void calcSizes( int u, int p ) {
    sizee[u] = 1;
    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        calcSizes( v, u );
        sizee[u] += sizee[v];
    }
}

long long maxSumDist;
vector<int> vert;
void calcDepths( int u, int p ) {
    depth[u] = depth[p] + 1;
    maxSumDist += 2 * depth[u];
    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        calcDepths( v, u );
    }
    vert.push_back( u );
}

void getMaxSumDist() {
    depth[0] = -1;
    calcDepths( c, 0 );
    for ( int i = 0; i < n / 2; i++ ) {
        pairMaxx[vert[i]] = vert[n / 2 + i];
        pairMaxx[vert[n / 2 + i]] = vert[i];
    }

    if ( pairMaxx[c] == 0 ) {
        int a = adj[c][0], b = pairMaxx[a];
        pairMaxx[a] = b;
        pairMaxx[b] = c;
        pairMaxx[c] = a;
    }
}

long long minSumDist;
void dfs( int u, int p ) {
    vector<int> unpaired;
    sizee[u] = 1;
    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        dfs( v, u );
        sizee[u] += sizee[v];
        if ( pairMinn[v] == 0 )
            unpaired.push_back( v );
    }
    if ( unpaired.size() >= 1 ) {
        unpaired.push_back( u );
        for ( int i = 0; i < (int)unpaired.size(); i++ )
            pairMinn[unpaired[i]] = unpaired[(i + 1) % unpaired.size()];
        minSumDist += 2 * (unpaired.size() - 1);
    }
}

void getMinSumDist() {
    minSumDist = 0;
    dfs( c, 0 );
    if ( pairMinn[c] == 0 ) {
        int a = adj[c][0], b = pairMinn[a];
        while ( pairMinn[b] != a )
            b = pairMinn[b];
        pairMinn[b] = c;
        pairMinn[c] = a;
        minSumDist += 2;
    }
}

int main() {
    cin >> n;
    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back( v );
        adj[v].push_back( u );
    }

    findCentroid( 1, 0 );
    getMinSumDist();
    getMaxSumDist();

    cout << minSumDist << " " << maxSumDist << "\n";
    for ( int v = 1; v <= n; v++ )
        cout << pairMinn[v] << " ";
    cout << "\n";
    for ( int v = 1; v <= n; v++ )
        cout << pairMaxx[v] << " ";
    cout << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2804 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2904 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2648 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2804 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2904 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2648 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2908 KB Output is correct
25 Correct 1 ms 2648 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2648 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2648 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2648 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 1 ms 2648 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 2 ms 2652 KB Output is correct
44 Correct 1 ms 2652 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2652 KB Output is correct
48 Correct 65 ms 8648 KB Output is correct
49 Correct 73 ms 9236 KB Output is correct
50 Correct 72 ms 9168 KB Output is correct
51 Correct 56 ms 7880 KB Output is correct
52 Correct 88 ms 9192 KB Output is correct
53 Correct 64 ms 8452 KB Output is correct
54 Correct 43 ms 9348 KB Output is correct
55 Correct 87 ms 16592 KB Output is correct
56 Correct 79 ms 12748 KB Output is correct
57 Correct 82 ms 11632 KB Output is correct
58 Correct 76 ms 10368 KB Output is correct
59 Correct 70 ms 9256 KB Output is correct
60 Correct 60 ms 9924 KB Output is correct
61 Correct 63 ms 9620 KB Output is correct
62 Correct 67 ms 9684 KB Output is correct
63 Correct 75 ms 9424 KB Output is correct
64 Correct 73 ms 9568 KB Output is correct
65 Correct 65 ms 9656 KB Output is correct
66 Correct 63 ms 9164 KB Output is correct
67 Correct 47 ms 7672 KB Output is correct
68 Correct 55 ms 8516 KB Output is correct
69 Correct 63 ms 9616 KB Output is correct
70 Correct 62 ms 9528 KB Output is correct
71 Correct 44 ms 7648 KB Output is correct
72 Correct 51 ms 8248 KB Output is correct
73 Correct 68 ms 9572 KB Output is correct
74 Correct 59 ms 9048 KB Output is correct
75 Correct 77 ms 9108 KB Output is correct
76 Correct 73 ms 8968 KB Output is correct
77 Correct 68 ms 9196 KB Output is correct
78 Correct 73 ms 7116 KB Output is correct
79 Correct 54 ms 7896 KB Output is correct
80 Correct 81 ms 9168 KB Output is correct
81 Correct 74 ms 9416 KB Output is correct
82 Correct 84 ms 9424 KB Output is correct