답안 #932512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932512 2024-02-23T14:32:03 Z a_l_i_r_e_z_a Village (BOI20_village) C++17
100 / 100
69 ms 17352 KB
// In the name of God
// Hope is last to die

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
// #define int long long    
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())

const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;
int n, ans[maxn], res, timer, sz[maxn], in[maxn];
vector<int> adj[maxn];
ll ans__;
vector<int> vec;
 
void dfs(int v = 1, int p = 0){
    ans[v] = v;
    for(auto u: adj[v]) if(u != p) dfs(u, v);
    if(ans[v] == v){
        res += 2;
        if(p) swap(ans[p], ans[v]);
        else swap(ans[v], ans[adj[v][0]]);
    }
}

void get_sz(int v, int p = -1){
    sz[v] = 1;
    for(auto u: adj[v]){
        if(u != p){
            get_sz(u, v);
            sz[v] += sz[u];
        }
    }
}
 
int find_centroid(int v, int p = -1){
    for(auto u: adj[v]) if(u != p && sz[u] * 2 > n) return find_centroid(u, v);
    return v;
}
 
void dfs__(int v, int h = 0, int p = -1){
    in[v] = timer++;
    vec.pb(v);
    ans__ += h;
    for(auto u: adj[v]) if(u != p) dfs__(u, h + 1, v);
}
 
int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
    cin >> n;
    for(int i = 0; i < n - 1; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs();
    get_sz(1);
    int v = find_centroid(1);
    dfs__(v);


    cout << res << ' ';
    cout << ans__ * 2 << '\n';

    for(int i = 1; i <= n; i++) cout << ans[i] << ' ';
    cout << '\n';


    for(int i = 1; i <= n; i++) cout << vec[(in[i] + n / 2) % n] << ' ';
    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 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 1 ms 2648 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 2648 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2648 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 2812 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2820 KB Output is correct
8 Correct 1 ms 2904 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 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 2652 KB Output is correct
14 Correct 2 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 2652 KB Output is correct
20 Correct 1 ms 2848 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 2652 KB Output is correct
25 Correct 1 ms 2648 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 2 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 2820 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 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 1 ms 2648 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 2648 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2648 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 2812 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2820 KB Output is correct
25 Correct 1 ms 2904 KB Output is correct
26 Correct 1 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 2 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 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 2848 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 2652 KB Output is correct
42 Correct 1 ms 2648 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 2 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 2820 KB Output is correct
48 Correct 40 ms 9416 KB Output is correct
49 Correct 43 ms 10036 KB Output is correct
50 Correct 44 ms 9936 KB Output is correct
51 Correct 32 ms 8404 KB Output is correct
52 Correct 46 ms 9940 KB Output is correct
53 Correct 38 ms 9168 KB Output is correct
54 Correct 25 ms 9944 KB Output is correct
55 Correct 69 ms 17352 KB Output is correct
56 Correct 49 ms 13520 KB Output is correct
57 Correct 49 ms 12196 KB Output is correct
58 Correct 44 ms 11220 KB Output is correct
59 Correct 47 ms 10008 KB Output is correct
60 Correct 32 ms 10060 KB Output is correct
61 Correct 34 ms 10396 KB Output is correct
62 Correct 36 ms 10440 KB Output is correct
63 Correct 33 ms 9944 KB Output is correct
64 Correct 38 ms 10204 KB Output is correct
65 Correct 34 ms 10448 KB Output is correct
66 Correct 36 ms 9988 KB Output is correct
67 Correct 30 ms 8404 KB Output is correct
68 Correct 30 ms 9136 KB Output is correct
69 Correct 34 ms 10444 KB Output is correct
70 Correct 32 ms 9932 KB Output is correct
71 Correct 29 ms 8048 KB Output is correct
72 Correct 27 ms 8916 KB Output is correct
73 Correct 33 ms 10484 KB Output is correct
74 Correct 35 ms 9676 KB Output is correct
75 Correct 44 ms 9928 KB Output is correct
76 Correct 46 ms 9928 KB Output is correct
77 Correct 35 ms 9944 KB Output is correct
78 Correct 24 ms 7640 KB Output is correct
79 Correct 28 ms 8404 KB Output is correct
80 Correct 43 ms 9928 KB Output is correct
81 Correct 38 ms 10100 KB Output is correct
82 Correct 33 ms 10376 KB Output is correct