Submission #951646

# Submission time Handle Problem Language Result Execution time Memory
951646 2024-03-22T08:41:25 Z temmieowo Village (BOI20_village) C++14
31 / 100
26 ms 16808 KB
#include <bits/stdc++.h>
#define int long long
#define fastio ios::sync_with_stdio(0), cin.tie(0);
using namespace std;

const int MAX_N = 1000+10;
const int INF = 2e18;

int n;
int a, b;
vector<vector<int>> G(MAX_N);
vector<vector<int>> dis(MAX_N, vector<int>(MAX_N));
bitset<MAX_N> vis;

int total = 0;
int ma = -INF, mi = INF;
vector<int> ma_ans(MAX_N, -1), mi_ans(MAX_N, -1);

void dfs(int start, int now, int pre){
    if (pre!=-1) dis[start][now] = dis[start][pre]+1;

    for (auto x : G[now]){
        if (x!=pre){
            dfs(start, x, now);
        }
    }
    return;
}

pair<int, int> dfs2(int now, int pre){ // <最遠點距離, 最遠點>
    pair<int, int> ret = {0, now};

    for (auto x : G[now]){
        if (x!=pre && vis[x]==0){
            pair<int, int> res = dfs2(x, now);
            ret = max(ret, res);
        }
    }

    ret.first++;
    return ret;
}

void dfs3(int now, int pre){
    // cerr << now << endl;
    for (auto x : G[now]){
        if (x!=pre){
            dfs3(x, now);
        }
    }

    if (mi_ans[now]==-1){
        if (pre!=-1){
            mi_ans[now] = (mi_ans[pre]==-1 ? pre : mi_ans[pre]);
            mi_ans[pre] = now;
        }else{
            mi_ans[now] = mi_ans[G[now].back()];
            mi_ans[G[now].back()] = now;
        }
        total += 2;
    }
    return;
}

