답안 #915193

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
915193 2024-01-23T13:10:47 Z 3as8 Village (BOI20_village) C++14
56 / 100
101 ms 15208 KB
#include <bits/stdc++.h>

#define ll long long
#define endl "\n"
#define fastIO cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false);

#define mid ((l + r) / 2)
#define lChild ((index * 2) + 1)
#define rChild ((index * 2) + 2)

using namespace std;

ll ans = 0;
bool dfs(vector<vector<ll> >& graph, vector<ll>& cur, ll startIndex, ll p) {

    bool hasLeaf = false;
    bool isLeaf = true;
    for(auto el : graph[startIndex]) {
        if(el == p) continue;

        hasLeaf &= dfs(graph, cur, el, startIndex);

        isLeaf = false;

    }

    if(isLeaf && cur[startIndex] == startIndex ) {

        ans += 2;
        //cout<<"swap "<<cur[p] + 1<<" "<<cur[startIndex] + 1<<endl;

        swap(cur[p], cur[startIndex]);

        return isLeaf;
    }

    if(cur[startIndex] == startIndex && p != startIndex) {


        ans += 2;
       // cout<<"swap no leaf "<<cur[p] + 1<<" "<<cur[startIndex] + 1<<endl;

        swap(cur[p], cur[startIndex]);
    }

    return isLeaf;
}


void deez(vector<vector<ll> >& graph, vector<ll>& dist, ll startIndex, ll p, ll depth = 0) {

    dist[startIndex] = depth;
    for(auto el : graph[startIndex]) {
        if(el == p) continue;

        deez(graph, dist, el, startIndex, depth + 1);
    }
}

void solve(ll _) {

    ll n; cin>>n;

    vector<vector<ll> > graph(n);
    for(int i = 0; i < n - 1; i++) {
        ll x, y; cin>>x>>y;
        x--; y--;

        graph[x].push_back(y);
        graph[y].push_back(x);
    }


    if(n <= 10) {

        vector<vector<ll> > dist(n, vector<ll>(n, -1));

        for(int i = 0; i < n; i++) {
            deez(graph, dist[i], i, -1);

        }


        vector<ll> arr(n);
        for(int i = 0; i < n; i++) arr[i] = i;

        vector<ll> mnA, mxA;

        ll mn = LLONG_MAX, mx = LLONG_MIN;
        do {

            bool is = true;
            for(int i = 0; i < n; i++) {
                is &= (arr[i] != i);
            }

            if(!is) continue;

            ll d = 0;
            for(int i = 0; i < n; i++) {
                d += dist[i][arr[i]];
            }

            if(mn > d) {
                mnA = arr;
            }
            if(mx < d) {
                mxA = arr;
            }

            mn = min(mn, d);
            mx = max(mx, d);

        } while(next_permutation(arr.begin(), arr.end()));


        cout<<mn<<" "<<mx<<endl;
        for(auto el : mnA) cout<<el + 1<<" ";
        cout<<endl;

        for(auto el : mxA) cout<<el + 1<<" ";
        cout<<endl;

        return;

    }
    vector<bool> stat(n, true);
    vector<ll> curr(n);
    for(int i = 0; i < n; i++) curr[i] = i;

    dfs(graph, curr, 0, 0);

    for(int i = 0; i < n; i++) {

        if(curr[i] == i) {
            ans += 2;
            swap(curr[i], curr[graph[i][0]]);

        }

    }

    cout<<ans<<" "<<2<<endl;
    for(auto el : curr) cout<<el + 1<<" ";
    cout<<endl;

    for(auto el : curr) cout<<el + 1<<" ";
    cout<<endl;
}

int main() {


    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);

    ll t = 0; solve(t);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 5 ms 348 KB Output is correct
