Submission #203385

# Submission time Handle Problem Language Result Execution time Memory
203385 2020-02-20T12:37:10 Z theStaticMind Mergers (JOI19_mergers) C++14
70 / 100
3000 ms 245616 KB
#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 1000000000//00000000
#define modulo 1000000007
#define mod 998244353
//#define int long long int
using namespace std;

struct Tree{
      vector<vector<int> >& adj;
      vector<vector<int> > anc;
      vector<int> depth;
      int n;
      int root;

      Tree(vector<vector<int> >& a, int N = 1e6, int r = 1): adj(a){
            n = N + 5;
            root = r;
            anc = vector<vector<int> > (25, vector<int>(n, 0));
            depth = vector<int>(n, 0);

            init(root, -1);

            for(int d = 1; d < 25; d++){

                  for(int x = 0; x < n; x++){

                        anc[d][x] = anc[d - 1][anc[d - 1][x]];

                  }

            }

      }

      void init(int x, int pre){

            for(int i = 0; i < adj[x].size(); i++){

                int y = adj[x][i];

                  if(y == pre)continue;

                  anc[0][y] = x;

                  depth[y] = depth[x] + 1;

                  init(y, x);

            }

      }

      int LCA(int x, int y){
            if(depth[x] < depth[y])swap(x,y);

            while(depth[x] != depth[y]){

                  int diff = depth[x] - depth[y];

                  diff = log2(diff&-diff);

                  x = anc[diff][x];

            }

            if(x == y) return x;

            for(int i = 24; i >= 0; i--){

                  if(anc[i][x] != anc[i][y]){

                        x = anc[i][x];

                        y = anc[i][y];

                  }

            }

            return anc[0][x];

      }

      int dist(int x, int y){

            int l = LCA(x, y);

            return depth[x] + depth[y] - 2 * depth[l];

      }

};

bool operator< (ii a, ii b){
      return a.first < b.first || (a.first == b.first && a.second < b.second);
}

vector<vector<int> > adj(500005);
vector<int> val(500005);
vector<int> comp[500005];
vector<int> up(500005);
vector<int> sub(500005);
map<ii,int> edge;
vector<bool> virt(500005, false);

int num(int x, int y){
      return edge[{x, y}];
}


void dfs(int x, int pre, vector<int>& depth){

      for(auto y : adj[x]){
            if(y == pre) continue;
            dfs(y, x, depth);
            if(depth[up[x]] > depth[up[y]])
            up[x] = up[y];
      }
      if(pre != 0 && up[x] != x){
            virt[num(x, pre)] = true;
      }
}

void get(int x, int pre, int& ans, int root){
      sub[x] = 0;
      if(pre != 0 && virt[num(x, pre)] == false) sub[x] = 1;
      for(auto y : adj[x]){
            if(y == pre)continue;
            get(y, x, ans, root);
            sub[x] += sub[y];
      }
      if(pre != 0 && pre != root && virt[num(x, pre)] == false && sub[x] == 1) ans++;
}

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int n, k;
      cin >> n >> k;
      if(n == 1){
            cout << 0;
            return 0;
      }
      for(int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            adj[x].pb(y);
            adj[y].pb(x);
            edge[{x, y}] = i;
            edge[{y, x}] = i;
      }
      for(int i = 1; i <= n; i++){
            cin >> val[i];
            comp[val[i]].pb(i);
            up[i] = i;
      }

      Tree A(adj);

      for(int i = 0; i < 500005; i++){
            if(comp[i].empty()) continue;
            int l = comp[i][0];
            for(auto x : comp[i]){
                  l = A.LCA(x, l);
            }
            for(auto x : comp[i]){
                  up[x] = l;
            }
      }

      dfs(1, 0, A.depth);
      int root = 0;
      for(auto itr = edge.begin(); itr != edge.end(); itr++) {
            int x = itr->first.first;
            int y = itr->first.second;
            int w = itr->second;
            if(virt[w]) continue;
            if(A.depth[x] < A.depth[y]) swap(x, y);
            if((root == 0 || A.depth[x] > A.depth[root])) root = x;
      }
      int ans = 0;
      get(root, 0, ans, root);
      if(root) ans++;
      cout << (ans + 1) / 2;

}

Compilation message

