답안 #719086

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719086 2023-04-05T10:23:51 Z Sam_a17 Village (BOI20_village) C++17
100 / 100
140 ms 38016 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt(x) __builtin_popcount(x)
 
#define pb push_back
#define popf pop_front
#define popb pop_back
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const long long N = 2e5 + 10, inf = 1e8;
vector<int> adj[N];
long long dp[N][2], each[N], pat[N];
vector<pair<int, int>> dpp[N][2];
long long n, max_ans;
long long sz[N];

void dfs(int node, int parent) {
  int c = 0;
  sz[node] = 1;
  for(auto i: adj[node]) {
    if(i == parent) continue;
    dfs(i, node);
    sz[node] += sz[i];
    c++;
  }

  max_ans += 2 * min(sz[node], n - sz[node]);

  if(!c) {
    dp[node][0] = 0;
    dp[node][1] = inf;
  } else {

    long long s = 0;
    for(auto i: adj[node]) {
      if(i == parent) continue;
      dp[node][0] += dp[i][1];
      dp[node][0] = min(dp[node][0], inf);

      s += min(dp[i][1], dp[i][0] + 2);
      if(dp[i][0] + 2 <= dp[i][1]) {
        dpp[node][1].push_back({i, 1});
      } else {
        dpp[node][1].push_back({i, 0});
      }
    }

    long long sx = INT32_MAX;
    int it = 0;

    pair<int, int> pp;
    for(auto i: adj[node]) {
      if(i == parent) continue;

      if(s - min(dp[i][1], dp[i][0] + 2) + min(dp[i][1] + 2, dp[i][0] + 2) < sx) {
        sx = s - min(dp[i][1], dp[i][0] + 2) + min(dp[i][1] + 2, dp[i][0] + 2);
        pp = {i, 1};
      }
      it++;
    }

    for(auto &i: dpp[node][1]) {
      if(i.first == pp.first) {
        i.second = 1;
      }
    }
    dp[node][1] = sx;
  } 
}

void gen_edges(int node, int state, int parent) {
  if(state == 0) {
    for(auto i: adj[node]) {
      if(i == parent) continue;
      gen_edges(i, 1, node);
    }
  } else {
    int cnt = 0;
    for(auto i: dpp[node][1]) {
      if(i.second == 1) {
        cnt++;
        swap(each[node], each[i.first]);
        if(dp[i.first][0] > dp[i.first][1]) {
          gen_edges(i.first, 1, node);
        } else {
          gen_edges(i.first, 0, node);
        }
      } else {
        gen_edges(i.first, 1, node);
      }
    }

    assert(cnt >= 1);
  }
}

int d[101][101];

void dist (int a, int b) {
  queue<int> q;
  vector<int> dist(n + 1);
  dist[a] = 1;
  q.push(a);

  while(!q.empty()) {
    auto u = q.front();
    q.pop();
    for(auto i: adj[u]) {
      if(dist[i]) continue;
      dist[i] = dist[u] + 1;
      q.push(i);
    }
  }

  for(int i = 1; i <= n; i++) {
    d[a][i] = dist[i] - 1;
  }
}

int dfsik(int node, int parent) {
  sz[node] = 1;
  for(auto i: adj[node]) {
    if(i == parent) continue;
    sz[node] += dfsik(i, node);
  }
  return sz[node];
}
 
int find_center(int node, int parent) {
  for(auto i: adj[node]) {
    if (i == parent) continue;
    if (2 * sz[i] > n) {
      return find_center(i, node);
    }
  }
  return node;
}
 
int centroid(int u) {
  dfsik(u, 0);
  u = find_center(u, 0);
  return u;
}

vector<int> path;
void dfs_order(int node, int parent) {
  path.push_back(node);
  for(auto i: adj[node]) {
    if(i == parent) continue;
    dfs_order(i, node);
  }
}
 

void solve_() {
  cin >> n;

  vector<pair<int, int>> edges;
  for(int i = 1; i <= n - 1; i++) {
    int a, b; cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
    edges.push_back({a, b});
  }

  dfs(1, 0);

  for(int i = 1; i <= n; i++) {
    each[i] = i;
  }
  gen_edges(1, 1, 0);

  for(int i = 1; i <= n; i++) {
    pat[each[i]] = i;
    assert(each[i] != i);
  }

  vector<int> pte;
  int bans = 0;

  int cen = centroid(1);
  dfs_order(cen, -1);

  vector<int> which(n + 1);

  for(int i = 0; i < n; i++) {
    which[path[i]] = path[(i + n / 2) % n];
  }

  cout << dp[1][1] << " " << max_ans << '\n';
  for(int i = 1; i <= n; i++) {
    cout << pat[i] << " ";
  }
  cout << '\n';
  for(int i = 1; i <= n; i++) {
    cout << which[i] << " ";
  }
  cout << '\n';
}
 
int main() {
  setIO();
 
  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1;
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
} 

Compilation message

Village.cpp: In function 'void solve_()':
Village.cpp:239:7: warning: unused variable 'bans' [-Wunused-variable]
  239 |   int bans = 0;
      |       ^~~~
