Submission #256145

# Submission time Handle Problem Language Result Execution time Memory
256145 2020-08-02T10:21:25 Z fedoseevtimofey Mergers (JOI19_mergers) C++14
0 / 100
1 ms 768 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 = 1007;
int par[N];

int get(int a) {
  return (a == par[a] ? a : par[a] = get(par[a]));
}

void join(int a, int b) {
  a = get(a);
  b = get(b);
  par[a] = b;
}

vector <int> g[N];

int a[N];
int cnt[N][N];
int all[N];

int k;
 
void dfs(int u, int p) {
  ++cnt[u][a[u]];
  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u);
      for (int i = 0; i < k; ++i) {
        cnt[u][i] += cnt[v][i];
      }
    }
  }
  bool ok = true;
  for (int i = 0; i < k; ++i) {
    ok &= (cnt[u][i] == 0 || cnt[u][i] == all[i]);
  }
  if (!ok && p != -1) {
    join(u, p); 
  }
}

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 >> a[i];
    --a[i];
    ++all[a[i]];
  }
  for (int i = 0; i < n; ++i) par[i] = i;
  dfs(0, -1);
  vector <int> deg(n);
  for (int i = 0; i < n; ++i) {
    for (int j : g[i]) {
      if (get(i) != get(j)) {
        ++deg[i];
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) if (deg[i] == 1) ++cnt;
  cout << (cnt + 1) / 2 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Incorrect 1 ms 768 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Incorrect 1 ms 768 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Incorrect 1 ms 768 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 512 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 1 ms 512 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Incorrect 1 ms 768 KB Output isn't correct
4 Halted 0 ms 0 KB -