답안 #203388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203388 2020-02-20T12:47:22 Z theStaticMind Mergers (JOI19_mergers) C++14
100 / 100
1775 ms 247320 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<vector<int> > id(500005);
vector<int> val(500005);
vector<int> comp[500005];
vector<int> up(500005);
vector<int> sub(500005);
vector<bool> virt(500005, false);

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


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

      for(int i = 0; i < adj[x].size(); i++){
            int y = adj[x][i];
            if(y == pre) continue;
            dfs(y, x, depth, i);
            if(depth[up[x]] > depth[up[y]])
            up[x] = up[y];
      }
      if(pre != 0 && up[x] != x){
            virt[num(pre, id)] = true;
      }
}

void get(int x, int pre, int& ans, int root, int id){
      sub[x] = 0;
      if(pre != 0 && virt[num(pre, id)] == false) sub[x] = 1;
      for(int i = 0; i < adj[x].size(); i++){
            int y = adj[x][i];
            if(y == pre)continue;
            get(y, x, ans, root, i);
            sub[x] += sub[y];
      }
      if(pre != 0 && pre != root && virt[num(pre, id)] == 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);
            id[x].pb(i);
            id[y].pb(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, -1);
      int root = 0;
      for(int i = 1; i <= n; i++) {
            for(int j = 0; j < adj[i].size(); j++){
                  int x = i;
                  int y = adj[i][j];
                  int w = num(i, j);
                  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, -1);
      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++){
                            ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'void dfs(int, int, std::vector<int>&, int)':
mergers.cpp:116:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'void get(int, int, int&, int, int)':
mergers.cpp:131:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:179:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 0; j < adj[i].size(); j++){
                            ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 143352 KB Output is correct
2 Correct 137 ms 143408 KB Output is correct
3 Correct 136 ms 143352 KB Output is correct
4 Correct 135 ms 143352 KB Output is correct
5 Correct 138 ms 143352 KB Output is correct
6 Correct 137 ms 143352 KB Output is correct
7 Correct 28 ms 41464 KB Output is correct
8 Correct 135 ms 143352 KB Output is correct
9 Correct 135 ms 143352 KB Output is correct
10 Correct 143 ms 143352 KB Output is correct
11 Correct 153 ms 143356 KB Output is correct
12 Correct 135 ms 143408 KB Output is correct
13 Correct 135 ms 143352 KB Output is correct
14 Correct 141 ms 143352 KB Output is correct
15 Correct 136 ms 143352 KB Output is correct
16 Correct 134 ms 143352 KB Output is correct
17 Correct 134 ms 143352 KB Output is correct
18 Correct 144 ms 143408 KB Output is correct
19 Correct 135 ms 143364 KB Output is correct
20 Correct 135 ms 143352 KB Output is correct
21 Correct 140 ms 143408 KB Output is correct
22 Correct 143 ms 143408 KB Output is correct
23 Correct 135 ms 143304 KB Output is correct
24 Correct 137 ms 143352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 143352 KB Output is correct
2 Correct 137 ms 143408 KB Output is correct
3 Correct 136 ms 143352 KB Output is correct
4 Correct 135 ms 143352 KB Output is correct
5 Correct 138 ms 143352 KB Output is correct
6 Correct 137 ms 143352 KB Output is correct
7 Correct 28 ms 41464 KB Output is correct
8 Correct 135 ms 143352 KB Output is correct
9 Correct 135 ms 143352 KB Output is correct
10 Correct 143 ms 143352 KB Output is correct
11 Correct 153 ms 143356 KB Output is correct
12 Correct 135 ms 143408 KB Output is correct
13 Correct 135 ms 143352 KB Output is correct
14 Correct 141 ms 143352 KB Output is correct
15 Correct 136 ms 143352 KB Output is correct
16 Correct 134 ms 143352 KB Output is correct
17 Correct 134 ms 143352 KB Output is correct
18 Correct 144 ms 143408 KB Output is correct
19 Correct 135 ms 143364 KB Output is correct
20 Correct 135 ms 143352 KB Output is correct
21 Correct 140 ms 143408 KB Output is correct
22 Correct 143 ms 143408 KB Output is correct
23 Correct 135 ms 143304 KB Output is correct
24 Correct 137 ms 143352 KB Output is correct
25 Correct 143 ms 143424 KB Output is correct
26 Correct 138 ms 143608 KB Output is correct
27 Correct 142 ms 143576 KB Output is correct
28 Correct 140 ms 143792 KB Output is correct
29 Correct 139 ms 143960 KB Output is correct
30 Correct 137 ms 143536 KB Output is correct
31 Correct 135 ms 143352 KB Output is correct
32 Correct 141 ms 143920 KB Output is correct
33 Correct 138 ms 143452 KB Output is correct
34 Correct 137 ms 143608 KB Output is correct
35 Correct 142 ms 143664 KB Output is correct
36 Correct 140 ms 143664 KB Output is correct
37 Correct 143 ms 143608 KB Output is correct
38 Correct 137 ms 143408 KB Output is correct
39 Correct 141 ms 143608 KB Output is correct
40 Correct 141 ms 143668 KB Output is correct
41 Correct 144 ms 143608 KB Output is correct
42 Correct 136 ms 143664 KB Output is correct
43 Correct 137 ms 143792 KB Output is correct
44 Correct 146 ms 143480 KB Output is correct
45 Correct 142 ms 143792 KB Output is correct
46 Correct 143 ms 143608 KB Output is correct
47 Correct 139 ms 143352 KB Output is correct
48 Correct 141 ms 143744 KB Output is correct
49 Correct 141 ms 143664 KB Output is correct
50 Correct 139 ms 143792 KB Output is correct
51 Correct 140 ms 143536 KB Output is correct
52 Correct 139 ms 143788 KB Output is correct
53 Correct 142 ms 143736 KB Output is correct
54 Correct 141 ms 143536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 143352 KB Output is correct
2 Correct 137 ms 143408 KB Output is correct
3 Correct 136 ms 143352 KB Output is correct
4 Correct 135 ms 143352 KB Output is correct
5 Correct 138 ms 143352 KB Output is correct
6 Correct 137 ms 143352 KB Output is correct
7 Correct 28 ms 41464 KB Output is correct
8 Correct 135 ms 143352 KB Output is correct
9 Correct 135 ms 143352 KB Output is correct
10 Correct 143 ms 143352 KB Output is correct
11 Correct 153 ms 143356 KB Output is correct
12 Correct 135 ms 143408 KB Output is correct
13 Correct 135 ms 143352 KB Output is correct
14 Correct 141 ms 143352 KB Output is correct
15 Correct 136 ms 143352 KB Output is correct
16 Correct 134 ms 143352 KB Output is correct
17 Correct 134 ms 143352 KB Output is correct
18 Correct 144 ms 143408 KB Output is correct
19 Correct 135 ms 143364 KB Output is correct
20 Correct 135 ms 143352 KB Output is correct
21 Correct 140 ms 143408 KB Output is correct
22 Correct 143 ms 143408 KB Output is correct
23 Correct 135 ms 143304 KB Output is correct
24 Correct 137 ms 143352 KB Output is correct
25 Correct 139 ms 143352 KB Output is correct
26 Correct 223 ms 150944 KB Output is correct
27 Correct 281 ms 150392 KB Output is correct
28 Correct 139 ms 143608 KB Output is correct
29 Correct 141 ms 143480 KB Output is correct
30 Correct 138 ms 143408 KB Output is correct
31 Correct 305 ms 150240 KB Output is correct
32 Correct 139 ms 143724 KB Output is correct
33 Correct 341 ms 157464 KB Output is correct
34 Correct 286 ms 150392 KB Output is correct
35 Correct 138 ms 143536 KB Output is correct
36 Correct 330 ms 151088 KB Output is correct
37 Correct 139 ms 143536 KB Output is correct
38 Correct 141 ms 143632 KB Output is correct
39 Correct 221 ms 150888 KB Output is correct
40 Correct 136 ms 143792 KB Output is correct
41 Correct 277 ms 150384 KB Output is correct
42 Correct 349 ms 153396 KB Output is correct
43 Correct 137 ms 143412 KB Output is correct
44 Correct 336 ms 157612 KB Output is correct
45 Correct 317 ms 153904 KB Output is correct
46 Correct 144 ms 143608 KB Output is correct
47 Correct 141 ms 143668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 223 ms 150944 KB Output is correct
2 Correct 242 ms 153652 KB Output is correct
3 Correct 140 ms 143672 KB Output is correct
4 Correct 145 ms 143536 KB Output is correct
5 Correct 140 ms 143352 KB Output is correct
6 Correct 142 ms 143408 KB Output is correct
7 Correct 138 ms 143608 KB Output is correct
8 Correct 331 ms 151376 KB Output is correct
9 Correct 141 ms 143536 KB Output is correct
10 Correct 289 ms 150448 KB Output is correct
11 Correct 135 ms 143352 KB Output is correct
12 Correct 331 ms 150320 KB Output is correct
13 Correct 326 ms 151288 KB Output is correct
14 Correct 298 ms 153084 KB Output is correct
15 Correct 213 ms 151016 KB Output is correct
16 Correct 136 ms 143608 KB Output is correct
17 Correct 137 ms 143352 KB Output is correct
18 Correct 252 ms 153304 KB Output is correct
19 Correct 345 ms 162096 KB Output is correct
20 Correct 138 ms 143608 KB Output is correct
21 Correct 135 ms 143360 KB Output is correct
22 Correct 250 ms 151968 KB Output is correct
23 Correct 141 ms 143540 KB Output is correct
24 Correct 335 ms 150576 KB Output is correct
25 Correct 310 ms 157360 KB Output is correct
26 Correct 139 ms 143664 KB Output is correct
27 Correct 142 ms 143664 KB Output is correct
28 Correct 137 ms 143540 KB Output is correct
29 Correct 141 ms 143608 KB Output is correct
30 Correct 142 ms 143792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 143352 KB Output is correct
2 Correct 137 ms 143408 KB Output is correct
3 Correct 136 ms 143352 KB Output is correct
4 Correct 135 ms 143352 KB Output is correct
5 Correct 138 ms 143352 KB Output is correct
6 Correct 137 ms 143352 KB Output is correct
7 Correct 28 ms 41464 KB Output is correct
8 Correct 135 ms 143352 KB Output is correct
9 Correct 135 ms 143352 KB Output is correct
10 Correct 143 ms 143352 KB Output is correct
11 Correct 153 ms 143356 KB Output is correct
12 Correct 135 ms 143408 KB Output is correct
13 Correct 135 ms 143352 KB Output is correct
14 Correct 141 ms 143352 KB Output is correct
15 Correct 136 ms 143352 KB Output is correct
16 Correct 134 ms 143352 KB Output is correct
17 Correct 134 ms 143352 KB Output is correct
18 Correct 144 ms 143408 KB Output is correct
19 Correct 135 ms 143364 KB Output is correct
20 Correct 135 ms 143352 KB Output is correct
21 Correct 140 ms 143408 KB Output is correct
22 Correct 143 ms 143408 KB Output is correct
23 Correct 135 ms 143304 KB Output is correct
24 Correct 137 ms 143352 KB Output is correct
25 Correct 143 ms 143424 KB Output is correct
26 Correct 138 ms 143608 KB Output is correct
27 Correct 142 ms 143576 KB Output is correct
28 Correct 140 ms 143792 KB Output is correct
29 Correct 139 ms 143960 KB Output is correct
30 Correct 137 ms 143536 KB Output is correct
31 Correct 135 ms 143352 KB Output is correct
32 Correct 141 ms 143920 KB Output is correct
33 Correct 138 ms 143452 KB Output is correct
34 Correct 137 ms 143608 KB Output is correct
35 Correct 142 ms 143664 KB Output is correct
36 Correct 140 ms 143664 KB Output is correct
37 Correct 143 ms 143608 KB Output is correct
38 Correct 137 ms 143408 KB Output is correct
39 Correct 141 ms 143608 KB Output is correct
40 Correct 141 ms 143668 KB Output is correct
41 Correct 144 ms 143608 KB Output is correct
42 Correct 136 ms 143664 KB Output is correct
43 Correct 137 ms 143792 KB Output is correct
44 Correct 146 ms 143480 KB Output is correct
45 Correct 142 ms 143792 KB Output is correct
46 Correct 143 ms 143608 KB Output is correct
47 Correct 139 ms 143352 KB Output is correct
48 Correct 141 ms 143744 KB Output is correct
49 Correct 141 ms 143664 KB Output is correct
50 Correct 139 ms 143792 KB Output is correct
51 Correct 140 ms 143536 KB Output is correct
52 Correct 139 ms 143788 KB Output is correct
53 Correct 142 ms 143736 KB Output is correct
54 Correct 141 ms 143536 KB Output is correct
55 Correct 139 ms 143352 KB Output is correct
56 Correct 223 ms 150944 KB Output is correct
57 Correct 281 ms 150392 KB Output is correct
58 Correct 139 ms 143608 KB Output is correct
59 Correct 141 ms 143480 KB Output is correct
60 Correct 138 ms 143408 KB Output is correct
61 Correct 305 ms 150240 KB Output is correct
62 Correct 139 ms 143724 KB Output is correct
63 Correct 341 ms 157464 KB Output is correct
64 Correct 286 ms 150392 KB Output is correct
65 Correct 138 ms 143536 KB Output is correct
66 Correct 330 ms 151088 KB Output is correct
67 Correct 139 ms 143536 KB Output is correct
68 Correct 141 ms 143632 KB Output is correct
69 Correct 221 ms 150888 KB Output is correct
70 Correct 136 ms 143792 KB Output is correct
71 Correct 277 ms 150384 KB Output is correct
72 Correct 349 ms 153396 KB Output is correct
73 Correct 137 ms 143412 KB Output is correct
74 Correct 336 ms 157612 KB Output is correct
75 Correct 317 ms 153904 KB Output is correct
76 Correct 144 ms 143608 KB Output is correct
77 Correct 141 ms 143668 KB Output is correct
78 Correct 223 ms 150944 KB Output is correct
79 Correct 242 ms 153652 KB Output is correct
80 Correct 140 ms 143672 KB Output is correct
81 Correct 145 ms 143536 KB Output is correct
82 Correct 140 ms 143352 KB Output is correct
83 Correct 142 ms 143408 KB Output is correct
84 Correct 138 ms 143608 KB Output is correct
85 Correct 331 ms 151376 KB Output is correct
86 Correct 141 ms 143536 KB Output is correct
87 Correct 289 ms 150448 KB Output is correct
88 Correct 135 ms 143352 KB Output is correct
89 Correct 331 ms 150320 KB Output is correct
90 Correct 326 ms 151288 KB Output is correct
91 Correct 298 ms 153084 KB Output is correct
92 Correct 213 ms 151016 KB Output is correct
93 Correct 136 ms 143608 KB Output is correct
94 Correct 137 ms 143352 KB Output is correct
95 Correct 252 ms 153304 KB Output is correct
96 Correct 345 ms 162096 KB Output is correct
97 Correct 138 ms 143608 KB Output is correct
98 Correct 135 ms 143360 KB Output is correct
99 Correct 250 ms 151968 KB Output is correct
100 Correct 141 ms 143540 KB Output is correct
101 Correct 335 ms 150576 KB Output is correct
102 Correct 310 ms 157360 KB Output is correct
103 Correct 139 ms 143664 KB Output is correct
104 Correct 142 ms 143664 KB Output is correct
105 Correct 137 ms 143540 KB Output is correct
106 Correct 141 ms 143608 KB Output is correct
107 Correct 142 ms 143792 KB Output is correct
108 Correct 1008 ms 185108 KB Output is correct
109 Correct 1775 ms 195284 KB Output is correct
110 Correct 1648 ms 214176 KB Output is correct
111 Correct 1652 ms 247320 KB Output is correct
112 Correct 1551 ms 224048 KB Output is correct
113 Correct 979 ms 202756 KB Output is correct
114 Correct 1265 ms 185572 KB Output is correct
115 Correct 1185 ms 185612 KB Output is correct
116 Correct 1619 ms 190428 KB Output is correct
117 Correct 1420 ms 201464 KB Output is correct
118 Correct 1285 ms 186652 KB Output is correct
119 Correct 1441 ms 201540 KB Output is correct
120 Correct 1571 ms 223920 KB Output is correct
121 Correct 1435 ms 201464 KB Output is correct
122 Correct 1607 ms 194992 KB Output is correct
123 Correct 946 ms 204300 KB Output is correct
124 Correct 1634 ms 200004 KB Output is correct