mergers.cpp: In member function 'void Tree::init(int, int)':
mergers.cpp:40:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int i = 0; i < adj[x].size(); i++){
                            ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 133 ms 131704 KB Output is correct
2 Correct 133 ms 131760 KB Output is correct
3 Correct 131 ms 131632 KB Output is correct
4 Correct 128 ms 131576 KB Output is correct
5 Correct 130 ms 131704 KB Output is correct
6 Correct 131 ms 131632 KB Output is correct
7 Correct 22 ms 29816 KB Output is correct
8 Correct 127 ms 131632 KB Output is correct
9 Correct 127 ms 131576 KB Output is correct
10 Correct 129 ms 131608 KB Output is correct
11 Correct 128 ms 131636 KB Output is correct
12 Correct 127 ms 131632 KB Output is correct
13 Correct 132 ms 131580 KB Output is correct
14 Correct 134 ms 131584 KB Output is correct
15 Correct 132 ms 131760 KB Output is correct
16 Correct 127 ms 131632 KB Output is correct
17 Correct 134 ms 131576 KB Output is correct
18 Correct 128 ms 131632 KB Output is correct
19 Correct 130 ms 131760 KB Output is correct
20 Correct 128 ms 131576 KB Output is correct
21 Correct 128 ms 131632 KB Output is correct
22 Correct 133 ms 131636 KB Output is correct
23 Correct 134 ms 131680 KB Output is correct
24 Correct 133 ms 131600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 131704 KB Output is correct
2 Correct 133 ms 131760 KB Output is correct
3 Correct 131 ms 131632 KB Output is correct
4 Correct 128 ms 131576 KB Output is correct
5 Correct 130 ms 131704 KB Output is correct
6 Correct 131 ms 131632 KB Output is correct
7 Correct 22 ms 29816 KB Output is correct
8 Correct 127 ms 131632 KB Output is correct
9 Correct 127 ms 131576 KB Output is correct
10 Correct 129 ms 131608 KB Output is correct
11 Correct 128 ms 131636 KB Output is correct
12 Correct 127 ms 131632 KB Output is correct
13 Correct 132 ms 131580 KB Output is correct
14 Correct 134 ms 131584 KB Output is correct
15 Correct 132 ms 131760 KB Output is correct
16 Correct 127 ms 131632 KB Output is correct
17 Correct 134 ms 131576 KB Output is correct
18 Correct 128 ms 131632 KB Output is correct
19 Correct 130 ms 131760 KB Output is correct
20 Correct 128 ms 131576 KB Output is correct
21 Correct 128 ms 131632 KB Output is correct
22 Correct 133 ms 131636 KB Output is correct
23 Correct 134 ms 131680 KB Output is correct
24 Correct 133 ms 131600 KB Output is correct
25 Correct 126 ms 131632 KB Output is correct
26 Correct 136 ms 132144 KB Output is correct
27 Correct 136 ms 132272 KB Output is correct
28 Correct 138 ms 132272 KB Output is correct
29 Correct 135 ms 132144 KB Output is correct
30 Correct 137 ms 132088 KB Output is correct
31 Correct 132 ms 131632 KB Output is correct
32 Correct 135 ms 132528 KB Output is correct
33 Correct 128 ms 131632 KB Output is correct
34 Correct 131 ms 132144 KB Output is correct
35 Correct 134 ms 132216 KB Output is correct
36 Correct 134 ms 132068 KB Output is correct
37 Correct 137 ms 132120 KB Output is correct
38 Correct 131 ms 131600 KB Output is correct
39 Correct 137 ms 132120 KB Output is correct
40 Correct 132 ms 132088 KB Output is correct
41 Correct 142 ms 132088 KB Output is correct
42 Correct 133 ms 132144 KB Output is correct
43 Correct 135 ms 132400 KB Output is correct
44 Correct 130 ms 131572 KB Output is correct
45 Correct 139 ms 132400 KB Output is correct
46 Correct 138 ms 132144 KB Output is correct
47 Correct 126 ms 131632 KB Output is correct
48 Correct 133 ms 132088 KB Output is correct
49 Correct 133 ms 132216 KB Output is correct
50 Correct 133 ms 132272 KB Output is correct
51 Correct 136 ms 132144 KB Output is correct
52 Correct 135 ms 132088 KB Output is correct
53 Correct 133 ms 132144 KB Output is correct
54 Correct 135 ms 132132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 131704 KB Output is correct
2 Correct 133 ms 131760 KB Output is correct
3 Correct 131 ms 131632 KB Output is correct
4 Correct 128 ms 131576 KB Output is correct
5 Correct 130 ms 131704 KB Output is correct
6 Correct 131 ms 131632 KB Output is correct
7 Correct 22 ms 29816 KB Output is correct
8 Correct 127 ms 131632 KB Output is correct
9 Correct 127 ms 131576 KB Output is correct
10 Correct 129 ms 131608 KB Output is correct
11 Correct 128 ms 131636 KB Output is correct
12 Correct 127 ms 131632 KB Output is correct
13 Correct 132 ms 131580 KB Output is correct
14 Correct 134 ms 131584 KB Output is correct
15 Correct 132 ms 131760 KB Output is correct
16 Correct 127 ms 131632 KB Output is correct
17 Correct 134 ms 131576 KB Output is correct
18 Correct 128 ms 131632 KB Output is correct
19 Correct 130 ms 131760 KB Output is correct
20 Correct 128 ms 131576 KB Output is correct
21 Correct 128 ms 131632 KB Output is correct
22 Correct 133 ms 131636 KB Output is correct
23 Correct 134 ms 131680 KB Output is correct
24 Correct 133 ms 131600 KB Output is correct
25 Correct 130 ms 131604 KB Output is correct
26 Correct 420 ms 148208 KB Output is correct
27 Correct 539 ms 147888 KB Output is correct
28 Correct 139 ms 132272 KB Output is correct
29 Correct 127 ms 131648 KB Output is correct
30 Correct 129 ms 131708 KB Output is correct
31 Correct 620 ms 147836 KB Output is correct
32 Correct 132 ms 132144 KB Output is correct
33 Correct 564 ms 159400 KB Output is correct
34 Correct 621 ms 149368 KB Output is correct
35 Correct 134 ms 132272 KB Output is correct
36 Correct 667 ms 150576 KB Output is correct
37 Correct 132 ms 132144 KB Output is correct
38 Correct 133 ms 132148 KB Output is correct
39 Correct 450 ms 149544 KB Output is correct
40 Correct 133 ms 132400 KB Output is correct
41 Correct 496 ms 149236 KB Output is correct
42 Correct 695 ms 153648 KB Output is correct
43 Correct 131 ms 131632 KB Output is correct
44 Correct 535 ms 159532 KB Output is correct
45 Correct 562 ms 154676 KB Output is correct
46 Correct 132 ms 132144 KB Output is correct
47 Correct 135 ms 132272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 419 ms 148208 KB Output is correct
2 Correct 434 ms 152232 KB Output is correct
3 Correct 136 ms 132144 KB Output is correct
4 Correct 135 ms 132208 KB Output is correct
5 Correct 128 ms 131632 KB Output is correct
6 Correct 129 ms 131632 KB Output is correct
7 Correct 131 ms 132108 KB Output is correct
8 Correct 633 ms 150832 KB Output is correct
9 Correct 131 ms 132148 KB Output is correct
10 Correct 636 ms 149680 KB Output is correct
11 Correct 128 ms 131632 KB Output is correct
12 Correct 660 ms 149552 KB Output is correct
13 Correct 627 ms 150832 KB Output is correct
14 Correct 537 ms 152240 KB Output is correct
15 Correct 440 ms 149616 KB Output is correct
16 Correct 132 ms 132144 KB Output is correct
17 Correct 135 ms 131624 KB Output is correct
18 Correct 488 ms 152304 KB Output is correct
19 Correct 629 ms 164868 KB Output is correct
20 Correct 137 ms 132144 KB Output is correct
21 Correct 133 ms 131632 KB Output is correct
22 Correct 513 ms 150876 KB Output is correct
23 Correct 136 ms 132144 KB Output is correct
24 Correct 621 ms 149936 KB Output is correct
25 Correct 584 ms 158516 KB Output is correct
26 Correct 131 ms 132220 KB Output is correct
27 Correct 135 ms 132400 KB Output is correct
28 Correct 136 ms 132156 KB Output is correct
29 Correct 134 ms 132144 KB Output is correct
30 Correct 134 ms 132144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 131704 KB Output is correct
2 Correct 133 ms 131760 KB Output is correct
3 Correct 131 ms 131632 KB Output is correct
4 Correct 128 ms 131576 KB Output is correct
5 Correct 130 ms 131704 KB Output is correct
6 Correct 131 ms 131632 KB Output is correct
7 Correct 22 ms 29816 KB Output is correct
8 Correct 127 ms 131632 KB Output is correct
9 Correct 127 ms 131576 KB Output is correct
10 Correct 129 ms 131608 KB Output is correct
11 Correct 128 ms 131636 KB Output is correct
12 Correct 127 ms 131632 KB Output is correct
13 Correct 132 ms 131580 KB Output is correct
14 Correct 134 ms 131584 KB Output is correct
15 Correct 132 ms 131760 KB Output is correct
16 Correct 127 ms 131632 KB Output is correct
17 Correct 134 ms 131576 KB Output is correct
18 Correct 128 ms 131632 KB Output is correct
19 Correct 130 ms 131760 KB Output is correct
20 Correct 128 ms 131576 KB Output is correct
21 Correct 128 ms 131632 KB Output is correct
22 Correct 133 ms 131636 KB Output is correct
23 Correct 134 ms 131680 KB Output is correct
24 Correct 133 ms 131600 KB Output is correct
25 Correct 126 ms 131632 KB Output is correct
26 Correct 136 ms 132144 KB Output is correct
27 Correct 136 ms 132272 KB Output is correct
28 Correct 138 ms 132272 KB Output is correct
29 Correct 135 ms 132144 KB Output is correct
30 Correct 137 ms 132088 KB Output is correct
31 Correct 132 ms 131632 KB Output is correct
32 Correct 135 ms 132528 KB Output is correct
33 Correct 128 ms 131632 KB Output is correct
34 Correct 131 ms 132144 KB Output is correct
35 Correct 134 ms 132216 KB Output is correct
36 Correct 134 ms 132068 KB Output is correct
37 Correct 137 ms 132120 KB Output is correct
38 Correct 131 ms 131600 KB Output is correct
39 Correct 137 ms 132120 KB Output is correct
40 Correct 132 ms 132088 KB Output is correct
41 Correct 142 ms 132088 KB Output is correct
42 Correct 133 ms 132144 KB Output is correct
43 Correct 135 ms 132400 KB Output is correct
44 Correct 130 ms 131572 KB Output is correct
45 Correct 139 ms 132400 KB Output is correct
46 Correct 138 ms 132144 KB Output is correct
47 Correct 126 ms 131632 KB Output is correct
48 Correct 133 ms 132088 KB Output is correct
49 Correct 133 ms 132216 KB Output is correct
50 Correct 133 ms 132272 KB Output is correct
51 Correct 136 ms 132144 KB Output is correct
52 Correct 135 ms 132088 KB Output is correct
53 Correct 133 ms 132144 KB Output is correct
54 Correct 135 ms 132132 KB Output is correct
55 Correct 130 ms 131604 KB Output is correct
56 Correct 420 ms 148208 KB Output is correct
57 Correct 539 ms 147888 KB Output is correct
58 Correct 139 ms 132272 KB Output is correct
59 Correct 127 ms 131648 KB Output is correct
60 Correct 129 ms 131708 KB Output is correct
61 Correct 620 ms 147836 KB Output is correct
62 Correct 132 ms 132144 KB Output is correct
63 Correct 564 ms 159400 KB Output is correct
64 Correct 621 ms 149368 KB Output is correct
65 Correct 134 ms 132272 KB Output is correct
66 Correct 667 ms 150576 KB Output is correct
67 Correct 132 ms 132144 KB Output is correct
68 Correct 133 ms 132148 KB Output is correct
69 Correct 450 ms 149544 KB Output is correct
70 Correct 133 ms 132400 KB Output is correct
71 Correct 496 ms 149236 KB Output is correct
72 Correct 695 ms 153648 KB Output is correct
73 Correct 131 ms 131632 KB Output is correct
74 Correct 535 ms 159532 KB Output is correct
75 Correct 562 ms 154676 KB Output is correct
76 Correct 132 ms 132144 KB Output is correct
77 Correct 135 ms 132272 KB Output is correct
78 Correct 419 ms 148208 KB Output is correct
79 Correct 434 ms 152232 KB Output is correct
80 Correct 136 ms 132144 KB Output is correct
81 Correct 135 ms 132208 KB Output is correct
82 Correct 128 ms 131632 KB Output is correct
83 Correct 129 ms 131632 KB Output is correct
84 Correct 131 ms 132108 KB Output is correct
85 Correct 633 ms 150832 KB Output is correct
86 Correct 131 ms 132148 KB Output is correct
87 Correct 636 ms 149680 KB Output is correct
88 Correct 128 ms 131632 KB Output is correct
89 Correct 660 ms 149552 KB Output is correct
90 Correct 627 ms 150832 KB Output is correct
91 Correct 537 ms 152240 KB Output is correct
92 Correct 440 ms 149616 KB Output is correct
93 Correct 132 ms 132144 KB Output is correct
94 Correct 135 ms 131624 KB Output is correct
95 Correct 488 ms 152304 KB Output is correct
96 Correct 629 ms 164868 KB Output is correct
97 Correct 137 ms 132144 KB Output is correct
98 Correct 133 ms 131632 KB Output is correct
99 Correct 513 ms 150876 KB Output is correct
100 Correct 136 ms 132144 KB Output is correct
101 Correct 621 ms 149936 KB Output is correct
102 Correct 584 ms 158516 KB Output is correct
103 Correct 131 ms 132220 KB Output is correct
104 Correct 135 ms 132400 KB Output is correct
105 Correct 136 ms 132156 KB Output is correct
106 Correct 134 ms 132144 KB Output is correct
107 Correct 134 ms 132144 KB Output is correct
108 Correct 2912 ms 228300 KB Output is correct
109 Execution timed out 3067 ms 245616 KB Time limit exceeded
110 Halted 0 ms 0 KB -