Submission #220201

# Submission time Handle Problem Language Result Execution time Memory
220201 2020-04-07T10:09:59 Z fedoseevtimofey Capital City (JOI20_capital_city) C++14
11 / 100
920 ms 524292 KB
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>

using namespace std;

typedef long long ll;

const int N = 2e5 + 7;
const int M = 6e6 + 7;

vector <int> g[N];
int col[N];

bool used[N];

int dfs(int u, int size, int &c, int p = -1) {
  int sum = 1;
  for (auto v : g[u]) {
    if (!used[v] && v != p) {
      sum += dfs(v, size, c, u);
    }
  }
  if (c == -1 && (2 * sum >= size || p == -1)) {
    c = u;
  }
  return sum;
}

int uk;

vector <int> gr[M];
vector <int> rg[M];

int num[N];
vector <int> who[N];

void add(int u, int v) {
  gr[u].push_back(v);
  rg[v].push_back(u);
}


vector <int> subtr;

void solve(int u, int p = -1) {
  subtr.push_back(u);
  for (auto v : g[u]) {
    if (!used[v] && v != p) {
      num[v] = uk++;
      add(num[v], col[v]);
      add(num[v], num[u]);
      solve(v, u);
    }
  }
}

int timer = 0;
int cur_cnt[N];
int last_seen[N];

void build(int u, int size, int last) {
  int c = -1; 
  dfs(u, size, c);
  used[c] = true;
  num[c] = uk++;
  add(num[c], col[c]);
  vector <vector <int>> cmp;
  for (auto v : g[c]) {
    if (!used[v]) {
      num[v] = uk++;
      add(num[v], col[v]);
      add(num[v], num[c]);

      subtr.clear();
      solve(v, c);
      cmp.push_back(subtr);
    }
  }
  cmp.push_back({c});
  for (auto sb : cmp) {
    ++timer;
    for (auto v : sb) {
      who[col[v]].push_back(v);
      if (last_seen[col[v]] != timer) {
        last_seen[col[v]] = timer;
        ++cur_cnt[col[v]];
      }
    }
  }

  for (auto sb : cmp) {
    for (auto v : sb) {
      if (cur_cnt[col[v]] > 1) {
        for (auto vr : who[col[v]]) {
          add(col[v], num[vr]);
        }
      }
      who[col[v]].clear();
      cur_cnt[col[v]] = 0;
    }
  }

  cmp.clear();

   
  for (auto v : g[c]) {
    if (!used[v]) {
      build(v, (size + 1) / 2, c);
    }
  }
}


vector <int> t;
int cmp[M];
int sz[M];
vector <int> gc[M];
bool hv[M];

void get_topsort(int u) {
  used[u] = true;
  for (auto v : gr[u]) {
    if (!used[v]) {
      get_topsort(v);
    }
  }
  t.push_back(u);
}

int cnt = 0;

int k;

void jhfs(int u) {
  used[u] = true;
  cmp[u] = cnt;
  if (u < k) ++sz[cnt];
  for (auto v : rg[u]) {
    if (!used[v]) {
      jhfs(v);
    }
  }
}

int ans;

void zhfs(int u) {
  used[u] = true;
  for (auto v : gc[u]) {
    if (!used[v]) {
      zhfs(v);
    }
    hv[u] |= hv[v];
  }
  if (!hv[u] && sz[u] > 0) {
    ans = min(ans, sz[u]);
  }
  if (sz[u] > 0) hv[u] = true;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n >> k;
  for (int i = 0; i + 1 < n; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; ++i) {
    cin >> col[i];
    --col[i];
  }

  uk = k;
  build(0, n, -1);
  
 
  fill(used, used + uk, false);
  for (int i = 0; i < uk; ++i) {
    if (!used[i]) {
      get_topsort(i);
    }
  }
  reverse(t.begin(), t.end());
  fill(used, used + uk, false);   
  for (int u : t) {
    if (!used[u]) {
      jhfs(u);
      ++cnt;
    }
  }
  for (int i = 0; i < uk; ++i) {
    for (auto v : gr[i]) {
      if (cmp[i] != cmp[v]) {
        gc[cmp[i]].push_back(cmp[v]);
      }
    }
  }
  for (int i = 0; i < cnt; ++i) {
    sort(gc[i].begin(), gc[i].end());
    gc[i].resize(unique(gc[i].begin(), gc[i].end()) - gc[i].begin());
  }
  ans = n;
  fill(used, used + cnt, 0);
  for (int i = 0; i < cnt; ++i) {
    if (!used[i]) {
      zhfs(i);
    }
  }
  cout << ans - 1 << '\n';
}

