답안 #896163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896163 2023-12-31T23:37:17 Z ShaShi Village (BOI20_village) C++17
50 / 100
60 ms 17740 KB
#include <bits/stdc++.h>
// #define int long long 
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << endl, exit(0);
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
 
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
 
const int MAXN = (int)1e5 + 7;
const int MOD = 998244353;
const int INF = (int)1e9 + 7;
 
 
 
int n, m, t, flag, k, u, v, w, ans, ans2, tmp, tmp2, tmp3, tmp4;
int arr[MAXN], arr2[MAXN], par[MAXN], h[MAXN], sz[MAXN], cent;
int arr3[MAXN], arr4[MAXN];
vector<int> adj[MAXN];

bool DFS(int v, int p=-1) {
    bool res = 0;

    for (int u:adj[v]) {
        if (u != p) res |= DFS(u, v);
    }

    if (res) return 0;

    if (v != 1) {
        swap(arr[v], arr[p]);
        ans++;
        return 1;
    }

    swap(arr[1], arr[adj[1][0]]);
    ans++;
    return 0;
}


void DFSsz(int v, int p=-1) {
    sz[v] = 1;

    for (int u:adj[v]) {
        if (u == p) continue;
        DFSsz(u, v);
        sz[v] += sz[u];
    }
}


int centroid(int tot, int v, int p=-1) {
    for (int u:adj[v]) {
        if (u != p && 2*sz[u] > tot) {
            return centroid(tot, u, v);
        }
    }

    return v;
}



vector<int> vec, vec2;
vector<pii> love;
pii tof;

void DFS2(int v, int p=-1) {
    vec2.pb(v);
    
    for (int u:adj[v]) {
        if (u == p || u == cent) continue;
        DFS2(u, v);
    }
}


void solve() {
    vec.pb(cent);

    tmp4 = 1;

    for (int u:adj[cent]) {
        love.pb(mp(sz[u], u));
    }
    
    sort(all(love), greater<pii>());

    for (auto cur:love) {
        u = cur.S;
        // vec2.clear();
        DFS2(u);

        reverse(all(vec));

        tmp4 += vec2.size();

        while (vec.size() && vec2.size()) {
            if (tof.F == -1) tof = mp(vec.back(), vec2.back());
            swap(arr3[vec.back()], arr3[vec2.back()]);
            vec2.pop_back();
            vec.pop_back();
        }

        while (vec2.size()) {
            vec.pb(vec2.back());
            vec2.pop_back();
        }
    }
}


int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    tof.F = -1;
 
    cin >> n;

    for (int i=1; i<n; i++) {
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);
    }

    // Min 
    for (int i=1; i<=n; i++) arr[i] = i;
    DFS(1);
    for (int i=1; i<=n; i++) arr2[arr[i]] = i;

    // Max
    DFSsz(1);
    cent = centroid(n, 1);
    DFSsz(cent);
    for (int i=1; i<=n; i++) arr3[i] = i;
    solve();

    // cout << "!" << cent << endl;

    for (int i=1; i<=n; i++) if (i != cent) ans2 += sz[i];

    if (tmp4%2) {
        u = vec.back();
        v = tof.F;
        w = tof.S;
        
        swap(arr3[v], arr3[w]);

        arr3[u] = w;
        arr3[v] = u;
        arr3[w] = v;
    }

    cout << 2*ans << " " << 2*ans2 << endl;

    for (int i=1; i<=n; i++) cout << arr2[i] << " ";
    cout << endl;

    for (int i=1; i<=n; i++) arr4[arr3[i]] = i;

    for (int i=1; i<=n; i++) cout << arr4[i] << " ";
    cout << endl;

 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 2 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Partially correct 1 ms 4700 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 4700 KB Partially correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 2 ms 4700 KB Output is correct
