Submission #220191

# Submission time Handle Problem Language Result Execution time Memory
220191 2020-04-07T09:30:54 Z fedoseevtimofey Capital City (JOI20_capital_city) C++14
11 / 100
524 ms 8952 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 = 3000 + 7;

vector <int> g[N];
vector <int> gr[N];
vector <int> rg[N];

int c[N];
bool bad[N][N];

vector <int> path;

void dfs(int u, int p = -1) {
  path.push_back(u); 
  if (c[path.back()] == c[path.front()]) {
    for (int v : path) {
      if (c[v] != c[u]) {
        bad[c[u]][c[v]] = 1;
      }
    }
  }
  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  path.pop_back();
}

vector <int> t;
bool used[N];
int cmp[N];
bool ok[N];
int sz[N];

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);
  }
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
  }
  for (int i = 0; i < n; ++i) {
    dfs(i, -1);
  }
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < k; ++j) {
      if (bad[i][j]) {
        gr[i].push_back(j);
        rg[j].push_back(i);
      }
    }
  }
  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 5 ms 512 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 70 ms 1528 KB Output is correct
12 Correct 73 ms 1540 KB Output is correct
13 Correct 74 ms 1532 KB Output is correct
14 Correct 80 ms 1576 KB Output is correct
15 Correct 51 ms 3192 KB Output is correct
16 Correct 66 ms 4216 KB Output is correct
17 Correct 494 ms 2168 KB Output is correct
18 Correct 524 ms 2216 KB Output is correct
19 Correct 518 ms 2272 KB Output is correct
20 Correct 512 ms 2296 KB Output is correct
21 Correct 511 ms 2256 KB Output is correct
22 Correct 78 ms 8952 KB Output is correct
23 Correct 90 ms 8568 KB Output is correct
24 Correct 54 ms 3708 KB Output is correct
25 Correct 192 ms 2316 KB Output is correct
26 Correct 360 ms 2296 KB Output is correct
27 Correct 176 ms 2424 KB Output is correct
28 Correct 233 ms 2300 KB Output is correct
29 Correct 172 ms 2400 KB Output is correct
30 Correct 226 ms 2296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 896 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 70 ms 1528 KB Output is correct
12 Correct 73 ms 1540 KB Output is correct
13 Correct 74 ms 1532 KB Output is correct
14 Correct 80 ms 1576 KB Output is correct
15 Correct 51 ms 3192 KB Output is correct
16 Correct 66 ms 4216 KB Output is correct
17 Correct 494 ms 2168 KB Output is correct
18 Correct 524 ms 2216 KB Output is correct
19 Correct 518 ms 2272 KB Output is correct
20 Correct 512 ms 2296 KB Output is correct
21 Correct 511 ms 2256 KB Output is correct
22 Correct 78 ms 8952 KB Output is correct
23 Correct 90 ms 8568 KB Output is correct
24 Correct 54 ms 3708 KB Output is correct
25 Correct 192 ms 2316 KB Output is correct
26 Correct 360 ms 2296 KB Output is correct
27 Correct 176 ms 2424 KB Output is correct
28 Correct 233 ms 2300 KB Output is correct
29 Correct 172 ms 2400 KB Output is correct
30 Correct 226 ms 2296 KB Output is correct
31 Runtime error 6 ms 896 KB Execution killed with signal 11 (could be triggered by violating memory limits)
32 Halted 0 ms 0 KB -