Submission #332893

# Submission time Handle Problem Language Result Execution time Memory
332893 2020-12-04T00:17:17 Z caoash Mergers (JOI19_mergers) C++17
100 / 100
1785 ms 202836 KB
#include <bits/stdc++.h> 
using namespace std;

using ll = long long;

using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

const int MX = 500005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;

namespace output {
  void pr(int x) {
    cout << x;
  }
  void pr(long x) {
    cout << x;
  }
  void pr(ll x) {
    cout << x;
  }
  void pr(unsigned x) {
    cout << x;
  }
  void pr(unsigned long x) {
    cout << x;
  }
  void pr(unsigned long long x) {
    cout << x;
  }
  void pr(float x) {
    cout << x;
  }
  void pr(double x) {
    cout << x;
  }
  void pr(long double x) {
    cout << x;
  }
  void pr(char x) {
    cout << x;
  }
  void pr(const char * x) {
    cout << x;
  }
  void pr(const string & x) {
    cout << x;
  }
  void pr(bool x) {
    pr(x ? "true" : "false");
  }

  template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
  template < class T > void pr(const T & x);

  template < class T, class...Ts > void pr(const T & t,
    const Ts & ...ts) {
    pr(t);
    pr(ts...);
  }
  template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
    pr("{", x.f, ", ", x.s, "}");
  }
  template < class T > void pr(const T & x) {
    pr("{"); // const iterator needed for vector<bool>
    bool fst = 1;
    for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
    pr("}");
  }

  void ps() {
    pr("\n");
  } // print w/ spaces
  template < class T, class...Ts > void ps(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(" ");
    ps(ts...);
  }

  void pc() {
    cout << "]" << endl;
  } // debug w/ commas
  template < class T, class...Ts > void pc(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(", ");
    pc(ts...);
  }
  // #define dbg(x...) pr("[", #x, "] = ["), pc(x);
}

#ifdef mikey 
using namespace output;
#else
using namespace output;
// #define dbg(x...)
#endif

