Submission #210645

# Submission time Handle Problem Language Result Execution time Memory
210645 2020-03-18T00:54:04 Z tri Mergers (JOI19_mergers) C++14
100 / 100
1871 ms 159348 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = true;

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

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

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

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


int N, K;

const int MAXN = 5e5 + 100;
const int LOGN = 20;

vi aL[MAXN];

int anc[MAXN][LOGN];
int depth[MAXN];

void dfs(int cV, int pV) {
    for (int aV : aL[cV]) {
        if (aV != pV) {
            depth[aV] = depth[cV] + 1;
            dfs(aV, cV);
            anc[aV][0] = cV;
        }
    }
}

void prep() {
    depth[0] = 0;
    dfs(0, -1);
    anc[0][0] = 0;
    for (int k = 1; k < LOGN; k++) {
        for (int i = 0; i < N; i++) {
            anc[i][k] = anc[anc[i][k - 1]][k - 1];
        }
    }
}

int j(int a, int d) {
    for (int i = 0; i < LOGN; i++) {
        if (d & (1 << i)) {
            a = anc[a][i];
        }
    }
    return a;
}

int fLCA(int a, int b) {
    if (depth[a] > depth[b]) swap(a, b);

    b = j(b, depth[b] - depth[a]);

    if (a == b) return a;

    for (int i = LOGN - 1; i >= 0; i--) {
        if (anc[a][i] != anc[b][i]) {
            a = anc[a][i];
            b = anc[b][i];
        }
    }
    return anc[a][0];
}

int st[MAXN];

vi bySt[MAXN];

int mark[MAXN];

vector<pi> mrg;

int dfs2(int cV, int pV) {
    int sum = mark[cV];
    for (int aV : aL[cV]) {
        if (aV != pV) {
            sum += dfs2(aV, cV);
        }
    }
//    ps(cV, sum);
    if (sum) {
        mrg.pb({cV, pV});
    }
    return sum;
}

struct DSU {
    int anc[MAXN];

    void init() {
        for (int i = 0; i < MAXN; i++) {
            anc[i] = i;
        }
    }

    int fRt(int i) {
        return anc[i] == i ? i : anc[i] = fRt(anc[i]);
    }

    bool merge(int a, int b) {
        a = fRt(a), b = fRt(b);
        if (a == b) {
            return false;
        }
        anc[a] = b;
        return true;
    }
} dsu;

int id[MAXN];

vi cAL[MAXN];