Village.cpp: In function 'void setIO(std::string)':
Village.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Village.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14356 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14348 KB Output is correct
6 Correct 9 ms 14384 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 9 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14376 KB Output is correct
13 Correct 9 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14432 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14428 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 8 ms 14460 KB Output is correct
6 Correct 9 ms 14548 KB Output is correct
7 Correct 8 ms 14548 KB Output is correct
8 Correct 10 ms 14516 KB Output is correct
9 Correct 8 ms 14548 KB Output is correct
10 Correct 8 ms 14548 KB Output is correct
11 Correct 8 ms 14548 KB Output is correct
12 Correct 8 ms 14548 KB Output is correct
13 Correct 8 ms 14548 KB Output is correct
14 Correct 9 ms 14560 KB Output is correct
15 Correct 10 ms 14448 KB Output is correct
16 Correct 8 ms 14548 KB Output is correct
17 Correct 8 ms 14552 KB Output is correct
18 Correct 8 ms 14536 KB Output is correct
19 Correct 8 ms 14548 KB Output is correct
20 Correct 8 ms 14492 KB Output is correct
21 Correct 8 ms 14548 KB Output is correct
22 Correct 9 ms 14548 KB Output is correct
23 Correct 9 ms 14548 KB Output is correct
24 Correct 7 ms 14524 KB Output is correct
25 Correct 8 ms 14396 KB Output is correct
26 Correct 10 ms 14508 KB Output is correct
27 Correct 7 ms 14420 KB Output is correct
28 Correct 10 ms 14496 KB Output is correct
29 Correct 8 ms 14548 KB Output is correct
30 Correct 8 ms 14496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14356 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14348 KB Output is correct
6 Correct 9 ms 14384 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 9 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14376 KB Output is correct
13 Correct 9 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14432 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14428 KB Output is correct
19 Correct 9 ms 14420 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 8 ms 14548 KB Output is correct
22 Correct 8 ms 14460 KB Output is correct
23 Correct 9 ms 14548 KB Output is correct
24 Correct 8 ms 14548 KB Output is correct
25 Correct 10 ms 14516 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 8 ms 14548 KB Output is correct
28 Correct 8 ms 14548 KB Output is correct
29 Correct 8 ms 14548 KB Output is correct
30 Correct 8 ms 14548 KB Output is correct
31 Correct 9 ms 14560 KB Output is correct
32 Correct 10 ms 14448 KB Output is correct
33 Correct 8 ms 14548 KB Output is correct
34 Correct 8 ms 14552 KB Output is correct
35 Correct 8 ms 14536 KB Output is correct
36 Correct 8 ms 14548 KB Output is correct
37 Correct 8 ms 14492 KB Output is correct
38 Correct 8 ms 14548 KB Output is correct
39 Correct 9 ms 14548 KB Output is correct
40 Correct 9 ms 14548 KB Output is correct
41 Correct 7 ms 14524 KB Output is correct
42 Correct 8 ms 14396 KB Output is correct
43 Correct 10 ms 14508 KB Output is correct
44 Correct 7 ms 14420 KB Output is correct
45 Correct 10 ms 14496 KB Output is correct
46 Correct 8 ms 14548 KB Output is correct
47 Correct 8 ms 14496 KB Output is correct
48 Correct 87 ms 25248 KB Output is correct
49 Correct 96 ms 26304 KB Output is correct
50 Correct 93 ms 26252 KB Output is correct
51 Correct 66 ms 23740 KB Output is correct
52 Correct 100 ms 26104 KB Output is correct
53 Correct 82 ms 25000 KB Output is correct
54 Correct 53 ms 25832 KB Output is correct
55 Correct 140 ms 38016 KB Output is correct
56 Correct 108 ms 31632 KB Output is correct
57 Correct 133 ms 29760 KB Output is correct
58 Correct 126 ms 28260 KB Output is correct
59 Correct 99 ms 26248 KB Output is correct
60 Correct 57 ms 25508 KB Output is correct
61 Correct 68 ms 26080 KB Output is correct
62 Correct 76 ms 25748 KB Output is correct
63 Correct 59 ms 25028 KB Output is correct
64 Correct 78 ms 26000 KB Output is correct
65 Correct 70 ms 26016 KB Output is correct
66 Correct 61 ms 25236 KB Output is correct
67 Correct 49 ms 22844 KB Output is correct
68 Correct 54 ms 24256 KB Output is correct
69 Correct 69 ms 26048 KB Output is correct
70 Correct 61 ms 25296 KB Output is correct
71 Correct 45 ms 22592 KB Output is correct
72 Correct 50 ms 23616 KB Output is correct
73 Correct 63 ms 26156 KB Output is correct
74 Correct 67 ms 25104 KB Output is correct
75 Correct 94 ms 25788 KB Output is correct
76 Correct 78 ms 25880 KB Output is correct
77 Correct 70 ms 25752 KB Output is correct
78 Correct 47 ms 22156 KB Output is correct
79 Correct 56 ms 23216 KB Output is correct
80 Correct 114 ms 25904 KB Output is correct
81 Correct 96 ms 25872 KB Output is correct
82 Correct 73 ms 25480 KB Output is correct