template < int SZ > struct LCA {
  int depth[SZ];
  int p[SZ][33];
  vector < int > adj[SZ];
  void addEdge(int u, int v) {
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  void dfs(int v, int par) {
    for (int to: adj[v]) {
      if (to != par) {
        p[to][0] = v;
        depth[to] = depth[v] + 1;
        dfs(to, v);
      }
    }
  }
  void precomp() {
    for (int i = 0; i < SZ; i++) {
      for (int j = 0; j < 32; j++) {
        p[i][j] = -1;
      }
    }
    p[0][0] = 0;
    dfs(0, -1);
    for (int j = 1; j < 32; j++) {
      for (int i = 0; i < SZ; i++) {
        if (p[i][j - 1] == -1) {
          p[i][j] = -1;
        } else {
          p[i][j] = p[p[i][j - 1]][j - 1];
        }
      }
    }
  }
  int query(int a, int b) {
    if (depth[a] > depth[b]) {
      swap(a, b);
    }
    int lift = depth[b] - depth[a];
    for (int j = 31; j >= 0; j--) {
      if (lift & (1 << j)) {
        b = p[b][j];
      }
    }
    for (int j = 31; j >= 0; j--) {
      if (p[a][j] != p[b][j]) {
        a = p[a][j];
        b = p[b][j];
      }
    }
    return (a == b) ? a : p[a][0];
  }
};

template < int SZ > struct DSU {
  int p[SZ], sz[SZ];
  void init() {
    for (int i = 0; i < SZ; i++) {
      p[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    return p[x] = (p[x] == x ? x : find(p[x]));
  }
  void merge(int u, int v) {
    int a = find(u);
    int b = find(v);
    if (a != b) {
      if (sz[a] < sz[b]) {
        swap(a, b);
      }
      p[b] = a;
      sz[a] += sz[b];
    }
  }
};

vi adj[MX], sadj[MX];
int col[MX];
int pull[MX], dep[MX];
vi nods[MX];
LCA<MX> anc;
DSU<MX> dsu;
int deg[MX];

void dfs_pull(int v, int p) {
    for (int to : adj[v]) {
        if (to != p) {
            dep[to] = dep[v] + 1;
            dfs_pull(to, v);
            pull[v] += pull[to]; 
        }
    }
}

int main(){
#ifdef mikey 
    freopen("a.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k; cin >> n >> k;
    vector<pi> eds;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        u--, v--;
        adj[u].pb(v), adj[v].pb(u);
        anc.addEdge(u, v);
        eds.pb(mp(u, v));
    }
    anc.precomp();
    auto upd = [&] (int i, int j) {
        // dbg(i, j);
        int lca = anc.query(i, j);
        pull[i]++, pull[j]++;
        pull[lca] -= 2;
    };
    for (int i = 0; i < n; i++) {
        cin >> col[i];
        col[i]--;
        nods[col[i]].pb(i);
        // dbg(i, col[i]);
    }
    for (int c = 0; c < k; c++) {
        if (nods[c].empty()) continue;
        // dbg(c, nods[c]);
        int fir = nods[c][0];
        for (int j = 1; j < sz(nods[c]); j++) {
            int sec = nods[c][j]; 
            upd(fir, sec);
        }
    }
    dfs_pull(0, -1);
    dsu.init();
    for (auto x : eds) {
        if (dep[x.f] > dep[x.s]) swap(x.f, x.s);
        if (pull[x.s]) {
            dsu.merge(x.f, x.s);
        } 
    }
    for (auto x : eds) {
        if (dep[x.f] > dep[x.s]) swap(x.f, x.s);
        if (pull[x.s]) continue;
        int u = dsu.find(x.f), v = dsu.find(x.s);
        deg[u]++, deg[v]++;
        // dbg(x.f + 1, x.s + 1, pull[x.s]);
    }
    set<int> leaf;
    for (int i = 0; i < n; i++) {
        if (deg[dsu.find(i)] == 1) leaf.insert(dsu.find(i));
    }
    // dbg(leaf);
    cout << ((sz(leaf) + 1) / 2) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 265 ms 115820 KB Output is correct
2 Correct 265 ms 115820 KB Output is correct
3 Correct 272 ms 115820 KB Output is correct
4 Correct 266 ms 115820 KB Output is correct
5 Correct 270 ms 115948 KB Output is correct
6 Correct 269 ms 115820 KB Output is correct
7 Correct 268 ms 115820 KB Output is correct
8 Correct 268 ms 115820 KB Output is correct
9 Correct 266 ms 115820 KB Output is correct
10 Correct 269 ms 115852 KB Output is correct
11 Correct 269 ms 115820 KB Output is correct
12 Correct 274 ms 115820 KB Output is correct
13 Correct 268 ms 115820 KB Output is correct
14 Correct 269 ms 115780 KB Output is correct
15 Correct 266 ms 115820 KB Output is correct
16 Correct 271 ms 115948 KB Output is correct
17 Correct 264 ms 115804 KB Output is correct
18 Correct 266 ms 115820 KB Output is correct
19 Correct 272 ms 115960 KB Output is correct
20 Correct 266 ms 115820 KB Output is correct
21 Correct 264 ms 115900 KB Output is correct
22 Correct 264 ms 115968 KB Output is correct
23 Correct 266 ms 115836 KB Output is correct
24 Correct 272 ms 115804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 265 ms 115820 KB Output is correct
2 Correct 265 ms 115820 KB Output is correct
3 Correct 272 ms 115820 KB Output is correct
4 Correct 266 ms 115820 KB Output is correct
5 Correct 270 ms 115948 KB Output is correct
6 Correct 269 ms 115820 KB Output is correct
7 Correct 268 ms 115820 KB Output is correct
8 Correct 268 ms 115820 KB Output is correct
9 Correct 266 ms 115820 KB Output is correct
10 Correct 269 ms 115852 KB Output is correct
11 Correct 269 ms 115820 KB Output is correct
12 Correct 274 ms 115820 KB Output is correct
13 Correct 268 ms 115820 KB Output is correct
14 Correct 269 ms 115780 KB Output is correct
15 Correct 266 ms 115820 KB Output is correct
16 Correct 271 ms 115948 KB Output is correct
17 Correct 264 ms 115804 KB Output is correct
18 Correct 266 ms 115820 KB Output is correct
19 Correct 272 ms 115960 KB Output is correct
20 Correct 266 ms 115820 KB Output is correct
21 Correct 264 ms 115900 KB Output is correct
22 Correct 264 ms 115968 KB Output is correct
23 Correct 266 ms 115836 KB Output is correct
24 Correct 272 ms 115804 KB Output is correct
25 Correct 272 ms 115820 KB Output is correct
26 Correct 274 ms 116204 KB Output is correct
27 Correct 268 ms 116204 KB Output is correct
28 Correct 271 ms 116460 KB Output is correct
29 Correct 272 ms 116460 KB Output is correct
30 Correct 270 ms 116204 KB Output is correct
31 Correct 271 ms 115820 KB Output is correct
32 Correct 269 ms 116332 KB Output is correct
33 Correct 265 ms 115820 KB Output is correct
34 Correct 270 ms 116196 KB Output is correct
35 Correct 274 ms 116332 KB Output is correct
36 Correct 273 ms 116204 KB Output is correct
37 Correct 277 ms 116204 KB Output is correct
38 Correct 268 ms 115820 KB Output is correct
39 Correct 268 ms 116204 KB Output is correct
40 Correct 268 ms 116140 KB Output is correct
41 Correct 272 ms 116212 KB Output is correct
42 Correct 265 ms 116204 KB Output is correct
43 Correct 274 ms 116332 KB Output is correct
44 Correct 264 ms 115820 KB Output is correct
45 Correct 273 ms 116200 KB Output is correct
46 Correct 275 ms 116332 KB Output is correct
47 Correct 271 ms 115832 KB Output is correct
48 Correct 271 ms 116204 KB Output is correct
49 Correct 283 ms 116332 KB Output is correct
50 Correct 271 ms 116332 KB Output is correct
51 Correct 282 ms 116332 KB Output is correct
52 Correct 273 ms 116204 KB Output is correct
53 Correct 275 ms 116204 KB Output is correct
54 Correct 284 ms 116204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 265 ms 115820 KB Output is correct
2 Correct 265 ms 115820 KB Output is correct
3 Correct 272 ms 115820 KB Output is correct
4 Correct 266 ms 115820 KB Output is correct
5 Correct 270 ms 115948 KB Output is correct
6 Correct 269 ms 115820 KB Output is correct
7 Correct 268 ms 115820 KB Output is correct
8 Correct 268 ms 115820 KB Output is correct
9 Correct 266 ms 115820 KB Output is correct
10 Correct 269 ms 115852 KB Output is correct
11 Correct 269 ms 115820 KB Output is correct
12 Correct 274 ms 115820 KB Output is correct
13 Correct 268 ms 115820 KB Output is correct
14 Correct 269 ms 115780 KB Output is correct
15 Correct 266 ms 115820 KB Output is correct
16 Correct 271 ms 115948 KB Output is correct
17 Correct 264 ms 115804 KB Output is correct
18 Correct 266 ms 115820 KB Output is correct
19 Correct 272 ms 115960 KB Output is correct
20 Correct 266 ms 115820 KB Output is correct
21 Correct 264 ms 115900 KB Output is correct
22 Correct 264 ms 115968 KB Output is correct
23 Correct 266 ms 115836 KB Output is correct
24 Correct 272 ms 115804 KB Output is correct
25 Correct 272 ms 115820 KB Output is correct
26 Correct 403 ms 125904 KB Output is correct
27 Correct 456 ms 125276 KB Output is correct
28 Correct 278 ms 116204 KB Output is correct
29 Correct 267 ms 115820 KB Output is correct
30 Correct 270 ms 115820 KB Output is correct
31 Correct 449 ms 125280 KB Output is correct
32 Correct 273 ms 116204 KB Output is correct
33 Correct 497 ms 132192 KB Output is correct
34 Correct 454 ms 125440 KB Output is correct
35 Correct 276 ms 116172 KB Output is correct
36 Correct 481 ms 126052 KB Output is correct
37 Correct 273 ms 116076 KB Output is correct
38 Correct 273 ms 116204 KB Output is correct
39 Correct 395 ms 125916 KB Output is correct
40 Correct 272 ms 116460 KB Output is correct
41 Correct 438 ms 125536 KB Output is correct
42 Correct 512 ms 127444 KB Output is correct
43 Correct 266 ms 115812 KB Output is correct
44 Correct 487 ms 132976 KB Output is correct
45 Correct 494 ms 128864 KB Output is correct
46 Correct 274 ms 116204 KB Output is correct
47 Correct 274 ms 116076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 396 ms 125904 KB Output is correct
2 Correct 385 ms 133072 KB Output is correct
3 Correct 277 ms 116332 KB Output is correct
4 Correct 273 ms 116292 KB Output is correct
5 Correct 270 ms 115820 KB Output is correct
6 Correct 272 ms 115820 KB Output is correct
7 Correct 272 ms 116204 KB Output is correct
8 Correct 458 ms 127200 KB Output is correct
9 Correct 273 ms 116076 KB Output is correct
10 Correct 456 ms 125664 KB Output is correct
11 Correct 272 ms 115820 KB Output is correct
12 Correct 479 ms 125536 KB Output is correct
13 Correct 449 ms 126948 KB Output is correct
14 Correct 425 ms 130508 KB Output is correct
15 Correct 404 ms 125768 KB Output is correct
16 Correct 277 ms 116204 KB Output is correct
17 Correct 281 ms 115812 KB Output is correct
18 Correct 398 ms 132316 KB Output is correct
19 Correct 445 ms 132204 KB Output is correct
20 Correct 271 ms 116204 KB Output is correct
21 Correct 269 ms 115820 KB Output is correct
22 Correct 391 ms 128208 KB Output is correct
23 Correct 276 ms 116204 KB Output is correct
24 Correct 463 ms 126048 KB Output is correct
25 Correct 442 ms 133024 KB Output is correct
26 Correct 270 ms 116460 KB Output is correct
27 Correct 273 ms 116332 KB Output is correct
28 Correct 275 ms 116204 KB Output is correct
29 Correct 275 ms 116204 KB Output is correct
30 Correct 277 ms 116204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 265 ms 115820 KB Output is correct
2 Correct 265 ms 115820 KB Output is correct
3 Correct 272 ms 115820 KB Output is correct
4 Correct 266 ms 115820 KB Output is correct
5 Correct 270 ms 115948 KB Output is correct
6 Correct 269 ms 115820 KB Output is correct
7 Correct 268 ms 115820 KB Output is correct
8 Correct 268 ms 115820 KB Output is correct
9 Correct 266 ms 115820 KB Output is correct
10 Correct 269 ms 115852 KB Output is correct
11 Correct 269 ms 115820 KB Output is correct
12 Correct 274 ms 115820 KB Output is correct
13 Correct 268 ms 115820 KB Output is correct
14 Correct 269 ms 115780 KB Output is correct
15 Correct 266 ms 115820 KB Output is correct
16 Correct 271 ms 115948 KB Output is correct
17 Correct 264 ms 115804 KB Output is correct
18 Correct 266 ms 115820 KB Output is correct
19 Correct 272 ms 115960 KB Output is correct
20 Correct 266 ms 115820 KB Output is correct
21 Correct 264 ms 115900 KB Output is correct
22 Correct 264 ms 115968 KB Output is correct
23 Correct 266 ms 115836 KB Output is correct
24 Correct 272 ms 115804 KB Output is correct
25 Correct 272 ms 115820 KB Output is correct
26 Correct 274 ms 116204 KB Output is correct
27 Correct 268 ms 116204 KB Output is correct
28 Correct 271 ms 116460 KB Output is correct
29 Correct 272 ms 116460 KB Output is correct
30 Correct 270 ms 116204 KB Output is correct
31 Correct 271 ms 115820 KB Output is correct
32 Correct 269 ms 116332 KB Output is correct
33 Correct 265 ms 115820 KB Output is correct
34 Correct 270 ms 116196 KB Output is correct
35 Correct 274 ms 116332 KB Output is correct
36 Correct 273 ms 116204 KB Output is correct
37 Correct 277 ms 116204 KB Output is correct
38 Correct 268 ms 115820 KB Output is correct
39 Correct 268 ms 116204 KB Output is correct
40 Correct 268 ms 116140 KB Output is correct
41 Correct 272 ms 116212 KB Output is correct
42 Correct 265 ms 116204 KB Output is correct
43 Correct 274 ms 116332 KB Output is correct
44 Correct 264 ms 115820 KB Output is correct
45 Correct 273 ms 116200 KB Output is correct
46 Correct 275 ms 116332 KB Output is correct
47 Correct 271 ms 115832 KB Output is correct
48 Correct 271 ms 116204 KB Output is correct
49 Correct 283 ms 116332 KB Output is correct
50 Correct 271 ms 116332 KB Output is correct
51 Correct 282 ms 116332 KB Output is correct
52 Correct 273 ms 116204 KB Output is correct
53 Correct 275 ms 116204 KB Output is correct
54 Correct 284 ms 116204 KB Output is correct
55 Correct 272 ms 115820 KB Output is correct
56 Correct 403 ms 125904 KB Output is correct
57 Correct 456 ms 125276 KB Output is correct
58 Correct 278 ms 116204 KB Output is correct
59 Correct 267 ms 115820 KB Output is correct
60 Correct 270 ms 115820 KB Output is correct
61 Correct 449 ms 125280 KB Output is correct
62 Correct 273 ms 116204 KB Output is correct
63 Correct 497 ms 132192 KB Output is correct
64 Correct 454 ms 125440 KB Output is correct
65 Correct 276 ms 116172 KB Output is correct
66 Correct 481 ms 126052 KB Output is correct
67 Correct 273 ms 116076 KB Output is correct
68 Correct 273 ms 116204 KB Output is correct
69 Correct 395 ms 125916 KB Output is correct
70 Correct 272 ms 116460 KB Output is correct
71 Correct 438 ms 125536 KB Output is correct
72 Correct 512 ms 127444 KB Output is correct
73 Correct 266 ms 115812 KB Output is correct
74 Correct 487 ms 132976 KB Output is correct
75 Correct 494 ms 128864 KB Output is correct
76 Correct 274 ms 116204 KB Output is correct
77 Correct 274 ms 116076 KB Output is correct
78 Correct 396 ms 125904 KB Output is correct
79 Correct 385 ms 133072 KB Output is correct
80 Correct 277 ms 116332 KB Output is correct
81 Correct 273 ms 116292 KB Output is correct
82 Correct 270 ms 115820 KB Output is correct
83 Correct 272 ms 115820 KB Output is correct
84 Correct 272 ms 116204 KB Output is correct
85 Correct 458 ms 127200 KB Output is correct
86 Correct 273 ms 116076 KB Output is correct
87 Correct 456 ms 125664 KB Output is correct
88 Correct 272 ms 115820 KB Output is correct
89 Correct 479 ms 125536 KB Output is correct
90 Correct 449 ms 126948 KB Output is correct
91 Correct 425 ms 130508 KB Output is correct
92 Correct 404 ms 125768 KB Output is correct
93 Correct 277 ms 116204 KB Output is correct
94 Correct 281 ms 115812 KB Output is correct
95 Correct 398 ms 132316 KB Output is correct
96 Correct 445 ms 132204 KB Output is correct
97 Correct 271 ms 116204 KB Output is correct
98 Correct 269 ms 115820 KB Output is correct
99 Correct 391 ms 128208 KB Output is correct
100 Correct 276 ms 116204 KB Output is correct
101 Correct 463 ms 126048 KB Output is correct
102 Correct 442 ms 133024 KB Output is correct
103 Correct 270 ms 116460 KB Output is correct
104 Correct 273 ms 116332 KB Output is correct
105 Correct 275 ms 116204 KB Output is correct
106 Correct 275 ms 116204 KB Output is correct
107 Correct 277 ms 116204 KB Output is correct
108 Correct 1125 ms 173368 KB Output is correct
109 Correct 1785 ms 181308 KB Output is correct
110 Correct 1713 ms 192852 KB Output is correct
111 Correct 1503 ms 200148 KB Output is correct
112 Correct 1530 ms 202172 KB Output is correct
113 Correct 1262 ms 197860 KB Output is correct
114 Correct 1408 ms 163592 KB Output is correct
115 Correct 1364 ms 164308 KB Output is correct
116 Correct 1570 ms 166764 KB Output is correct
117 Correct 1383 ms 189328 KB Output is correct
118 Correct 1499 ms 163796 KB Output is correct
119 Correct 1375 ms 189464 KB Output is correct
120 Correct 1500 ms 202836 KB Output is correct
121 Correct 1392 ms 189396 KB Output is correct
122 Correct 1501 ms 175188 KB Output is correct
123 Correct 1075 ms 202008 KB Output is correct
124 Correct 1466 ms 175912 KB Output is correct