답안 #233265

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
233265 2020-05-20T07:50:20 Z AlexLuchianov Mergers (JOI19_mergers) C++14
24 / 100
190 ms 58840 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>

using namespace std;

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 500000;
int const lgmax = 20;

vector<int> g[1 + nmax];
int far[1 + lgmax][1 + nmax];
int level[1 + nmax];

void dfs(int node, int parent){
  level[node] = level[parent] + 1;
  far[0][node] = parent;
  for(int h = 0; h < g[node].size(); h++){
    int to = g[node][h];
    if(to != parent)
      dfs(to, node);
  }
}

void computefar(int n){
  for(int h = 1; h <= lgmax; h++)
    for(int i = 1;i <= n; i++)
      far[h][i] = far[h - 1][far[h - 1][i]];
}
int getlca(int x, int y){
  if(level[x] < level[y])
    swap(x, y);
  for(int h = lgmax; 0 <= h; h--)
    if(level[y] + (1 << h) <= level[x])
      x = far[h][x];
  if(x == y)
    return x;
  for(int h = lgmax; 0 <= h; h--)
    if(far[h][x] != far[h][y]){
      x = far[h][x];
      y = far[h][y];
    }
  return far[0][x];
}

class Dsu{
private:
  vector<int> mult;
public:
  Dsu(int n){
    mult.resize(1 + n);
    for(int i = 1;i <= n; i++)
      mult[i] = i;
  }
  int jump(int x){
    if(mult[x] != x)
      mult[x] = jump(mult[x]);
    return mult[x];
  }
  bool unite(int gala, int galb){
    gala = jump(gala);
    galb = jump(galb);
    if(gala == galb)
      return 0;
    mult[gala] = galb;
    return 1;
  }
};
int frec[1 + nmax];

void markpath(int x, int y, Dsu &dsu){
  int lca = getlca(x, y);
  while(dsu.unite(lca, x) == 1)
    x = far[0][x];
  while(dsu.unite(lca, y) == 1)
    y = far[0][y];

}

set<int> tree[1 + nmax];
int edge[1 + nmax][2];

