Submission #233264

# Submission time Handle Problem Language Result Execution time Memory
233264 2020-05-20T07:49:39 Z AlexLuchianov Mergers (JOI19_mergers) C++14
24 / 100
161 ms 32240 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 = 100000;
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++){
                  ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 8 ms 7552 KB Output is correct
3 Correct 9 ms 7456 KB Output is correct
4 Correct 9 ms 7552 KB Output is correct
5 Correct 9 ms 7552 KB Output is correct
6 Correct 8 ms 7552 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7552 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
13 Correct 9 ms 7552 KB Output is correct
14 Correct 8 ms 7552 KB Output is correct
15 Correct 9 ms 7552 KB Output is correct
16 Correct 9 ms 7552 KB Output is correct
17 Correct 8 ms 7552 KB Output is correct
18 Correct 8 ms 7552 KB Output is correct
19 Correct 8 ms 7552 KB Output is correct
20 Correct 9 ms 7552 KB Output is correct
21 Correct 10 ms 7552 KB Output is correct
22 Correct 8 ms 7552 KB Output is correct
23 Correct 8 ms 7424 KB Output is correct
24 Correct 9 ms 7552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 8 ms 7552 KB Output is correct
3 Correct 9 ms 7456 KB Output is correct
4 Correct 9 ms 7552 KB Output is correct
5 Correct 9 ms 7552 KB Output is correct
6 Correct 8 ms 7552 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7552 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
13 Correct 9 ms 7552 KB Output is correct
14 Correct 8 ms 7552 KB Output is correct
15 Correct 9 ms 7552 KB Output is correct
16 Correct 9 ms 7552 KB Output is correct
17 Correct 8 ms 7552 KB Output is correct
18 Correct 8 ms 7552 KB Output is correct
19 Correct 8 ms 7552 KB Output is correct
20 Correct 9 ms 7552 KB Output is correct
21 Correct 10 ms 7552 KB Output is correct
22 Correct 8 ms 7552 KB Output is correct
23 Correct 8 ms 7424 KB Output is correct
24 Correct 9 ms 7552 KB Output is correct
25 Correct 9 ms 7552 KB Output is correct
26 Correct 11 ms 8064 KB Output is correct
27 Correct 11 ms 7936 KB Output is correct
28 Correct 11 ms 8320 KB Output is correct
29 Correct 11 ms 8192 KB Output is correct
30 Correct 11 ms 7936 KB Output is correct
31 Correct 9 ms 7552 KB Output is correct
32 Correct 10 ms 8192 KB Output is correct
33 Correct 9 ms 7552 KB Output is correct
34 Correct 10 ms 7936 KB Output is correct
35 Correct 11 ms 8192 KB Output is correct
36 Correct 11 ms 7936 KB Output is correct
37 Correct 11 ms 7936 KB Output is correct
38 Correct 9 ms 7552 KB Output is correct
39 Correct 11 ms 7936 KB Output is correct
40 Correct 11 ms 7936 KB Output is correct
41 Correct 11 ms 7936 KB Output is correct
42 Correct 11 ms 8044 KB Output is correct
43 Correct 11 ms 8064 KB Output is correct
44 Correct 8 ms 7552 KB Output is correct
45 Incorrect 10 ms 8064 KB Output isn't correct
46 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 8 ms 7552 KB Output is correct
3 Correct 9 ms 7456 KB Output is correct
4 Correct 9 ms 7552 KB Output is correct
5 Correct 9 ms 7552 KB Output is correct
6 Correct 8 ms 7552 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7552 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
13 Correct 9 ms 7552 KB Output is correct
14 Correct 8 ms 7552 KB Output is correct
15 Correct 9 ms 7552 KB Output is correct
16 Correct 9 ms 7552 KB Output is correct
17 Correct 8 ms 7552 KB Output is correct
18 Correct 8 ms 7552 KB Output is correct
19 Correct 8 ms 7552 KB Output is correct
20 Correct 9 ms 7552 KB Output is correct
21 Correct 10 ms 7552 KB Output is correct
22 Correct 8 ms 7552 KB Output is correct
23 Correct 8 ms 7424 KB Output is correct
24 Correct 9 ms 7552 KB Output is correct
25 Correct 9 ms 7552 KB Output is correct
26 Correct 76 ms 22176 KB Output is correct
27 Correct 128 ms 21880 KB Output is correct
28 Correct 10 ms 7936 KB Output is correct
29 Correct 9 ms 7552 KB Output is correct
30 Correct 9 ms 7552 KB Output is correct
31 Correct 128 ms 21880 KB Output is correct
32 Correct 11 ms 7936 KB Output is correct
33 Correct 161 ms 26104 KB Output is correct
34 Correct 133 ms 21880 KB Output is correct
35 Correct 11 ms 7936 KB Output is correct
36 Correct 131 ms 22264 KB Output is correct
37 Correct 12 ms 7936 KB Output is correct
38 Correct 11 ms 7936 KB Output is correct
39 Correct 78 ms 22128 KB Output is correct
40 Correct 11 ms 8064 KB Output is correct
41 Correct 134 ms 21752 KB Output is correct
42 Correct 152 ms 23160 KB Output is correct
43 Correct 9 ms 7552 KB Output is correct
44 Correct 154 ms 26232 KB Output is correct
45 Correct 156 ms 24056 KB Output is correct
46 Correct 12 ms 7936 KB Output is correct
47 Correct 11 ms 7936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 22132 KB Output is correct
2 Correct 140 ms 32240 KB Output is correct
3 Correct 11 ms 8064 KB Output is correct
4 Correct 11 ms 7936 KB Output is correct
5 Correct 9 ms 7552 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 10 ms 7936 KB Output is correct
8 Incorrect 120 ms 23288 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 8 ms 7552 KB Output is correct
3 Correct 9 ms 7456 KB Output is correct
4 Correct 9 ms 7552 KB Output is correct
5 Correct 9 ms 7552 KB Output is correct
6 Correct 8 ms 7552 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7552 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
13 Correct 9 ms 7552 KB Output is correct
14 Correct 8 ms 7552 KB Output is correct
15 Correct 9 ms 7552 KB Output is correct
16 Correct 9 ms 7552 KB Output is correct
17 Correct 8 ms 7552 KB Output is correct
18 Correct 8 ms 7552 KB Output is correct
19 Correct 8 ms 7552 KB Output is correct
20 Correct 9 ms 7552 KB Output is correct
21 Correct 10 ms 7552 KB Output is correct
22 Correct 8 ms 7552 KB Output is correct
23 Correct 8 ms 7424 KB Output is correct
24 Correct 9 ms 7552 KB Output is correct
25 Correct 9 ms 7552 KB Output is correct
26 Correct 11 ms 8064 KB Output is correct
27 Correct 11 ms 7936 KB Output is correct
28 Correct 11 ms 8320 KB Output is correct
29 Correct 11 ms 8192 KB Output is correct
30 Correct 11 ms 7936 KB Output is correct
31 Correct 9 ms 7552 KB Output is correct
32 Correct 10 ms 8192 KB Output is correct
33 Correct 9 ms 7552 KB Output is correct
34 Correct 10 ms 7936 KB Output is correct
35 Correct 11 ms 8192 KB Output is correct
36 Correct 11 ms 7936 KB Output is correct
37 Correct 11 ms 7936 KB Output is correct
38 Correct 9 ms 7552 KB Output is correct
39 Correct 11 ms 7936 KB Output is correct
40 Correct 11 ms 7936 KB Output is correct
41 Correct 11 ms 7936 KB Output is correct
42 Correct 11 ms 8044 KB Output is correct
43 Correct 11 ms 8064 KB Output is correct
44 Correct 8 ms 7552 KB Output is correct
45 Incorrect 10 ms 8064 KB Output isn't correct
46 Halted 0 ms 0 KB -