Submission #220207

# Submission time Handle Problem Language Result Execution time Memory
220207 2020-04-07T10:28:43 Z fedoseevtimofey Capital City (JOI20_capital_city) C++14
100 / 100
273 ms 49264 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;
 
vector <int> g[N];
vector <int> gr[N];
vector <int> rg[N];
 
int c[N];
 
vector <int> t;
bool used[N];
int cmp[N];
bool ok[N];
int sz[N];

int bad[N];

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

int tin[N], tout[N];

int timer = 0;

void init(int u, int p) {
  tin[u] = timer++;
  for (auto v : g[u]) {
    if (v != p) {
      init(v, u);
    }
  }
  tout[u] = timer++;
}

bool anc(int a, int b) {
  return tin[a] <= tin[b] && tout[b] <= tout[a];
}

void dfs(int u, int p = -1) {
  if (u != bad[c[u]]) {
    add(c[u], c[p]);
  }
  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
}
 
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;
 
void jhfs(int u) {
  used[u] = true;
  cmp[u] = cnt;
  ++sz[cnt];
  for (auto v : rg[u]) {
    if (!used[v]) {
      jhfs(v);
    }
  }
}
 
int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n, k;
  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);
  }
  vector <vector <int>> who(k);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
    who[c[i]].push_back(i);
  }
  init(0, -1);

  for (int i = 0; i < k; ++i) {
    int v = who[i].front();
    for (auto w : who[i]) {
      if (anc(w, v)) v = w;
    }
    bool ok = true;
    for (auto w : who[i]) {
      ok &= anc(v, w);
    }
    if (ok) bad[i] = v;
    else bad[i] = -1;
  }
  dfs(0);
  for (int i = 0; i < k; ++i) {
    if (!used[i]) {
      get_topsort(i);
    }
  }
  
  reverse(t.begin(), t.end());
  fill(used, used + k, false);   
  for (int u : t) {
    if (!used[u]) {
      jhfs(u);
      ++cnt;
    }
  }
  fill(ok, ok + cnt, true);
  for (int i = 0; i < k; ++i) {
    for (int j : gr[i]) {
      if (cmp[i] != cmp[j]) {
        ok[cmp[i]] = false;
      }
    }
  }
  int ans = n;
  for (int i = 0; i < cnt; ++i) {
    if (ok[i]) ans = min(ans, sz[i]);
  }
  cout << ans - 1 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 15 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 15 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 14 ms 14592 KB Output is correct
