답안 #630927

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
630927 2022-08-17T10:45:05 Z cadmiumsky Mergers (JOI19_mergers) C++14
10 / 100
165 ms 26648 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 2e5 + 5, nbit = 18;

int n, k;
vector<int> g[nmax];

#define lsb(x) (x & -x)
namespace AIB {
  int tree[nmax], len;
  void init(int nlen) {
    len = nlen;
  }
  void upd(int x, int val) {
    while(x > 0) tree[x] += val, x -= lsb(x);
  }
  int query(int x) {
    int sum = 0;
    while(x <= n) sum += tree[x], x += lsb(x);
    return sum;
  }
  int query(int l, int r) {
    return query(r) - query(l - 1);
  }
}

int pin[nmax], pout[nmax], inp = 1;
namespace LCA {
  int p[nmax][nbit];
  void init(int node, int f) {
    pin[node] = inp++;
    p[node][0] = f;
    for(int i = 1; i < nbit; i++)
      p[node][i] = p[p[node][i - 1]][i - 1];
    for(auto x : g[node])
      if(x == f) continue;
      else init(x, node);
    pout[node] = inp++;
  }
  bool isanc(int x, int y) {return pin[x] <= pin[y] && pout[y] <= pout[x]; }
  int lca(int x, int y) {
    if(isanc(x, y)) return x;
    if(isanc(y, x)) return y;
    for(int i = nbit - 1; i >= 0; i--)
      if(!isanc(p[x][i], y))	
	x = p[x][i];
    return p[x][0];
  }
};

vector<int> ofcol[nmax];
int lca[nmax];
int start[nmax], fin[nmax];
int cnt;

vector<int> cuts;
int identify(int node, int f) {
  int sum = start[node] + fin[node];
  for(auto x : g[node]) {
    if(x == f) continue;
    int tmp = identify(x, node);
    if(tmp == 0)
      cuts.push_back(x);
    else
      sum += tmp;
  }
  return sum;
}

signed main() {
  cin >> n >> k;
  for(int i = 1, x, y; i < n; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  LCA::init(1, 1);
  AIB::init(inp);
  for(int i = 1, x; i <= n; i++) {
    cin >> x;
    ofcol[x].push_back(i); 
  }
  for(int i = 1; i <= k; i++) {
    lca[i] = ofcol[i].back();
    for(auto x : ofcol[i])
      lca[i] = LCA::lca(x, lca[i]);
    for(auto x : ofcol[i])
      start[x]++, 
      fin[lca[i]]--;
  }
  //for(int i = 1; i <= n; i++)
    //cerr << pin[i] << ' ' << pout[i] << '\n';
  identify(1, 1);
  sort(all(cuts), [&](int a, int b) {return pout[a] < pout[b]; });
  int total = 0; 
  for(auto x : cuts) {
    if(AIB::query(pin[x], pout[x]) == 0) 
      AIB::upd(pin[x], 1), total++;
  }
  
  cout << (total + 1) / 2 << '\n';
}

/**
  De-atâtea nopți aud plouând,
  Aud materia plângând..
  Sînt singur, și mă duce un gând
  Spre locuințele lacustre.

  Și parcă dorm pe scânduri ude,
  În spate mă izbește-un val --
  Tresar prin somn și mi se pare
  Că n-am tras podul de la mal.

  Un gol istoric se întinde,
  Pe-același vremuri mă găsesc..
  Și simt cum de atâta ploaie
  Pilonii grei se prăbușesc.

  De-atâtea nopți aud plouând,
  Tot tresărind, tot așteptând..
  Sînt singur, și mă duce-un gând
  Spre locuințele lacustre.
-- George Bacovia, Lacustră
*/

# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9712 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 6 ms 9716 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 5 ms 9712 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 6 ms 9672 KB Output is correct
19 Correct 5 ms 9716 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 7 ms 9740 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 7 ms 9656 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9712 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 6 ms 9716 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 5 ms 9712 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 6 ms 9672 KB Output is correct
19 Correct 5 ms 9716 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 7 ms 9740 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 7 ms 9656 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9752 KB Output is correct
26 Incorrect 8 ms 10200 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9712 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 6 ms 9716 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 5 ms 9712 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 6 ms 9672 KB Output is correct
19 Correct 5 ms 9716 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 7 ms 9740 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 7 ms 9656 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9656 KB Output is correct
26 Correct 102 ms 22488 KB Output is correct
27 Correct 165 ms 22220 KB Output is correct
28 Incorrect 8 ms 10068 KB Output isn't correct
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 21920 KB Output is correct
2 Incorrect 133 ms 26648 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9712 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 6 ms 9716 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 5 ms 9712 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 6 ms 9672 KB Output is correct
19 Correct 5 ms 9716 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 7 ms 9740 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 7 ms 9656 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9752 KB Output is correct
26 Incorrect 8 ms 10200 KB Output isn't correct
27 Halted 0 ms 0 KB -