# Verdict Execution time Memory Grader output
1 Correct 235 ms 432504 KB Output is correct
2 Correct 251 ms 432548 KB Output is correct
3 Correct 252 ms 432624 KB Output is correct
4 Correct 243 ms 432504 KB Output is correct
5 Correct 249 ms 432632 KB Output is correct
6 Correct 238 ms 432532 KB Output is correct
7 Correct 241 ms 432504 KB Output is correct
8 Correct 254 ms 432608 KB Output is correct
9 Correct 239 ms 432504 KB Output is correct
10 Correct 240 ms 432504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 235 ms 432504 KB Output is correct
2 Correct 251 ms 432548 KB Output is correct
3 Correct 252 ms 432624 KB Output is correct
4 Correct 243 ms 432504 KB Output is correct
5 Correct 249 ms 432632 KB Output is correct
6 Correct 238 ms 432532 KB Output is correct
7 Correct 241 ms 432504 KB Output is correct
8 Correct 254 ms 432608 KB Output is correct
9 Correct 239 ms 432504 KB Output is correct
10 Correct 240 ms 432504 KB Output is correct
11 Correct 249 ms 433912 KB Output is correct
12 Correct 253 ms 433912 KB Output is correct
13 Correct 252 ms 433912 KB Output is correct
14 Correct 253 ms 433912 KB Output is correct
15 Correct 249 ms 434424 KB Output is correct
16 Correct 259 ms 434368 KB Output is correct
17 Correct 246 ms 433784 KB Output is correct
18 Correct 259 ms 433880 KB Output is correct
19 Correct 261 ms 433828 KB Output is correct
20 Correct 249 ms 433784 KB Output is correct
21 Correct 267 ms 433888 KB Output is correct
22 Correct 272 ms 435064 KB Output is correct
23 Correct 260 ms 434808 KB Output is correct
24 Correct 265 ms 434608 KB Output is correct
25 Correct 267 ms 434416 KB Output is correct
26 Correct 257 ms 434372 KB Output is correct
27 Correct 267 ms 434424 KB Output is correct
28 Correct 274 ms 434376 KB Output is correct
29 Correct 281 ms 434440 KB Output is correct
30 Correct 272 ms 434272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 920 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 235 ms 432504 KB Output is correct
2 Correct 251 ms 432548 KB Output is correct
3 Correct 252 ms 432624 KB Output is correct
4 Correct 243 ms 432504 KB Output is correct
5 Correct 249 ms 432632 KB Output is correct
6 Correct 238 ms 432532 KB Output is correct
7 Correct 241 ms 432504 KB Output is correct
8 Correct 254 ms 432608 KB Output is correct
9 Correct 239 ms 432504 KB Output is correct
10 Correct 240 ms 432504 KB Output is correct
11 Correct 249 ms 433912 KB Output is correct
12 Correct 253 ms 433912 KB Output is correct
13 Correct 252 ms 433912 KB Output is correct
14 Correct 253 ms 433912 KB Output is correct
15 Correct 249 ms 434424 KB Output is correct
16 Correct 259 ms 434368 KB Output is correct
17 Correct 246 ms 433784 KB Output is correct
18 Correct 259 ms 433880 KB Output is correct
19 Correct 261 ms 433828 KB Output is correct
20 Correct 249 ms 433784 KB Output is correct
21 Correct 267 ms 433888 KB Output is correct
22 Correct 272 ms 435064 KB Output is correct
23 Correct 260 ms 434808 KB Output is correct
24 Correct 265 ms 434608 KB Output is correct
25 Correct 267 ms 434416 KB Output is correct
26 Correct 257 ms 434372 KB Output is correct
27 Correct 267 ms 434424 KB Output is correct
28 Correct 274 ms 434376 KB Output is correct
29 Correct 281 ms 434440 KB Output is correct
30 Correct 272 ms 434272 KB Output is correct
31 Runtime error 920 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
32 Halted 0 ms 0 KB -