10 Correct 49 ms 432 KB Output is correct
11 Correct 49 ms 348 KB Output is correct
12 Correct 61 ms 344 KB Output is correct
13 Correct 49 ms 344 KB Output is correct
14 Correct 52 ms 344 KB Output is correct
15 Correct 49 ms 348 KB Output is correct
16 Correct 49 ms 348 KB Output is correct
17 Correct 49 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 348 KB Partially correct
2 Partially correct 1 ms 348 KB Partially correct
3 Partially correct 1 ms 348 KB Partially correct
4 Partially correct 1 ms 348 KB Partially correct
5 Partially correct 1 ms 344 KB Partially correct
6 Partially correct 1 ms 600 KB Partially correct
7 Partially correct 1 ms 348 KB Partially correct
8 Partially correct 1 ms 348 KB Partially correct
9 Partially correct 2 ms 348 KB Partially correct
10 Partially correct 1 ms 348 KB Partially correct
11 Partially correct 1 ms 348 KB Partially correct
12 Partially correct 1 ms 348 KB Partially correct
13 Partially correct 1 ms 348 KB Partially correct
14 Partially correct 1 ms 348 KB Partially correct
15 Partially correct 1 ms 348 KB Partially correct
16 Partially correct 1 ms 348 KB Partially correct
17 Partially correct 1 ms 344 KB Partially correct
18 Partially correct 1 ms 344 KB Partially correct
19 Partially correct 1 ms 348 KB Partially correct
20 Partially correct 1 ms 348 KB Partially correct
21 Partially correct 1 ms 348 KB Partially correct
22 Partially correct 1 ms 348 KB Partially correct
23 Partially correct 1 ms 344 KB Partially correct
24 Partially correct 1 ms 344 KB Partially correct
25 Partially correct 1 ms 344 KB Partially correct
26 Partially correct 2 ms 344 KB Partially correct
27 Partially correct 1 ms 348 KB Partially correct
28 Partially correct 2 ms 348 KB Partially correct
29 Partially correct 1 ms 348 KB Partially correct
30 Partially correct 1 ms 348 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 5 ms 348 KB Output is correct
10 Correct 49 ms 432 KB Output is correct
11 Correct 49 ms 348 KB Output is correct
12 Correct 61 ms 344 KB Output is correct
13 Correct 49 ms 344 KB Output is correct
14 Correct 52 ms 344 KB Output is correct
15 Correct 49 ms 348 KB Output is correct
16 Correct 49 ms 348 KB Output is correct
17 Correct 49 ms 344 KB Output is correct
18 Partially correct 1 ms 348 KB Partially correct
19 Partially correct 1 ms 348 KB Partially correct
20 Partially correct 1 ms 348 KB Partially correct
21 Partially correct 1 ms 348 KB Partially correct
22 Partially correct 1 ms 344 KB Partially correct
23 Partially correct 1 ms 600 KB Partially correct
24 Partially correct 1 ms 348 KB Partially correct
25 Partially correct 1 ms 348 KB Partially correct
26 Partially correct 2 ms 348 KB Partially correct
27 Partially correct 1 ms 348 KB Partially correct
28 Partially correct 1 ms 348 KB Partially correct
29 Partially correct 1 ms 348 KB Partially correct
30 Partially correct 1 ms 348 KB Partially correct
31 Partially correct 1 ms 348 KB Partially correct
32 Partially correct 1 ms 348 KB Partially correct
33 Partially correct 1 ms 348 KB Partially correct
34 Partially correct 1 ms 344 KB Partially correct
35 Partially correct 1 ms 344 KB Partially correct
36 Partially correct 1 ms 348 KB Partially correct
37 Partially correct 1 ms 348 KB Partially correct
38 Partially correct 1 ms 348 KB Partially correct
39 Partially correct 1 ms 348 KB Partially correct
40 Partially correct 1 ms 344 KB Partially correct
41 Partially correct 1 ms 344 KB Partially correct
42 Partially correct 1 ms 344 KB Partially correct
43 Partially correct 2 ms 344 KB Partially correct
44 Partially correct 1 ms 348 KB Partially correct
45 Partially correct 2 ms 348 KB Partially correct
46 Partially correct 1 ms 348 KB Partially correct
47 Partially correct 1 ms 348 KB Partially correct
48 Partially correct 60 ms 7688 KB Partially correct
49 Partially correct 67 ms 8284 KB Partially correct
50 Partially correct 73 ms 8376 KB Partially correct
51 Partially correct 61 ms 6628 KB Partially correct
52 Partially correct 72 ms 8276 KB Partially correct
53 Partially correct 62 ms 7460 KB Partially correct
54 Partially correct 34 ms 7508 KB Partially correct
55 Partially correct 77 ms 15208 KB Partially correct
56 Partially correct 101 ms 11856 KB Partially correct
57 Partially correct 68 ms 10576 KB Partially correct
58 Partially correct 68 ms 9552 KB Partially correct
59 Partially correct 75 ms 8652 KB Partially correct
60 Partially correct 61 ms 8648 KB Partially correct
61 Partially correct 60 ms 8508 KB Partially correct
62 Partially correct 62 ms 8724 KB Partially correct
63 Partially correct 59 ms 8276 KB Partially correct
64 Partially correct 72 ms 8764 KB Partially correct
65 Partially correct 68 ms 8936 KB Partially correct
66 Partially correct 58 ms 8272 KB Partially correct
67 Partially correct 45 ms 6348 KB Partially correct
68 Partially correct 53 ms 7608 KB Partially correct
69 Partially correct 62 ms 8948 KB Partially correct
70 Partially correct 62 ms 8272 KB Partially correct
71 Partially correct 42 ms 6236 KB Partially correct
72 Partially correct 48 ms 6992 KB Partially correct
73 Partially correct 61 ms 8944 KB Partially correct
74 Partially correct 60 ms 8276 KB Partially correct
75 Partially correct 66 ms 8788 KB Partially correct
76 Partially correct 70 ms 8272 KB Partially correct
77 Partially correct 67 ms 8784 KB Partially correct
78 Partially correct 45 ms 5972 KB Partially correct
79 Partially correct 49 ms 6732 KB Partially correct
80 Partially correct 81 ms 8784 KB Partially correct
81 Partially correct 62 ms 8784 KB Partially correct
82 Partially correct 74 ms 8620 KB Partially correct