답안 #366138

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
366138 2021-02-13T09:46:37 Z watemus Mergers (JOI19_mergers) C++17
0 / 100
114 ms 35804 KB
//
// Created by watemus on 13.02.2021.
//

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define FF first
#define SS second

using ll = long long;
using ld = long double;

#define int ll

template<typename T>
using vec = std::vector<T>;

template<typename T>
using uset = std::unordered_set<T>;

template<typename T1, typename T2>
using umap = std::unordered_map<T1, T2>;

constexpr ll INFL = 1000000000000000069;
constexpr int INFI = 1000000069;
const ld PI = acos(-1);

#ifdef LOCAL
std::mt19937 rnd(69);
#else
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
#endif

vec<pair<int, int>> DD = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

#ifdef LOCAL
#else
#endif

const int N = 5e5 + 10;

vec<pair<int, int>> g[N];
int tin[N], tup[N];
int usd[N + N];
int br[N + N];

int dfs_timer = 0;

void dfs(int u, int p) {
  usd[u] = 1;
  tin[u] = tup[u] = dfs_timer++;
  for (auto [v, id] : g[u]) {
    if (id == p) continue;
    if (usd[v]) {
      tup[u] = min(tup[u], tin[v]);
    } else {
      dfs(v, id);
      if (tin[u] < tup[v]) {
        br[id] = 1;
      }
      tup[u] = min(tup[u], tup[v]);
    }
  }
}

int clr[N];

void color(int u, int c) {
  usd[u] = 1;
  clr[u] = c;
  for (auto [v, id] : g[u]) {
    if (!br[id] && !usd[v]) {
      color(v, c);
    }
  }
}

void run() {
  int n, k;
  cin >> n >> k;
  vec<vec<int>> kk(k);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].emplace_back(v, i);
    g[v].emplace_back(u, i);
  }
  int m = n - 1;
  for (int i = 0; i < n; i++) {
    int col;
    cin >> col;
    col--;
    kk[col].push_back(i);
  }
  for (int i = 0; i < k; i++) {
    for (int j = 1; j < kk[i].size(); j++) {
      g[kk[i][j]].emplace_back(kk[i][j - 1], m);
      g[kk[i][j - 1]].emplace_back(kk[i][j], m);
      m++;
    }
  }
  dfs(0, -1);
  memset(usd, 0, sizeof usd);
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (!usd[i]) {
      color(i, c);
      c++;
    }
  }
  vec<int> deg(c);
  for (int i = 0; i < n; i++) {
    for (auto [v, id] : g[i]) {
      if (br[id]) {
        deg[clr[i]]++;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < c; i++) {
    if (deg[i] == 1) {
      ans++;
    }
  }
  cout << max(ans - 1, 0LL) << '\n';
}

signed main() {
#ifdef LOCAL
  std::freopen("input.txt", "r", stdin);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    run();
  }
  return 0;
}


Compilation message

mergers.cpp: In function 'void run()':
mergers.cpp:105:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for (int j = 1; j < kk[i].size(); j++) {
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 19948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 19948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 19948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 34928 KB Output is correct
2 Incorrect 86 ms 35804 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 19948 KB Output isn't correct
2 Halted 0 ms 0 KB -