void solve1(){
 
    // input
    cin >> n;
    for (int i=0 ; i<n-1 ; i++){
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
 
    // init
    vector<int> v;
    for (int i=0 ; i<n ; i++){
        v.push_back(i);
        dfs(i, i, -1);
    }
    // for (int i=0 ; i<n ; i++){
    //     for (int j=0 ; j<n ; j++){
    //         cerr << dis[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    // return;
 
    // process
    int ma = -INF, mi = INF;
    vector<int> ma_ans, mi_ans;
    do{
        int total = 0;
        for (int i=0 ; i<n ; i++){
            if (i==v[i]) goto flag;
            total += dis[i][v[i]];
        }
 
        if (total>ma){
            ma = total;
            ma_ans = v;
        }
        if (total<mi){
            mi = total;
            mi_ans = v;
        }
 
        flag:;
    } while (next_permutation(v.begin(), v.end()));
 
    cout << mi << " " << ma << "\n";
    for (auto x : mi_ans){
        cout << x+1 << " ";
    }
    cout << "\n";
    for (auto x : ma_ans){
        cout << x+1 << " ";
    }
    cout << "\n";
 
    return;
}

void solve2(){

    // input
    cin >> n;
    for (int i=0 ; i<n-1 ; i++){
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    // 預處理距離
    for (int i=0 ; i<n ; i++){
        dfs(i, i, -1);
    }

    // 找到最長的方式
    total = 0;
    pair<int, int> aa, bb;
    for (int i=0 ; i<n/2 ; i++){
        for (int j=0 ; j<n ; j++){
            if (vis[j]==0){
                aa = dfs2(j, -1);
                bb = dfs2(aa.second, -1);

                vis[aa.second] = 1;
                vis[bb.second] = 1;
                total += (bb.first-1)*2;
                cerr << "樹直徑:" << aa.second+1 << " & " << bb.second+1 << " 貢獻:" << (bb.first-1)*2 << endl;
                ma_ans[aa.second] = bb.second;
                ma_ans[bb.second] = aa.second;
                break;
            }
        }
    }
    if (n%2==1){
        for (int i=0 ; i<n ; i++){
            if (ma_ans[i]==-1){
                ma_ans[i] = ma_ans[aa.second];
                ma_ans[aa.second] = i;
            }
        }
    }
    ma = total;

    // 找到最短的方法
    total = 0;
    dfs3(0, -1);
    mi = total;

    // output
    vis = 0;
    cout << mi << " " << ma << "\n";
    for (int i=0 ; i<n ; i++){
        assert(mi_ans[i] != -1);
        assert(mi_ans[i] != i);
        assert(vis[mi_ans[i]]==0);
        vis[mi_ans[i]] = 1;
        cout << mi_ans[i]+1 << " ";
    }
    cout << "\n";

    total = 0;
    vis = 0;
    for (int i=0 ; i<n ; i++){
        assert(ma_ans[i] != -1);
        assert(ma_ans[i] != i);
        assert(vis[ma_ans[i]]==0);
        vis[ma_ans[i]] = 1;
        total += dis[i][ma_ans[i]];
        cout << ma_ans[i]+1 << " ";
    }
    assert(total==ma);
    cout << "\n";

    return;
}

signed main(){

    fastio;

    int t = 1;
    while (t--){
        solve2();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8284 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 5 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8284 KB Output is correct
6 Correct 4 ms 8284 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Correct 4 ms 8280 KB Output is correct
10 Correct 5 ms 8280 KB Output is correct
11 Correct 4 ms 8284 KB Output is correct
12 Correct 4 ms 8288 KB Output is correct
13 Correct 5 ms 8536 KB Output is correct
14 Correct 5 ms 8284 KB Output is correct
15 Correct 4 ms 8280 KB Output is correct
16 Correct 4 ms 8284 KB Output is correct
17 Correct 4 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8540 KB Output is correct
2 Correct 12 ms 8540 KB Output is correct
3 Correct 10 ms 8536 KB Output is correct
4 Partially correct 20 ms 8540 KB Partially correct
5 Correct 19 ms 8536 KB Output is correct
6 Correct 20 ms 8560 KB Output is correct
7 Correct 23 ms 8540 KB Output is correct
8 Correct 22 ms 8576 KB Output is correct
9 Correct 18 ms 8540 KB Output is correct
10 Correct 26 ms 8540 KB Output is correct
11 Correct 18 ms 8540 KB Output is correct
12 Correct 16 ms 8536 KB Output is correct
13 Correct 23 ms 8540 KB Output is correct
14 Correct 18 ms 8540 KB Output is correct
15 Correct 16 ms 8536 KB Output is correct
16 Correct 24 ms 8540 KB Output is correct
17 Correct 24 ms 8536 KB Output is correct
18 Correct 18 ms 8540 KB Output is correct
19 Correct 18 ms 8540 KB Output is correct
20 Correct 20 ms 8564 KB Output is correct
21 Correct 18 ms 8564 KB Output is correct
22 Correct 18 ms 8536 KB Output is correct
23 Correct 17 ms 8536 KB Output is correct
24 Correct 17 ms 8552 KB Output is correct
25 Correct 9 ms 8540 KB Output is correct
26 Correct 21 ms 8540 KB Output is correct
27 Correct 10 ms 8540 KB Output is correct
28 Correct 18 ms 8540 KB Output is correct
29 Correct 19 ms 8540 KB Output is correct
30 Correct 24 ms 8836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8284 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 5 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8284 KB Output is correct
6 Correct 4 ms 8284 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Correct 4 ms 8280 KB Output is correct
10 Correct 5 ms 8280 KB Output is correct
11 Correct 4 ms 8284 KB Output is correct
12 Correct 4 ms 8288 KB Output is correct
13 Correct 5 ms 8536 KB Output is correct
14 Correct 5 ms 8284 KB Output is correct
15 Correct 4 ms 8280 KB Output is correct
16 Correct 4 ms 8284 KB Output is correct
17 Correct 4 ms 8284 KB Output is correct
18 Correct 8 ms 8540 KB Output is correct
19 Correct 12 ms 8540 KB Output is correct
20 Correct 10 ms 8536 KB Output is correct
21 Partially correct 20 ms 8540 KB Partially correct
22 Correct 19 ms 8536 KB Output is correct
23 Correct 20 ms 8560 KB Output is correct
24 Correct 23 ms 8540 KB Output is correct
25 Correct 22 ms 8576 KB Output is correct
26 Correct 18 ms 8540 KB Output is correct
27 Correct 26 ms 8540 KB Output is correct
28 Correct 18 ms 8540 KB Output is correct
29 Correct 16 ms 8536 KB Output is correct
30 Correct 23 ms 8540 KB Output is correct
31 Correct 18 ms 8540 KB Output is correct
32 Correct 16 ms 8536 KB Output is correct
33 Correct 24 ms 8540 KB Output is correct
34 Correct 24 ms 8536 KB Output is correct
35 Correct 18 ms 8540 KB Output is correct
36 Correct 18 ms 8540 KB Output is correct
37 Correct 20 ms 8564 KB Output is correct
38 Correct 18 ms 8564 KB Output is correct
39 Correct 18 ms 8536 KB Output is correct
40 Correct 17 ms 8536 KB Output is correct
41 Correct 17 ms 8552 KB Output is correct
42 Correct 9 ms 8540 KB Output is correct
43 Correct 21 ms 8540 KB Output is correct
44 Correct 10 ms 8540 KB Output is correct
45 Correct 18 ms 8540 KB Output is correct
46 Correct 19 ms 8540 KB Output is correct
47 Correct 24 ms 8836 KB Output is correct
48 Runtime error 12 ms 16808 KB Execution killed with signal 11
49 Halted 0 ms 0 KB -