Submission #600388

# Submission time Handle Problem Language Result Execution time Memory
600388 2022-07-20T19:41:45 Z MilosMilutinovic Village (BOI20_village) C++14
100 / 100
98 ms 19024 KB
/**
 *    author:  wxhtzdy
 *    created: 20.07.2022 21:10:18
**/

/*
https://codeforces.com/contest/1387/submission/92450554
https://codeforces.com/contest/1387/submission/137586993
*/

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u; --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<pair<long long, vector<int>>> ans;
  {
    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    int cnt = 0;
    function<void(int, int)> Dfs = [&](int v, int pr) {
      for (int u : g[v]) {
        if (u != pr) {
          if (p[u] == u) {
            Dfs(u, v);
            if (p[u] == u) {
              cnt += 2;
              swap(p[v], p[u]);           
            }
          }
        }
      }
    };
    Dfs(0, 0);
    if (p[0] == 0) {
      cnt += 2;
      swap(p[0], p[g[0][0]]);
    }
    ans.emplace_back(cnt, p);  
  }
  {       
    int root;
    vector<int> sz(n); 
    function<void(int, int)> Dfs = [&](int v, int pr) {
      sz[v] = 1;
      int mx = 0;
      for (int u : g[v]) {
        if (u != pr) {
          Dfs(u, v);
          sz[v] += sz[u];
          mx = max(mx, sz[u]);
        }
      }
      if (mx * 2 < n && sz[v] * 2 >= n) {
        root = v;
      }
    };
    Dfs(0, 0);
    long long cnt = 0;
    vector<int> a;
    Dfs = [&](int v, int pr) {
      a.push_back(v);
      sz[v] = 1;
      for (int u : g[v]) {
        if (u != pr) {
          Dfs(u, v);
          sz[v] += sz[u];
          cnt += sz[u] * 2;
        }
      }      
    };
    Dfs(root, root);
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
      b[a[i]] = a[(i + n / 2) % n]; 
    }
    ans.emplace_back(cnt, b);
  }
  cout << ans[0].first << " " << ans[1].first << '\n';
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < n; i++) {
      cout << ans[j].second[i] + 1 << " \n"[i == n - 1];
    }
  }  
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 56 ms 7836 KB Output is correct
49 Correct 62 ms 8652 KB Output is correct
50 Correct 98 ms 8684 KB Output is correct
51 Correct 45 ms 6768 KB Output is correct
52 Correct 59 ms 8516 KB Output is correct
53 Correct 57 ms 7688 KB Output is correct
54 Correct 31 ms 9284 KB Output is correct
55 Correct 83 ms 19024 KB Output is correct
56 Correct 96 ms 13492 KB Output is correct
57 Correct 72 ms 11792 KB Output is correct
58 Correct 77 ms 10336 KB Output is correct
59 Correct 61 ms 8764 KB Output is correct
60 Correct 43 ms 8884 KB Output is correct
61 Correct 56 ms 8968 KB Output is correct
62 Correct 54 ms 9020 KB Output is correct
63 Correct 58 ms 8428 KB Output is correct
64 Correct 79 ms 9052 KB Output is correct
65 Correct 55 ms 9080 KB Output is correct
66 Correct 61 ms 8524 KB Output is correct
67 Correct 34 ms 6688 KB Output is correct
68 Correct 43 ms 7656 KB Output is correct
69 Correct 65 ms 9068 KB Output is correct
70 Correct 63 ms 8512 KB Output is correct
71 Correct 36 ms 6424 KB Output is correct
72 Correct 36 ms 7196 KB Output is correct
73 Correct 69 ms 9120 KB Output is correct
74 Correct 52 ms 8284 KB Output is correct
75 Correct 62 ms 8456 KB Output is correct
76 Correct 59 ms 8488 KB Output is correct
77 Correct 52 ms 8568 KB Output is correct
78 Correct 35 ms 5964 KB Output is correct
79 Correct 57 ms 6852 KB Output is correct
80 Correct 59 ms 8488 KB Output is correct
81 Correct 65 ms 8764 KB Output is correct
82 Correct 51 ms 8844 KB Output is correct