4 Partially correct 2 ms 4696 KB Partially correct
5 Partially correct 1 ms 4700 KB Partially correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Partially correct 1 ms 4700 KB Partially correct
10 Correct 1 ms 4700 KB Output is correct
11 Partially correct 1 ms 4700 KB Partially correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Partially correct 1 ms 4812 KB Partially correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4700 KB Output is correct
23 Partially correct 1 ms 4700 KB Partially correct
24 Partially correct 1 ms 4700 KB Partially correct
25 Correct 1 ms 4700 KB Output is correct
26 Partially correct 2 ms 4700 KB Partially correct
27 Correct 2 ms 4696 KB Output is correct
28 Partially correct 2 ms 4908 KB Partially correct
29 Partially correct 1 ms 4700 KB Partially correct
30 Correct 1 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 2 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Partially correct 1 ms 4700 KB Partially correct
18 Partially correct 1 ms 4700 KB Partially correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 2 ms 4700 KB Output is correct
21 Partially correct 2 ms 4696 KB Partially correct
22 Partially correct 1 ms 4700 KB Partially correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Partially correct 1 ms 4700 KB Partially correct
27 Correct 1 ms 4700 KB Output is correct
28 Partially correct 1 ms 4700 KB Partially correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Partially correct 1 ms 4812 KB Partially correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 1 ms 4700 KB Output is correct
35 Correct 1 ms 4700 KB Output is correct
36 Correct 1 ms 4700 KB Output is correct
37 Correct 1 ms 4700 KB Output is correct
38 Correct 1 ms 4700 KB Output is correct
39 Correct 1 ms 4700 KB Output is correct
40 Partially correct 1 ms 4700 KB Partially correct
41 Partially correct 1 ms 4700 KB Partially correct
42 Correct 1 ms 4700 KB Output is correct
43 Partially correct 2 ms 4700 KB Partially correct
44 Correct 2 ms 4696 KB Output is correct
45 Partially correct 2 ms 4908 KB Partially correct
46 Partially correct 1 ms 4700 KB Partially correct
47 Correct 1 ms 4700 KB Output is correct
48 Partially correct 39 ms 9808 KB Partially correct
49 Correct 42 ms 10320 KB Output is correct
50 Correct 44 ms 10324 KB Output is correct
51 Correct 34 ms 9040 KB Output is correct
52 Correct 42 ms 10076 KB Output is correct
53 Partially correct 37 ms 9656 KB Partially correct
54 Correct 25 ms 11100 KB Output is correct
55 Partially correct 60 ms 17740 KB Partially correct
56 Partially correct 53 ms 13940 KB Partially correct
57 Correct 48 ms 12624 KB Output is correct
58 Partially correct 48 ms 11600 KB Partially correct
59 Partially correct 50 ms 10324 KB Partially correct
60 Correct 41 ms 11076 KB Output is correct
61 Correct 40 ms 10700 KB Output is correct
62 Correct 35 ms 10760 KB Output is correct
63 Correct 35 ms 10328 KB Output is correct
64 Correct 36 ms 10320 KB Output is correct
65 Correct 34 ms 10848 KB Output is correct
66 Correct 37 ms 10328 KB Output is correct
67 Correct 28 ms 9044 KB Output is correct
68 Correct 40 ms 9712 KB Output is correct
69 Correct 35 ms 10840 KB Output is correct
70 Correct 32 ms 10320 KB Output is correct
71 Correct 24 ms 8792 KB Output is correct
72 Correct 30 ms 9560 KB Output is correct
73 Correct 34 ms 10844 KB Output is correct
74 Correct 40 ms 10068 KB Output is correct
75 Partially correct 51 ms 10068 KB Partially correct
76 Partially correct 39 ms 10188 KB Partially correct
77 Partially correct 34 ms 10580 KB Partially correct
78 Partially correct 25 ms 8784 KB Partially correct
79 Partially correct 36 ms 9040 KB Partially correct
80 Partially correct 43 ms 10064 KB Partially correct
81 Partially correct 41 ms 10380 KB Partially correct
82 Partially correct 39 ms 10312 KB Partially correct