int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  for(int i = 1;i < n; i++){
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    edge[i][0] = x;
    edge[i][1] = y;
  }
  dfs(1, 0);

  computefar(n);
  Dsu dsu(n);
  for(int i = 1;i <= n; i++){
    int val;
    cin >> val;
    if(0 < frec[val])
      markpath(frec[val], i, dsu);
    frec[val] = i;
  }


  for(int i = 1;i < n; i++){
    int x = dsu.jump(edge[i][0]);
    int y = dsu.jump(edge[i][1]);
    if(x != y){
      tree[x].insert(y);
      tree[y].insert(x);
    }
  }

  int leafs = 0;
  for(int i = 1; i <= n; i++)
    leafs += (tree[i].size() == 1);

  if(leafs <= 1)
    cout << 0;
  else
    cout << (leafs + 1) / 2;
  return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:24:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int h = 0; h < g[node].size(); h++){
                  ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 35712 KB Output is correct
2 Correct 24 ms 35712 KB Output is correct
3 Correct 23 ms 35840 KB Output is correct
4 Correct 24 ms 35712 KB Output is correct
5 Correct 24 ms 35712 KB Output is correct
6 Correct 24 ms 35840 KB Output is correct
7 Correct 25 ms 35712 KB Output is correct
8 Correct 23 ms 35712 KB Output is correct
9 Correct 23 ms 35712 KB Output is correct
10 Correct 25 ms 35840 KB Output is correct
11 Correct 24 ms 35712 KB Output is correct
12 Correct 24 ms 35840 KB Output is correct
13 Correct 23 ms 35712 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35712 KB Output is correct
16 Correct 23 ms 35712 KB Output is correct
17 Correct 23 ms 35712 KB Output is correct
18 Correct 24 ms 35840 KB Output is correct
19 Correct 24 ms 35712 KB Output is correct
20 Correct 24 ms 35712 KB Output is correct
21 Correct 24 ms 35808 KB Output is correct
22 Correct 24 ms 35840 KB Output is correct
23 Correct 24 ms 35712 KB Output is correct
24 Correct 24 ms 35760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 35712 KB Output is correct
2 Correct 24 ms 35712 KB Output is correct
3 Correct 23 ms 35840 KB Output is correct
4 Correct 24 ms 35712 KB Output is correct
5 Correct 24 ms 35712 KB Output is correct
6 Correct 24 ms 35840 KB Output is correct
7 Correct 25 ms 35712 KB Output is correct
8 Correct 23 ms 35712 KB Output is correct
9 Correct 23 ms 35712 KB Output is correct
10 Correct 25 ms 35840 KB Output is correct
11 Correct 24 ms 35712 KB Output is correct
12 Correct 24 ms 35840 KB Output is correct
13 Correct 23 ms 35712 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35712 KB Output is correct
16 Correct 23 ms 35712 KB Output is correct
17 Correct 23 ms 35712 KB Output is correct
18 Correct 24 ms 35840 KB Output is correct
19 Correct 24 ms 35712 KB Output is correct
20 Correct 24 ms 35712 KB Output is correct
21 Correct 24 ms 35808 KB Output is correct
22 Correct 24 ms 35840 KB Output is correct
23 Correct 24 ms 35712 KB Output is correct
24 Correct 24 ms 35760 KB Output is correct
25 Correct 23 ms 35712 KB Output is correct
26 Correct 25 ms 36352 KB Output is correct
27 Correct 26 ms 36224 KB Output is correct
28 Correct 26 ms 36480 KB Output is correct
29 Correct 27 ms 36352 KB Output is correct
30 Correct 25 ms 36096 KB Output is correct
31 Correct 24 ms 35712 KB Output is correct
32 Correct 26 ms 36352 KB Output is correct
33 Correct 24 ms 35712 KB Output is correct
34 Correct 27 ms 36224 KB Output is correct
35 Correct 28 ms 36480 KB Output is correct
36 Correct 26 ms 36224 KB Output is correct
37 Correct 25 ms 36224 KB Output is correct
38 Correct 23 ms 35840 KB Output is correct
39 Correct 26 ms 36096 KB Output is correct
40 Correct 26 ms 36096 KB Output is correct
41 Correct 28 ms 36224 KB Output is correct
42 Correct 26 ms 36224 KB Output is correct
43 Correct 26 ms 36224 KB Output is correct
44 Correct 23 ms 35712 KB Output is correct
45 Incorrect 26 ms 36224 KB Output isn't correct
46 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 35712 KB Output is correct
2 Correct 24 ms 35712 KB Output is correct
3 Correct 23 ms 35840 KB Output is correct
4 Correct 24 ms 35712 KB Output is correct
5 Correct 24 ms 35712 KB Output is correct
6 Correct 24 ms 35840 KB Output is correct
7 Correct 25 ms 35712 KB Output is correct
8 Correct 23 ms 35712 KB Output is correct
9 Correct 23 ms 35712 KB Output is correct
10 Correct 25 ms 35840 KB Output is correct
11 Correct 24 ms 35712 KB Output is correct
12 Correct 24 ms 35840 KB Output is correct
13 Correct 23 ms 35712 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35712 KB Output is correct
16 Correct 23 ms 35712 KB Output is correct
17 Correct 23 ms 35712 KB Output is correct
18 Correct 24 ms 35840 KB Output is correct
19 Correct 24 ms 35712 KB Output is correct
20 Correct 24 ms 35712 KB Output is correct
21 Correct 24 ms 35808 KB Output is correct
22 Correct 24 ms 35840 KB Output is correct
23 Correct 24 ms 35712 KB Output is correct
24 Correct 24 ms 35760 KB Output is correct
25 Correct 23 ms 35712 KB Output is correct
26 Correct 88 ms 49008 KB Output is correct
27 Correct 137 ms 48760 KB Output is correct
28 Correct 26 ms 36224 KB Output is correct
29 Correct 23 ms 35712 KB Output is correct
30 Correct 24 ms 35712 KB Output is correct
31 Correct 143 ms 48760 KB Output is correct
32 Correct 26 ms 36096 KB Output is correct
33 Correct 173 ms 52960 KB Output is correct
34 Correct 141 ms 48760 KB Output is correct
35 Correct 26 ms 36096 KB Output is correct
36 Correct 134 ms 49144 KB Output is correct
37 Correct 26 ms 36096 KB Output is correct
38 Correct 26 ms 36096 KB Output is correct
39 Correct 100 ms 49008 KB Output is correct
40 Correct 26 ms 36224 KB Output is correct
41 Correct 149 ms 48760 KB Output is correct
42 Correct 190 ms 50040 KB Output is correct
43 Correct 25 ms 35712 KB Output is correct
44 Correct 158 ms 53112 KB Output is correct
45 Correct 177 ms 50936 KB Output is correct
46 Correct 25 ms 36096 KB Output is correct
47 Correct 26 ms 36096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 49008 KB Output is correct
2 Correct 161 ms 58840 KB Output is correct
3 Correct 26 ms 36224 KB Output is correct
4 Correct 27 ms 36096 KB Output is correct
5 Correct 25 ms 35712 KB Output is correct
6 Correct 24 ms 35712 KB Output is correct
7 Correct 26 ms 36096 KB Output is correct
8 Incorrect 139 ms 50064 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 35712 KB Output is correct
2 Correct 24 ms 35712 KB Output is correct
3 Correct 23 ms 35840 KB Output is correct
4 Correct 24 ms 35712 KB Output is correct
5 Correct 24 ms 35712 KB Output is correct
6 Correct 24 ms 35840 KB Output is correct
7 Correct 25 ms 35712 KB Output is correct
8 Correct 23 ms 35712 KB Output is correct
9 Correct 23 ms 35712 KB Output is correct
10 Correct 25 ms 35840 KB Output is correct
11 Correct 24 ms 35712 KB Output is correct
12 Correct 24 ms 35840 KB Output is correct
13 Correct 23 ms 35712 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35712 KB Output is correct
16 Correct 23 ms 35712 KB Output is correct
17 Correct 23 ms 35712 KB Output is correct
18 Correct 24 ms 35840 KB Output is correct
19 Correct 24 ms 35712 KB Output is correct
20 Correct 24 ms 35712 KB Output is correct
21 Correct 24 ms 35808 KB Output is correct
22 Correct 24 ms 35840 KB Output is correct
23 Correct 24 ms 35712 KB Output is correct
24 Correct 24 ms 35760 KB Output is correct
25 Correct 23 ms 35712 KB Output is correct
26 Correct 25 ms 36352 KB Output is correct
27 Correct 26 ms 36224 KB Output is correct
28 Correct 26 ms 36480 KB Output is correct
29 Correct 27 ms 36352 KB Output is correct
30 Correct 25 ms 36096 KB Output is correct
31 Correct 24 ms 35712 KB Output is correct
32 Correct 26 ms 36352 KB Output is correct
33 Correct 24 ms 35712 KB Output is correct
34 Correct 27 ms 36224 KB Output is correct
35 Correct 28 ms 36480 KB Output is correct
36 Correct 26 ms 36224 KB Output is correct
37 Correct 25 ms 36224 KB Output is correct
38 Correct 23 ms 35840 KB Output is correct
39 Correct 26 ms 36096 KB Output is correct
40 Correct 26 ms 36096 KB Output is correct
41 Correct 28 ms 36224 KB Output is correct
42 Correct 26 ms 36224 KB Output is correct
43 Correct 26 ms 36224 KB Output is correct
44 Correct 23 ms 35712 KB Output is correct
45 Incorrect 26 ms 36224 KB Output isn't correct
46 Halted 0 ms 0 KB -