12 Correct 15 ms 14720 KB Output is correct
13 Correct 14 ms 14592 KB Output is correct
14 Correct 14 ms 14592 KB Output is correct
15 Correct 14 ms 14720 KB Output is correct
16 Correct 14 ms 14720 KB Output is correct
17 Correct 14 ms 14720 KB Output is correct
18 Correct 14 ms 14720 KB Output is correct
19 Correct 14 ms 14720 KB Output is correct
20 Correct 14 ms 14720 KB Output is correct
21 Correct 14 ms 14720 KB Output is correct
22 Correct 14 ms 14848 KB Output is correct
23 Correct 14 ms 14720 KB Output is correct
24 Correct 14 ms 14720 KB Output is correct
25 Correct 14 ms 14720 KB Output is correct
26 Correct 14 ms 14720 KB Output is correct
27 Correct 17 ms 14720 KB Output is correct
28 Correct 15 ms 14720 KB Output is correct
29 Correct 14 ms 14720 KB Output is correct
30 Correct 14 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 255 ms 48856 KB Output is correct
2 Correct 150 ms 49264 KB Output is correct
3 Correct 273 ms 48624 KB Output is correct
4 Correct 152 ms 49264 KB Output is correct
5 Correct 261 ms 45492 KB Output is correct
6 Correct 145 ms 49012 KB Output is correct
7 Correct 244 ms 45092 KB Output is correct
8 Correct 142 ms 47384 KB Output is correct
9 Correct 229 ms 40180 KB Output is correct
10 Correct 223 ms 38004 KB Output is correct
11 Correct 223 ms 40308 KB Output is correct
12 Correct 225 ms 42484 KB Output is correct
13 Correct 222 ms 37724 KB Output is correct
14 Correct 222 ms 42740 KB Output is correct
15 Correct 225 ms 42648 KB Output is correct
16 Correct 222 ms 38512 KB Output is correct
17 Correct 235 ms 39068 KB Output is correct
18 Correct 234 ms 39284 KB Output is correct
19 Correct 222 ms 41716 KB Output is correct
20 Correct 228 ms 43380 KB Output is correct
21 Correct 13 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 15 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 14 ms 14592 KB Output is correct
12 Correct 15 ms 14720 KB Output is correct
13 Correct 14 ms 14592 KB Output is correct
14 Correct 14 ms 14592 KB Output is correct
15 Correct 14 ms 14720 KB Output is correct
16 Correct 14 ms 14720 KB Output is correct
17 Correct 14 ms 14720 KB Output is correct
18 Correct 14 ms 14720 KB Output is correct
19 Correct 14 ms 14720 KB Output is correct
20 Correct 14 ms 14720 KB Output is correct
21 Correct 14 ms 14720 KB Output is correct
22 Correct 14 ms 14848 KB Output is correct
23 Correct 14 ms 14720 KB Output is correct
24 Correct 14 ms 14720 KB Output is correct
25 Correct 14 ms 14720 KB Output is correct
26 Correct 14 ms 14720 KB Output is correct
27 Correct 17 ms 14720 KB Output is correct
28 Correct 15 ms 14720 KB Output is correct
29 Correct 14 ms 14720 KB Output is correct
30 Correct 14 ms 14720 KB Output is correct
31 Correct 255 ms 48856 KB Output is correct
32 Correct 150 ms 49264 KB Output is correct
33 Correct 273 ms 48624 KB Output is correct
34 Correct 152 ms 49264 KB Output is correct
35 Correct 261 ms 45492 KB Output is correct
36 Correct 145 ms 49012 KB Output is correct
37 Correct 244 ms 45092 KB Output is correct
38 Correct 142 ms 47384 KB Output is correct
39 Correct 229 ms 40180 KB Output is correct
40 Correct 223 ms 38004 KB Output is correct
41 Correct 223 ms 40308 KB Output is correct
42 Correct 225 ms 42484 KB Output is correct
43 Correct 222 ms 37724 KB Output is correct
44 Correct 222 ms 42740 KB Output is correct
45 Correct 225 ms 42648 KB Output is correct
46 Correct 222 ms 38512 KB Output is correct
47 Correct 235 ms 39068 KB Output is correct
48 Correct 234 ms 39284 KB Output is correct
49 Correct 222 ms 41716 KB Output is correct
50 Correct 228 ms 43380 KB Output is correct
51 Correct 13 ms 14464 KB Output is correct
52 Correct 250 ms 29304 KB Output is correct
53 Correct 242 ms 29304 KB Output is correct
54 Correct 238 ms 29304 KB Output is correct
55 Correct 254 ms 29304 KB Output is correct
56 Correct 247 ms 29304 KB Output is correct
57 Correct 245 ms 29304 KB Output is correct
58 Correct 221 ms 35312 KB Output is correct
59 Correct 224 ms 35952 KB Output is correct
60 Correct 269 ms 35828 KB Output is correct
61 Correct 256 ms 35568 KB Output is correct
62 Correct 149 ms 49264 KB Output is correct
63 Correct 143 ms 49264 KB Output is correct
64 Correct 141 ms 47728 KB Output is correct
65 Correct 149 ms 49068 KB Output is correct
66 Correct 205 ms 36684 KB Output is correct
67 Correct 194 ms 36844 KB Output is correct
68 Correct 184 ms 36680 KB Output is correct
69 Correct 197 ms 36684 KB Output is correct
70 Correct 189 ms 36812 KB Output is correct
71 Correct 189 ms 38224 KB Output is correct
72 Correct 187 ms 40012 KB Output is correct
73 Correct 197 ms 39628 KB Output is correct
74 Correct 190 ms 40268 KB Output is correct
75 Correct 190 ms 40328 KB Output is correct
76 Correct 235 ms 44656 KB Output is correct
77 Correct 257 ms 43248 KB Output is correct
78 Correct 229 ms 42612 KB Output is correct
79 Correct 231 ms 40952 KB Output is correct
80 Correct 226 ms 43640 KB Output is correct
81 Correct 222 ms 40180 KB Output is correct
82 Correct 222 ms 41076 KB Output is correct
83 Correct 220 ms 37624 KB Output is correct
84 Correct 226 ms 42384 KB Output is correct
85 Correct 243 ms 41336 KB Output is correct
86 Correct 222 ms 37236 KB Output is correct
87 Correct 223 ms 38648 KB Output is correct
88 Correct 234 ms 39672 KB Output is correct
89 Correct 198 ms 36084 KB Output is correct
90 Correct 197 ms 35952 KB Output is correct
91 Correct 235 ms 38004 KB Output is correct
92 Correct 204 ms 36852 KB Output is correct
93 Correct 206 ms 36468 KB Output is correct
94 Correct 210 ms 36084 KB Output is correct
95 Correct 209 ms 37236 KB Output is correct
96 Correct 209 ms 36340 KB Output is correct
97 Correct 210 ms 37896 KB Output is correct