Submission #332892

# Submission time Handle Problem Language Result Execution time Memory
332892 2020-12-04T00:16:11 Z caoash Mergers (JOI19_mergers) C++17
70 / 100
337 ms 63952 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 = 200005;
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 111 ms 46444 KB Output is correct
2 Correct 112 ms 46572 KB Output is correct
3 Correct 108 ms 46572 KB Output is correct
4 Correct 109 ms 46572 KB Output is correct
5 Correct 111 ms 46572 KB Output is correct
6 Correct 108 ms 46700 KB Output is correct
7 Correct 113 ms 46572 KB Output is correct
8 Correct 110 ms 46572 KB Output is correct
9 Correct 107 ms 46572 KB Output is correct
10 Correct 110 ms 46572 KB Output is correct
11 Correct 109 ms 46572 KB Output is correct
12 Correct 110 ms 46572 KB Output is correct
13 Correct 111 ms 46592 KB Output is correct
14 Correct 110 ms 46572 KB Output is correct
15 Correct 109 ms 46572 KB Output is correct
16 Correct 111 ms 46572 KB Output is correct
17 Correct 108 ms 46536 KB Output is correct
18 Correct 109 ms 46572 KB Output is correct
19 Correct 109 ms 46572 KB Output is correct
20 Correct 108 ms 46584 KB Output is correct
21 Correct 110 ms 46572 KB Output is correct
22 Correct 111 ms 46700 KB Output is correct
23 Correct 112 ms 46572 KB Output is correct
24 Correct 111 ms 46700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 46444 KB Output is correct
2 Correct 112 ms 46572 KB Output is correct
3 Correct 108 ms 46572 KB Output is correct
4 Correct 109 ms 46572 KB Output is correct
5 Correct 111 ms 46572 KB Output is correct
6 Correct 108 ms 46700 KB Output is correct
7 Correct 113 ms 46572 KB Output is correct
8 Correct 110 ms 46572 KB Output is correct
9 Correct 107 ms 46572 KB Output is correct
10 Correct 110 ms 46572 KB Output is correct
11 Correct 109 ms 46572 KB Output is correct
12 Correct 110 ms 46572 KB Output is correct
13 Correct 111 ms 46592 KB Output is correct
14 Correct 110 ms 46572 KB Output is correct
15 Correct 109 ms 46572 KB Output is correct
16 Correct 111 ms 46572 KB Output is correct
17 Correct 108 ms 46536 KB Output is correct
18 Correct 109 ms 46572 KB Output is correct
19 Correct 109 ms 46572 KB Output is correct
20 Correct 108 ms 46584 KB Output is correct
21 Correct 110 ms 46572 KB Output is correct
22 Correct 111 ms 46700 KB Output is correct
23 Correct 112 ms 46572 KB Output is correct
24 Correct 111 ms 46700 KB Output is correct
25 Correct 111 ms 46572 KB Output is correct
26 Correct 110 ms 46956 KB Output is correct
27 Correct 115 ms 46956 KB Output is correct
28 Correct 120 ms 47340 KB Output is correct
29 Correct 112 ms 47084 KB Output is correct
30 Correct 113 ms 47084 KB Output is correct
31 Correct 110 ms 46700 KB Output is correct
32 Correct 118 ms 47084 KB Output is correct
33 Correct 109 ms 46700 KB Output is correct
34 Correct 113 ms 46956 KB Output is correct
35 Correct 115 ms 47084 KB Output is correct
36 Correct 112 ms 46956 KB Output is correct
37 Correct 111 ms 46956 KB Output is correct
38 Correct 108 ms 46572 KB Output is correct
39 Correct 111 ms 46956 KB Output is correct
40 Correct 113 ms 46828 KB Output is correct
41 Correct 122 ms 46956 KB Output is correct
42 Correct 111 ms 47104 KB Output is correct
43 Correct 116 ms 47084 KB Output is correct
44 Correct 118 ms 46572 KB Output is correct
45 Correct 112 ms 47084 KB Output is correct
46 Correct 111 ms 46828 KB Output is correct
47 Correct 109 ms 46700 KB Output is correct
48 Correct 112 ms 46828 KB Output is correct
49 Correct 113 ms 47084 KB Output is correct
50 Correct 116 ms 47100 KB Output is correct
51 Correct 115 ms 46956 KB Output is correct
52 Correct 115 ms 46880 KB Output is correct
53 Correct 111 ms 46956 KB Output is correct
54 Correct 114 ms 46828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 46444 KB Output is correct
2 Correct 112 ms 46572 KB Output is correct
3 Correct 108 ms 46572 KB Output is correct
4 Correct 109 ms 46572 KB Output is correct
5 Correct 111 ms 46572 KB Output is correct
6 Correct 108 ms 46700 KB Output is correct
7 Correct 113 ms 46572 KB Output is correct
8 Correct 110 ms 46572 KB Output is correct
9 Correct 107 ms 46572 KB Output is correct
10 Correct 110 ms 46572 KB Output is correct
11 Correct 109 ms 46572 KB Output is correct
12 Correct 110 ms 46572 KB Output is correct
13 Correct 111 ms 46592 KB Output is correct
14 Correct 110 ms 46572 KB Output is correct
15 Correct 109 ms 46572 KB Output is correct
16 Correct 111 ms 46572 KB Output is correct
17 Correct 108 ms 46536 KB Output is correct
18 Correct 109 ms 46572 KB Output is correct
19 Correct 109 ms 46572 KB Output is correct
20 Correct 108 ms 46584 KB Output is correct
21 Correct 110 ms 46572 KB Output is correct
22 Correct 111 ms 46700 KB Output is correct
23 Correct 112 ms 46572 KB Output is correct
24 Correct 111 ms 46700 KB Output is correct
25 Correct 108 ms 46572 KB Output is correct
26 Correct 238 ms 56668 KB Output is correct
27 Correct 284 ms 55904 KB Output is correct
28 Correct 110 ms 46828 KB Output is correct
29 Correct 108 ms 46572 KB Output is correct
30 Correct 107 ms 46572 KB Output is correct
31 Correct 286 ms 56000 KB Output is correct
32 Correct 113 ms 46828 KB Output is correct
33 Correct 335 ms 62944 KB Output is correct
34 Correct 290 ms 55988 KB Output is correct
35 Correct 115 ms 46828 KB Output is correct
36 Correct 312 ms 56672 KB Output is correct
37 Correct 110 ms 46828 KB Output is correct
38 Correct 113 ms 46828 KB Output is correct
39 Correct 235 ms 56540 KB Output is correct
40 Correct 115 ms 47212 KB Output is correct
41 Correct 267 ms 56288 KB Output is correct
42 Correct 337 ms 58208 KB Output is correct
43 Correct 112 ms 46572 KB Output is correct
44 Correct 314 ms 63756 KB Output is correct
45 Correct 321 ms 59616 KB Output is correct
46 Correct 111 ms 46932 KB Output is correct
47 Correct 112 ms 46808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 230 ms 56528 KB Output is correct
2 Correct 245 ms 63952 KB Output is correct
3 Correct 114 ms 46956 KB Output is correct
4 Correct 115 ms 46828 KB Output is correct
5 Correct 111 ms 46572 KB Output is correct
6 Correct 111 ms 46572 KB Output is correct
7 Correct 114 ms 46828 KB Output is correct
8 Correct 296 ms 57824 KB Output is correct
9 Correct 114 ms 46828 KB Output is correct
10 Correct 290 ms 56416 KB Output is correct
11 Correct 111 ms 46572 KB Output is correct
12 Correct 317 ms 56308 KB Output is correct
13 Correct 293 ms 57700 KB Output is correct
14 Correct 271 ms 61280 KB Output is correct
15 Correct 236 ms 56540 KB Output is correct
16 Correct 111 ms 47084 KB Output is correct
17 Correct 113 ms 46572 KB Output is correct
18 Correct 244 ms 63068 KB Output is correct
19 Correct 281 ms 62816 KB Output is correct
20 Correct 112 ms 46828 KB Output is correct
21 Correct 111 ms 46572 KB Output is correct
22 Correct 232 ms 58960 KB Output is correct
23 Correct 114 ms 46828 KB Output is correct
24 Correct 303 ms 56672 KB Output is correct
25 Correct 270 ms 63716 KB Output is correct
26 Correct 111 ms 47084 KB Output is correct
27 Correct 113 ms 47084 KB Output is correct
28 Correct 112 ms 46828 KB Output is correct
29 Correct 114 ms 46828 KB Output is correct
30 Correct 117 ms 46956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 46444 KB Output is correct
2 Correct 112 ms 46572 KB Output is correct
3 Correct 108 ms 46572 KB Output is correct
4 Correct 109 ms 46572 KB Output is correct
5 Correct 111 ms 46572 KB Output is correct
6 Correct 108 ms 46700 KB Output is correct
7 Correct 113 ms 46572 KB Output is correct
8 Correct 110 ms 46572 KB Output is correct
9 Correct 107 ms 46572 KB Output is correct
10 Correct 110 ms 46572 KB Output is correct
11 Correct 109 ms 46572 KB Output is correct
12 Correct 110 ms 46572 KB Output is correct
13 Correct 111 ms 46592 KB Output is correct
14 Correct 110 ms 46572 KB Output is correct
15 Correct 109 ms 46572 KB Output is correct
16 Correct 111 ms 46572 KB Output is correct
17 Correct 108 ms 46536 KB Output is correct
18 Correct 109 ms 46572 KB Output is correct
19 Correct 109 ms 46572 KB Output is correct
20 Correct 108 ms 46584 KB Output is correct
21 Correct 110 ms 46572 KB Output is correct
22 Correct 111 ms 46700 KB Output is correct
23 Correct 112 ms 46572 KB Output is correct
24 Correct 111 ms 46700 KB Output is correct
25 Correct 111 ms 46572 KB Output is correct
26 Correct 110 ms 46956 KB Output is correct
27 Correct 115 ms 46956 KB Output is correct
28 Correct 120 ms 47340 KB Output is correct
29 Correct 112 ms 47084 KB Output is correct
30 Correct 113 ms 47084 KB Output is correct
31 Correct 110 ms 46700 KB Output is correct
32 Correct 118 ms 47084 KB Output is correct
33 Correct 109 ms 46700 KB Output is correct
34 Correct 113 ms 46956 KB Output is correct
35 Correct 115 ms 47084 KB Output is correct
36 Correct 112 ms 46956 KB Output is correct
37 Correct 111 ms 46956 KB Output is correct
38 Correct 108 ms 46572 KB Output is correct
39 Correct 111 ms 46956 KB Output is correct
40 Correct 113 ms 46828 KB Output is correct
41 Correct 122 ms 46956 KB Output is correct
42 Correct 111 ms 47104 KB Output is correct
43 Correct 116 ms 47084 KB Output is correct
44 Correct 118 ms 46572 KB Output is correct
45 Correct 112 ms 47084 KB Output is correct
46 Correct 111 ms 46828 KB Output is correct
47 Correct 109 ms 46700 KB Output is correct
48 Correct 112 ms 46828 KB Output is correct
49 Correct 113 ms 47084 KB Output is correct
50 Correct 116 ms 47100 KB Output is correct
51 Correct 115 ms 46956 KB Output is correct
52 Correct 115 ms 46880 KB Output is correct
53 Correct 111 ms 46956 KB Output is correct
54 Correct 114 ms 46828 KB Output is correct
55 Correct 108 ms 46572 KB Output is correct
56 Correct 238 ms 56668 KB Output is correct
57 Correct 284 ms 55904 KB Output is correct
58 Correct 110 ms 46828 KB Output is correct
59 Correct 108 ms 46572 KB Output is correct
60 Correct 107 ms 46572 KB Output is correct
61 Correct 286 ms 56000 KB Output is correct
62 Correct 113 ms 46828 KB Output is correct
63 Correct 335 ms 62944 KB Output is correct
64 Correct 290 ms 55988 KB Output is correct
65 Correct 115 ms 46828 KB Output is correct
66 Correct 312 ms 56672 KB Output is correct
67 Correct 110 ms 46828 KB Output is correct
68 Correct 113 ms 46828 KB Output is correct
69 Correct 235 ms 56540 KB Output is correct
70 Correct 115 ms 47212 KB Output is correct
71 Correct 267 ms 56288 KB Output is correct
72 Correct 337 ms 58208 KB Output is correct
73 Correct 112 ms 46572 KB Output is correct
74 Correct 314 ms 63756 KB Output is correct
75 Correct 321 ms 59616 KB Output is correct
76 Correct 111 ms 46932 KB Output is correct
77 Correct 112 ms 46808 KB Output is correct
78 Correct 230 ms 56528 KB Output is correct
79 Correct 245 ms 63952 KB Output is correct
80 Correct 114 ms 46956 KB Output is correct
81 Correct 115 ms 46828 KB Output is correct
82 Correct 111 ms 46572 KB Output is correct
83 Correct 111 ms 46572 KB Output is correct
84 Correct 114 ms 46828 KB Output is correct
85 Correct 296 ms 57824 KB Output is correct
86 Correct 114 ms 46828 KB Output is correct
87 Correct 290 ms 56416 KB Output is correct
88 Correct 111 ms 46572 KB Output is correct
89 Correct 317 ms 56308 KB Output is correct
90 Correct 293 ms 57700 KB Output is correct
91 Correct 271 ms 61280 KB Output is correct
92 Correct 236 ms 56540 KB Output is correct
93 Correct 111 ms 47084 KB Output is correct
94 Correct 113 ms 46572 KB Output is correct
95 Correct 244 ms 63068 KB Output is correct
96 Correct 281 ms 62816 KB Output is correct
97 Correct 112 ms 46828 KB Output is correct
98 Correct 111 ms 46572 KB Output is correct
99 Correct 232 ms 58960 KB Output is correct
100 Correct 114 ms 46828 KB Output is correct
101 Correct 303 ms 56672 KB Output is correct
102 Correct 270 ms 63716 KB Output is correct
103 Correct 111 ms 47084 KB Output is correct
104 Correct 113 ms 47084 KB Output is correct
105 Correct 112 ms 46828 KB Output is correct
106 Correct 114 ms 46828 KB Output is correct
107 Correct 117 ms 46956 KB Output is correct
108 Runtime error 38 ms 38508 KB Execution killed with signal 11 (could be triggered by violating memory limits)
109 Halted 0 ms 0 KB -