int main() {
    cin >> N >> K;
    for (int i = 0; i < N - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        aL[x].pb(y);
        aL[y].pb(x);
    }

    for (int i = 0; i < N; i++) {
        cin >> st[i];
        bySt[st[i]].pb(i);
    }


    prep();
//    ps(fLCA(5, 7));

    memset(mark, 0, sizeof(mark));

    for (vi &list : bySt) {
        for (int i = 0; i + 1 < list.size(); i++) {
            int x = list[i];
            int y = list[i + 1];
            int lca = fLCA(x, y);
//            ps(x, y, lca);
            mark[x]++;
            mark[y]++;
            mark[lca] -= 2;
        }
    }

//    for (int i = 0; i < N; i++) {
//        ps(mark[i]);
//    }

    dfs2(0, -1);

//    ps("mrg:", mrg);

    dsu.init();
    for (pi x : mrg) {
        dsu.merge(x.f, x.s);
    }

    for (int i = 0; i < N; i++) {
        id[i] = dsu.fRt(i);
    }

    for (int cV = 0; cV < N; cV++) {
        for (int aV :aL[cV]) {
            if (id[cV] != id[aV]) {
                cAL[id[cV]].pb(id[aV]);
            }
        }
    }

    for (int i = 0; i < N; i++) {
        sort(cAL[i].begin(), cAL[i].end());
        cAL[i].erase(unique(cAL[i].begin(), cAL[i].end()), cAL[i].end());
//        ps(cAL[i]);
    }

    int leaves = 0;
    for (int i = 0; i < N; i++) {
        if (cAL[i].size() == 1) {
            leaves++;
        }
    }
    cout << (leaves + 1) / 2 << endl;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:205:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i + 1 < list.size(); i++) {
                         ~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 29 ms 39544 KB Output is correct
2 Correct 30 ms 39544 KB Output is correct
3 Correct 29 ms 39544 KB Output is correct
4 Correct 29 ms 39420 KB Output is correct
5 Correct 29 ms 39544 KB Output is correct
6 Correct 30 ms 39544 KB Output is correct
7 Correct 30 ms 39544 KB Output is correct
8 Correct 30 ms 39544 KB Output is correct
9 Correct 30 ms 39544 KB Output is correct
10 Correct 30 ms 39544 KB Output is correct
11 Correct 29 ms 39564 KB Output is correct
12 Correct 30 ms 39544 KB Output is correct
13 Correct 30 ms 39544 KB Output is correct
14 Correct 31 ms 39544 KB Output is correct
15 Correct 30 ms 39544 KB Output is correct
16 Correct 30 ms 39544 KB Output is correct
17 Correct 29 ms 39544 KB Output is correct
18 Correct 29 ms 39548 KB Output is correct
19 Correct 30 ms 39544 KB Output is correct
20 Correct 29 ms 39544 KB Output is correct
21 Correct 29 ms 39544 KB Output is correct
22 Correct 29 ms 39544 KB Output is correct
23 Correct 30 ms 39544 KB Output is correct
24 Correct 30 ms 39544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 39544 KB Output is correct
2 Correct 30 ms 39544 KB Output is correct
3 Correct 29 ms 39544 KB Output is correct
4 Correct 29 ms 39420 KB Output is correct
5 Correct 29 ms 39544 KB Output is correct
6 Correct 30 ms 39544 KB Output is correct
7 Correct 30 ms 39544 KB Output is correct
8 Correct 30 ms 39544 KB Output is correct
9 Correct 30 ms 39544 KB Output is correct
10 Correct 30 ms 39544 KB Output is correct
11 Correct 29 ms 39564 KB Output is correct
12 Correct 30 ms 39544 KB Output is correct
13 Correct 30 ms 39544 KB Output is correct
14 Correct 31 ms 39544 KB Output is correct
15 Correct 30 ms 39544 KB Output is correct
16 Correct 30 ms 39544 KB Output is correct
17 Correct 29 ms 39544 KB Output is correct
18 Correct 29 ms 39548 KB Output is correct
19 Correct 30 ms 39544 KB Output is correct
20 Correct 29 ms 39544 KB Output is correct
21 Correct 29 ms 39544 KB Output is correct
22 Correct 29 ms 39544 KB Output is correct
23 Correct 30 ms 39544 KB Output is correct
24 Correct 30 ms 39544 KB Output is correct
25 Correct 30 ms 39544 KB Output is correct
26 Correct 34 ms 40056 KB Output is correct
27 Correct 34 ms 39928 KB Output is correct
28 Correct 35 ms 40184 KB Output is correct
29 Correct 35 ms 40056 KB Output is correct
30 Correct 35 ms 39928 KB Output is correct
31 Correct 29 ms 39544 KB Output is correct
32 Correct 35 ms 40184 KB Output is correct
33 Correct 29 ms 39544 KB Output is correct
34 Correct 34 ms 39928 KB Output is correct
35 Correct 34 ms 40060 KB Output is correct
36 Correct 34 ms 39928 KB Output is correct
37 Correct 35 ms 39928 KB Output is correct
38 Correct 29 ms 39544 KB Output is correct
39 Correct 34 ms 39928 KB Output is correct
40 Correct 33 ms 39928 KB Output is correct
41 Correct 33 ms 39928 KB Output is correct
42 Correct 34 ms 40056 KB Output is correct
43 Correct 34 ms 40184 KB Output is correct
44 Correct 29 ms 39544 KB Output is correct
45 Correct 34 ms 40056 KB Output is correct
46 Correct 34 ms 39928 KB Output is correct
47 Correct 29 ms 39544 KB Output is correct
48 Correct 33 ms 39928 KB Output is correct
49 Correct 33 ms 40184 KB Output is correct
50 Correct 34 ms 40056 KB Output is correct
51 Correct 34 ms 39928 KB Output is correct
52 Correct 34 ms 39928 KB Output is correct
53 Correct 34 ms 40056 KB Output is correct
54 Correct 34 ms 39928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 39544 KB Output is correct
2 Correct 30 ms 39544 KB Output is correct
3 Correct 29 ms 39544 KB Output is correct
4 Correct 29 ms 39420 KB Output is correct
5 Correct 29 ms 39544 KB Output is correct
6 Correct 30 ms 39544 KB Output is correct
7 Correct 30 ms 39544 KB Output is correct
8 Correct 30 ms 39544 KB Output is correct
9 Correct 30 ms 39544 KB Output is correct
10 Correct 30 ms 39544 KB Output is correct
11 Correct 29 ms 39564 KB Output is correct
12 Correct 30 ms 39544 KB Output is correct
13 Correct 30 ms 39544 KB Output is correct
14 Correct 31 ms 39544 KB Output is correct
15 Correct 30 ms 39544 KB Output is correct
16 Correct 30 ms 39544 KB Output is correct
17 Correct 29 ms 39544 KB Output is correct
18 Correct 29 ms 39548 KB Output is correct
19 Correct 30 ms 39544 KB Output is correct
20 Correct 29 ms 39544 KB Output is correct
21 Correct 29 ms 39544 KB Output is correct
22 Correct 29 ms 39544 KB Output is correct
23 Correct 30 ms 39544 KB Output is correct
24 Correct 30 ms 39544 KB Output is correct
25 Correct 30 ms 39544 KB Output is correct
26 Correct 190 ms 53676 KB Output is correct
27 Correct 230 ms 53296 KB Output is correct
28 Correct 34 ms 39928 KB Output is correct
29 Correct 30 ms 39544 KB Output is correct
30 Correct 30 ms 39544 KB Output is correct
31 Correct 223 ms 53240 KB Output is correct
32 Correct 35 ms 39928 KB Output is correct
33 Correct 280 ms 58952 KB Output is correct
34 Correct 225 ms 53264 KB Output is correct
35 Correct 35 ms 39928 KB Output is correct
36 Correct 246 ms 53820 KB Output is correct
37 Correct 34 ms 39928 KB Output is correct
38 Correct 34 ms 39928 KB Output is correct
39 Correct 190 ms 53496 KB Output is correct
40 Correct 34 ms 40184 KB Output is correct
41 Correct 204 ms 53108 KB Output is correct
42 Correct 277 ms 55124 KB Output is correct
43 Correct 30 ms 39544 KB Output is correct
44 Correct 256 ms 59120 KB Output is correct
45 Correct 275 ms 56196 KB Output is correct
46 Correct 33 ms 40056 KB Output is correct
47 Correct 33 ms 39932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 53496 KB Output is correct
2 Correct 222 ms 58732 KB Output is correct
3 Correct 34 ms 39928 KB Output is correct
4 Correct 34 ms 39928 KB Output is correct
5 Correct 29 ms 39548 KB Output is correct
6 Correct 29 ms 39544 KB Output is correct
7 Correct 33 ms 39928 KB Output is correct
8 Correct 254 ms 54508 KB Output is correct
9 Correct 33 ms 39928 KB Output is correct
10 Correct 222 ms 53364 KB Output is correct
11 Correct 29 ms 39544 KB Output is correct
12 Correct 240 ms 53224 KB Output is correct
13 Correct 259 ms 54640 KB Output is correct
14 Correct 236 ms 58232 KB Output is correct
15 Correct 187 ms 53496 KB Output is correct
16 Correct 34 ms 40056 KB Output is correct
17 Correct 29 ms 39544 KB Output is correct
18 Correct 216 ms 57964 KB Output is correct
19 Correct 260 ms 61180 KB Output is correct
20 Correct 34 ms 39928 KB Output is correct
21 Correct 29 ms 39544 KB Output is correct
22 Correct 216 ms 55020 KB Output is correct
23 Correct 33 ms 39928 KB Output is correct
24 Correct 258 ms 53616 KB Output is correct
25 Correct 259 ms 60152 KB Output is correct
26 Correct 35 ms 40056 KB Output is correct
27 Correct 34 ms 40056 KB Output is correct
28 Correct 34 ms 39928 KB Output is correct
29 Correct 33 ms 39928 KB Output is correct
30 Correct 34 ms 40056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 39544 KB Output is correct
2 Correct 30 ms 39544 KB Output is correct
3 Correct 29 ms 39544 KB Output is correct
4 Correct 29 ms 39420 KB Output is correct
5 Correct 29 ms 39544 KB Output is correct
6 Correct 30 ms 39544 KB Output is correct
7 Correct 30 ms 39544 KB Output is correct
8 Correct 30 ms 39544 KB Output is correct
9 Correct 30 ms 39544 KB Output is correct
10 Correct 30 ms 39544 KB Output is correct
11 Correct 29 ms 39564 KB Output is correct
12 Correct 30 ms 39544 KB Output is correct
13 Correct 30 ms 39544 KB Output is correct
14 Correct 31 ms 39544 KB Output is correct
15 Correct 30 ms 39544 KB Output is correct
16 Correct 30 ms 39544 KB Output is correct
17 Correct 29 ms 39544 KB Output is correct
18 Correct 29 ms 39548 KB Output is correct
19 Correct 30 ms 39544 KB Output is correct
20 Correct 29 ms 39544 KB Output is correct
21 Correct 29 ms 39544 KB Output is correct
22 Correct 29 ms 39544 KB Output is correct
23 Correct 30 ms 39544 KB Output is correct
24 Correct 30 ms 39544 KB Output is correct
25 Correct 30 ms 39544 KB Output is correct
26 Correct 34 ms 40056 KB Output is correct
27 Correct 34 ms 39928 KB Output is correct
28 Correct 35 ms 40184 KB Output is correct
29 Correct 35 ms 40056 KB Output is correct
30 Correct 35 ms 39928 KB Output is correct
31 Correct 29 ms 39544 KB Output is correct
32 Correct 35 ms 40184 KB Output is correct
33 Correct 29 ms 39544 KB Output is correct
34 Correct 34 ms 39928 KB Output is correct
35 Correct 34 ms 40060 KB Output is correct
36 Correct 34 ms 39928 KB Output is correct
37 Correct 35 ms 39928 KB Output is correct
38 Correct 29 ms 39544 KB Output is correct
39 Correct 34 ms 39928 KB Output is correct
40 Correct 33 ms 39928 KB Output is correct
41 Correct 33 ms 39928 KB Output is correct
42 Correct 34 ms 40056 KB Output is correct
43 Correct 34 ms 40184 KB Output is correct
44 Correct 29 ms 39544 KB Output is correct
45 Correct 34 ms 40056 KB Output is correct
46 Correct 34 ms 39928 KB Output is correct
47 Correct 29 ms 39544 KB Output is correct
48 Correct 33 ms 39928 KB Output is correct
49 Correct 33 ms 40184 KB Output is correct
50 Correct 34 ms 40056 KB Output is correct
51 Correct 34 ms 39928 KB Output is correct
52 Correct 34 ms 39928 KB Output is correct
53 Correct 34 ms 40056 KB Output is correct
54 Correct 34 ms 39928 KB Output is correct
55 Correct 30 ms 39544 KB Output is correct
56 Correct 190 ms 53676 KB Output is correct
57 Correct 230 ms 53296 KB Output is correct
58 Correct 34 ms 39928 KB Output is correct
59 Correct 30 ms 39544 KB Output is correct
60 Correct 30 ms 39544 KB Output is correct
61 Correct 223 ms 53240 KB Output is correct
62 Correct 35 ms 39928 KB Output is correct
63 Correct 280 ms 58952 KB Output is correct
64 Correct 225 ms 53264 KB Output is correct
65 Correct 35 ms 39928 KB Output is correct
66 Correct 246 ms 53820 KB Output is correct
67 Correct 34 ms 39928 KB Output is correct
68 Correct 34 ms 39928 KB Output is correct
69 Correct 190 ms 53496 KB Output is correct
70 Correct 34 ms 40184 KB Output is correct
71 Correct 204 ms 53108 KB Output is correct
72 Correct 277 ms 55124 KB Output is correct
73 Correct 30 ms 39544 KB Output is correct
74 Correct 256 ms 59120 KB Output is correct
75 Correct 275 ms 56196 KB Output is correct
76 Correct 33 ms 40056 KB Output is correct
77 Correct 33 ms 39932 KB Output is correct
78 Correct 190 ms 53496 KB Output is correct
79 Correct 222 ms 58732 KB Output is correct
80 Correct 34 ms 39928 KB Output is correct
81 Correct 34 ms 39928 KB Output is correct
82 Correct 29 ms 39548 KB Output is correct
83 Correct 29 ms 39544 KB Output is correct
84 Correct 33 ms 39928 KB Output is correct
85 Correct 254 ms 54508 KB Output is correct
86 Correct 33 ms 39928 KB Output is correct
87 Correct 222 ms 53364 KB Output is correct
88 Correct 29 ms 39544 KB Output is correct
89 Correct 240 ms 53224 KB Output is correct
90 Correct 259 ms 54640 KB Output is correct
91 Correct 236 ms 58232 KB Output is correct
92 Correct 187 ms 53496 KB Output is correct
93 Correct 34 ms 40056 KB Output is correct
94 Correct 29 ms 39544 KB Output is correct
95 Correct 216 ms 57964 KB Output is correct
96 Correct 260 ms 61180 KB Output is correct
97 Correct 34 ms 39928 KB Output is correct
98 Correct 29 ms 39544 KB Output is correct
99 Correct 216 ms 55020 KB Output is correct
100 Correct 33 ms 39928 KB Output is correct
101 Correct 258 ms 53616 KB Output is correct
102 Correct 259 ms 60152 KB Output is correct
103 Correct 35 ms 40056 KB Output is correct
104 Correct 34 ms 40056 KB Output is correct
105 Correct 34 ms 39928 KB Output is correct
106 Correct 33 ms 39928 KB Output is correct
107 Correct 34 ms 40056 KB Output is correct
108 Correct 1235 ms 123660 KB Output is correct
109 Correct 1795 ms 128684 KB Output is correct
110 Correct 1871 ms 137580 KB Output is correct
111 Correct 1663 ms 159348 KB Output is correct
112 Correct 1554 ms 147944 KB Output is correct
113 Correct 1280 ms 141140 KB Output is correct
114 Correct 1256 ms 114256 KB Output is correct
115 Correct 1245 ms 114392 KB Output is correct
116 Correct 1494 ms 119152 KB Output is correct
117 Correct 1500 ms 142496 KB Output is correct
118 Correct 1284 ms 114780 KB Output is correct
119 Correct 1450 ms 142456 KB Output is correct
120 Correct 1669 ms 153080 KB Output is correct
121 Correct 1480 ms 142456 KB Output is correct
122 Correct 1507 ms 126176 KB Output is correct
123 Correct 1222 ms 145376 KB Output is correct
124 Correct 1618 ms 135656 KB Output is correct