Submission #203382

# Submission time Handle Problem Language Result Execution time Memory
203382 2020-02-20T12:28:56 Z theStaticMind Mergers (JOI19_mergers) C++14
34 / 100
498 ms 262148 KB
#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 100000000000000000
#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<vector<int> > cost;
      vector<int> depth;
      vector<int> sub;
      vector<bool> vis;
      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);
            vis = vector<bool> (n, false);
            sub = vector<int> (n, 1);

            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);

                  sub[x] += sub[y];

            }

      }

      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 up(int x, int k){

            while(k){

                  int diff = log2(k&-k);

                  k -= k & -k;

                  x = anc[diff][x];

            }

            return x;

      }

      int dist(int x, int y){

            int l = LCA(x, y);

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

      }

};

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

int num(int x, int y){
      return edge[x * 1e6 + 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 * 1e6 + y] = i;
            edge[y * 1e6 + 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 / 1e6;
            int y = itr->first % 1000000;
            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(long long int, long long int)':
mergers.cpp:45: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 238 ms 247268 KB Output is correct
2 Correct 248 ms 247144 KB Output is correct
3 Correct 246 ms 247268 KB Output is correct
4 Correct 238 ms 247268 KB Output is correct
5 Correct 237 ms 247140 KB Output is correct
6 Correct 243 ms 247280 KB Output is correct
7 Correct 24 ms 35580 KB Output is correct
8 Correct 242 ms 247276 KB Output is correct
9 Correct 239 ms 247144 KB Output is correct
10 Correct 248 ms 247300 KB Output is correct
11 Correct 237 ms 247268 KB Output is correct
12 Correct 233 ms 247268 KB Output is correct
13 Correct 237 ms 247396 KB Output is correct
14 Correct 240 ms 247272 KB Output is correct
15 Correct 237 ms 247272 KB Output is correct
16 Correct 239 ms 247324 KB Output is correct
17 Correct 236 ms 247140 KB Output is correct
18 Correct 241 ms 247272 KB Output is correct
19 Correct 237 ms 247140 KB Output is correct
20 Correct 243 ms 247140 KB Output is correct
21 Correct 236 ms 247268 KB Output is correct
22 Correct 247 ms 247404 KB Output is correct
23 Correct 241 ms 247140 KB Output is correct
24 Correct 239 ms 247268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 247268 KB Output is correct
2 Correct 248 ms 247144 KB Output is correct
3 Correct 246 ms 247268 KB Output is correct
4 Correct 238 ms 247268 KB Output is correct
5 Correct 237 ms 247140 KB Output is correct
6 Correct 243 ms 247280 KB Output is correct
7 Correct 24 ms 35580 KB Output is correct
8 Correct 242 ms 247276 KB Output is correct
9 Correct 239 ms 247144 KB Output is correct
10 Correct 248 ms 247300 KB Output is correct
11 Correct 237 ms 247268 KB Output is correct
12 Correct 233 ms 247268 KB Output is correct
13 Correct 237 ms 247396 KB Output is correct
14 Correct 240 ms 247272 KB Output is correct
15 Correct 237 ms 247272 KB Output is correct
16 Correct 239 ms 247324 KB Output is correct
17 Correct 236 ms 247140 KB Output is correct
18 Correct 241 ms 247272 KB Output is correct
19 Correct 237 ms 247140 KB Output is correct
20 Correct 243 ms 247140 KB Output is correct
21 Correct 236 ms 247268 KB Output is correct
22 Correct 247 ms 247404 KB Output is correct
23 Correct 241 ms 247140 KB Output is correct
24 Correct 239 ms 247268 KB Output is correct
25 Correct 248 ms 247144 KB Output is correct
26 Correct 247 ms 247656 KB Output is correct
27 Correct 242 ms 247652 KB Output is correct
28 Correct 242 ms 247908 KB Output is correct
29 Correct 242 ms 247672 KB Output is correct
30 Correct 242 ms 247652 KB Output is correct
31 Correct 234 ms 247268 KB Output is correct
32 Correct 245 ms 247964 KB Output is correct
33 Correct 236 ms 247272 KB Output is correct
34 Correct 237 ms 247784 KB Output is correct
35 Correct 241 ms 247652 KB Output is correct
36 Correct 238 ms 247652 KB Output is correct
37 Correct 237 ms 247652 KB Output is correct
38 Correct 235 ms 247268 KB Output is correct
39 Correct 242 ms 247656 KB Output is correct
40 Correct 249 ms 247784 KB Output is correct
41 Correct 247 ms 247652 KB Output is correct
42 Correct 247 ms 247708 KB Output is correct
43 Correct 237 ms 247900 KB Output is correct
44 Correct 238 ms 247268 KB Output is correct
45 Correct 241 ms 247776 KB Output is correct
46 Correct 244 ms 247652 KB Output is correct
47 Correct 238 ms 247144 KB Output is correct
48 Correct 240 ms 247652 KB Output is correct
49 Correct 243 ms 247780 KB Output is correct
50 Correct 249 ms 247780 KB Output is correct
51 Correct 245 ms 247780 KB Output is correct
52 Correct 237 ms 247652 KB Output is correct
53 Correct 242 ms 247656 KB Output is correct
54 Correct 255 ms 247656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 247268 KB Output is correct
2 Correct 248 ms 247144 KB Output is correct
3 Correct 246 ms 247268 KB Output is correct
4 Correct 238 ms 247268 KB Output is correct
5 Correct 237 ms 247140 KB Output is correct
6 Correct 243 ms 247280 KB Output is correct
7 Correct 24 ms 35580 KB Output is correct
8 Correct 242 ms 247276 KB Output is correct
9 Correct 239 ms 247144 KB Output is correct
10 Correct 248 ms 247300 KB Output is correct
11 Correct 237 ms 247268 KB Output is correct
12 Correct 233 ms 247268 KB Output is correct
13 Correct 237 ms 247396 KB Output is correct
14 Correct 240 ms 247272 KB Output is correct
15 Correct 237 ms 247272 KB Output is correct
16 Correct 239 ms 247324 KB Output is correct
17 Correct 236 ms 247140 KB Output is correct
18 Correct 241 ms 247272 KB Output is correct
19 Correct 237 ms 247140 KB Output is correct
20 Correct 243 ms 247140 KB Output is correct
21 Correct 236 ms 247268 KB Output is correct
22 Correct 247 ms 247404 KB Output is correct
23 Correct 241 ms 247140 KB Output is correct
24 Correct 239 ms 247268 KB Output is correct
25 Correct 237 ms 247156 KB Output is correct
26 Correct 393 ms 261660 KB Output is correct
27 Correct 480 ms 261292 KB Output is correct
28 Correct 243 ms 247780 KB Output is correct
29 Correct 238 ms 247140 KB Output is correct
30 Correct 240 ms 247272 KB Output is correct
31 Correct 498 ms 261264 KB Output is correct
32 Correct 244 ms 247780 KB Output is correct
33 Runtime error 344 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 383 ms 260256 KB Output is correct
2 Runtime error 306 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 238 ms 247268 KB Output is correct
2 Correct 248 ms 247144 KB Output is correct
3 Correct 246 ms 247268 KB Output is correct
4 Correct 238 ms 247268 KB Output is correct
5 Correct 237 ms 247140 KB Output is correct
6 Correct 243 ms 247280 KB Output is correct
7 Correct 24 ms 35580 KB Output is correct
8 Correct 242 ms 247276 KB Output is correct
9 Correct 239 ms 247144 KB Output is correct
10 Correct 248 ms 247300 KB Output is correct
11 Correct 237 ms 247268 KB Output is correct
12 Correct 233 ms 247268 KB Output is correct
13 Correct 237 ms 247396 KB Output is correct
14 Correct 240 ms 247272 KB Output is correct
15 Correct 237 ms 247272 KB Output is correct
16 Correct 239 ms 247324 KB Output is correct
17 Correct 236 ms 247140 KB Output is correct
18 Correct 241 ms 247272 KB Output is correct
19 Correct 237 ms 247140 KB Output is correct
20 Correct 243 ms 247140 KB Output is correct
21 Correct 236 ms 247268 KB Output is correct
22 Correct 247 ms 247404 KB Output is correct
23 Correct 241 ms 247140 KB Output is correct
24 Correct 239 ms 247268 KB Output is correct
25 Correct 248 ms 247144 KB Output is correct
26 Correct 247 ms 247656 KB Output is correct
27 Correct 242 ms 247652 KB Output is correct
28 Correct 242 ms 247908 KB Output is correct
29 Correct 242 ms 247672 KB Output is correct
30 Correct 242 ms 247652 KB Output is correct
31 Correct 234 ms 247268 KB Output is correct
32 Correct 245 ms 247964 KB Output is correct
33 Correct 236 ms 247272 KB Output is correct
34 Correct 237 ms 247784 KB Output is correct
35 Correct 241 ms 247652 KB Output is correct
36 Correct 238 ms 247652 KB Output is correct
37 Correct 237 ms 247652 KB Output is correct
38 Correct 235 ms 247268 KB Output is correct
39 Correct 242 ms 247656 KB Output is correct
40 Correct 249 ms 247784 KB Output is correct
41 Correct 247 ms 247652 KB Output is correct
42 Correct 247 ms 247708 KB Output is correct
43 Correct 237 ms 247900 KB Output is correct
44 Correct 238 ms 247268 KB Output is correct
45 Correct 241 ms 247776 KB Output is correct
46 Correct 244 ms 247652 KB Output is correct
47 Correct 238 ms 247144 KB Output is correct
48 Correct 240 ms 247652 KB Output is correct
49 Correct 243 ms 247780 KB Output is correct
50 Correct 249 ms 247780 KB Output is correct
51 Correct 245 ms 247780 KB Output is correct
52 Correct 237 ms 247652 KB Output is correct
53 Correct 242 ms 247656 KB Output is correct
54 Correct 255 ms 247656 KB Output is correct
55 Correct 237 ms 247156 KB Output is correct
56 Correct 393 ms 261660 KB Output is correct
57 Correct 480 ms 261292 KB Output is correct
58 Correct 243 ms 247780 KB Output is correct
59 Correct 238 ms 247140 KB Output is correct
60 Correct 240 ms 247272 KB Output is correct
61 Correct 498 ms 261264 KB Output is correct
62 Correct 244 ms 247780 KB Output is correct
63 Runtime error 344 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
64 Halted 0 ms 0 KB -