Submission #306956

# Submission time Handle Problem Language Result Execution time Memory
306956 2020-09-26T15:11:00 Z HynDuf Mergers (JOI19_mergers) C++11
100 / 100
840 ms 87724 KB
#include <bits/stdc++.h>

#define task "M"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 5e5 + 2;
int n, numCol, deg[N], rt[N], dep[N], par[N];
vi col[N], g[N];
void dfs(int u)
{
    dep[u] = dep[par[u]] + 1;
    for (int v : g[u]) if (v != par[u])
    {
        par[v] = u;
        dfs(v);
    }
}
int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);}
bool in[N];
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    cin >> n >> numCol;
    rep(I, 2, n)
    {
        int u, v;
        cin >> u >> v;
        g[u].eb(v);
        g[v].eb(u);
    }
    dfs(1);
    rep(i, 1, n)
    {
        int c;
        cin >> c;
        col[c].eb(i);
        rt[i] = i;
    }
    rep(c, 1, numCol)
    {
        rep(i, 1, SZ(col[c]) - 1)
        {
            int u = frt(col[c][i - 1]), v = frt(col[c][i]);
            while (u != v)
            {
                if (dep[u] < dep[v]) swap(u, v);
                rt[u] = frt(par[u]);
                u = rt[u];
            }
        }
    }
    vi change;
    rep(i, 1, n)
    {
        for (int v : g[i]) if (frt(v) != frt(i) && !in[frt(v)]) change.eb(frt(v)), in[frt(v)] = 1, deg[frt(v)]++;
        for (int v : change) in[v] = 0;
        change.clear();
    }
    int cnt = 0;
    rep(i, 1, n) cnt += (deg[i] == 1);
    cout << (cnt + 1) / 2;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 19 ms 24064 KB Output is correct
27 Correct 22 ms 24064 KB Output is correct
28 Correct 20 ms 24192 KB Output is correct
29 Correct 21 ms 24192 KB Output is correct
30 Correct 18 ms 24064 KB Output is correct
31 Correct 17 ms 23808 KB Output is correct
32 Correct 19 ms 24192 KB Output is correct
33 Correct 18 ms 23808 KB Output is correct
34 Correct 19 ms 24064 KB Output is correct
35 Correct 19 ms 24184 KB Output is correct
36 Correct 19 ms 24192 KB Output is correct
37 Correct 19 ms 24064 KB Output is correct
38 Correct 18 ms 23936 KB Output is correct
39 Correct 20 ms 24064 KB Output is correct
40 Correct 19 ms 24064 KB Output is correct
41 Correct 19 ms 24056 KB Output is correct
42 Correct 20 ms 24056 KB Output is correct
43 Correct 20 ms 24192 KB Output is correct
44 Correct 17 ms 23808 KB Output is correct
45 Correct 19 ms 24184 KB Output is correct
46 Correct 19 ms 24064 KB Output is correct
47 Correct 17 ms 23808 KB Output is correct
48 Correct 19 ms 24064 KB Output is correct
49 Correct 18 ms 24192 KB Output is correct
50 Correct 19 ms 24192 KB Output is correct
51 Correct 20 ms 24060 KB Output is correct
52 Correct 18 ms 24064 KB Output is correct
53 Correct 18 ms 24064 KB Output is correct
54 Correct 19 ms 24064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 83 ms 30576 KB Output is correct
27 Correct 97 ms 30200 KB Output is correct
28 Correct 19 ms 24064 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 101 ms 30456 KB Output is correct
32 Correct 20 ms 24056 KB Output is correct
33 Correct 110 ms 34424 KB Output is correct
34 Correct 98 ms 30200 KB Output is correct
35 Correct 20 ms 24060 KB Output is correct
36 Correct 111 ms 30712 KB Output is correct
37 Correct 19 ms 24064 KB Output is correct
38 Correct 19 ms 24064 KB Output is correct
39 Correct 79 ms 30448 KB Output is correct
40 Correct 20 ms 24192 KB Output is correct
41 Correct 93 ms 30196 KB Output is correct
42 Correct 111 ms 31608 KB Output is correct
43 Correct 17 ms 23808 KB Output is correct
44 Correct 107 ms 34548 KB Output is correct
45 Correct 107 ms 32376 KB Output is correct
46 Correct 19 ms 24056 KB Output is correct
47 Correct 19 ms 24064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 30576 KB Output is correct
2 Correct 94 ms 34836 KB Output is correct
3 Correct 19 ms 24064 KB Output is correct
4 Correct 18 ms 24064 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 19 ms 24064 KB Output is correct
8 Correct 133 ms 32120 KB Output is correct
9 Correct 19 ms 24064 KB Output is correct
10 Correct 108 ms 30796 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 113 ms 30780 KB Output is correct
13 Correct 113 ms 31984 KB Output is correct
14 Correct 110 ms 33656 KB Output is correct
15 Correct 85 ms 30576 KB Output is correct
16 Correct 19 ms 24064 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 91 ms 34032 KB Output is correct
19 Correct 122 ms 35960 KB Output is correct
20 Correct 18 ms 24064 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 89 ms 32492 KB Output is correct
23 Correct 18 ms 24064 KB Output is correct
24 Correct 108 ms 31224 KB Output is correct
25 Correct 113 ms 35064 KB Output is correct
26 Correct 19 ms 24192 KB Output is correct
27 Correct 19 ms 24192 KB Output is correct
28 Correct 18 ms 24064 KB Output is correct
29 Correct 19 ms 24064 KB Output is correct
30 Correct 19 ms 24064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 19 ms 24064 KB Output is correct
27 Correct 22 ms 24064 KB Output is correct
28 Correct 20 ms 24192 KB Output is correct
29 Correct 21 ms 24192 KB Output is correct
30 Correct 18 ms 24064 KB Output is correct
31 Correct 17 ms 23808 KB Output is correct
32 Correct 19 ms 24192 KB Output is correct
33 Correct 18 ms 23808 KB Output is correct
34 Correct 19 ms 24064 KB Output is correct
35 Correct 19 ms 24184 KB Output is correct
36 Correct 19 ms 24192 KB Output is correct
37 Correct 19 ms 24064 KB Output is correct
38 Correct 18 ms 23936 KB Output is correct
39 Correct 20 ms 24064 KB Output is correct
40 Correct 19 ms 24064 KB Output is correct
41 Correct 19 ms 24056 KB Output is correct
42 Correct 20 ms 24056 KB Output is correct
43 Correct 20 ms 24192 KB Output is correct
44 Correct 17 ms 23808 KB Output is correct
45 Correct 19 ms 24184 KB Output is correct
46 Correct 19 ms 24064 KB Output is correct
47 Correct 17 ms 23808 KB Output is correct
48 Correct 19 ms 24064 KB Output is correct
49 Correct 18 ms 24192 KB Output is correct
50 Correct 19 ms 24192 KB Output is correct
51 Correct 20 ms 24060 KB Output is correct
52 Correct 18 ms 24064 KB Output is correct
53 Correct 18 ms 24064 KB Output is correct
54 Correct 19 ms 24064 KB Output is correct
55 Correct 17 ms 23808 KB Output is correct
56 Correct 83 ms 30576 KB Output is correct
57 Correct 97 ms 30200 KB Output is correct
58 Correct 19 ms 24064 KB Output is correct
59 Correct 17 ms 23808 KB Output is correct
60 Correct 17 ms 23808 KB Output is correct
61 Correct 101 ms 30456 KB Output is correct
62 Correct 20 ms 24056 KB Output is correct
63 Correct 110 ms 34424 KB Output is correct
64 Correct 98 ms 30200 KB Output is correct
65 Correct 20 ms 24060 KB Output is correct
66 Correct 111 ms 30712 KB Output is correct
67 Correct 19 ms 24064 KB Output is correct
68 Correct 19 ms 24064 KB Output is correct
69 Correct 79 ms 30448 KB Output is correct
70 Correct 20 ms 24192 KB Output is correct
71 Correct 93 ms 30196 KB Output is correct
72 Correct 111 ms 31608 KB Output is correct
73 Correct 17 ms 23808 KB Output is correct
74 Correct 107 ms 34548 KB Output is correct
75 Correct 107 ms 32376 KB Output is correct
76 Correct 19 ms 24056 KB Output is correct
77 Correct 19 ms 24064 KB Output is correct
78 Correct 76 ms 30576 KB Output is correct
79 Correct 94 ms 34836 KB Output is correct
80 Correct 19 ms 24064 KB Output is correct
81 Correct 18 ms 24064 KB Output is correct
82 Correct 17 ms 23808 KB Output is correct
83 Correct 17 ms 23808 KB Output is correct
84 Correct 19 ms 24064 KB Output is correct
85 Correct 133 ms 32120 KB Output is correct
86 Correct 19 ms 24064 KB Output is correct
87 Correct 108 ms 30796 KB Output is correct
88 Correct 18 ms 23808 KB Output is correct
89 Correct 113 ms 30780 KB Output is correct
90 Correct 113 ms 31984 KB Output is correct
91 Correct 110 ms 33656 KB Output is correct
92 Correct 85 ms 30576 KB Output is correct
93 Correct 19 ms 24064 KB Output is correct
94 Correct 17 ms 23808 KB Output is correct
95 Correct 91 ms 34032 KB Output is correct
96 Correct 122 ms 35960 KB Output is correct
97 Correct 18 ms 24064 KB Output is correct
98 Correct 17 ms 23808 KB Output is correct
99 Correct 89 ms 32492 KB Output is correct
100 Correct 18 ms 24064 KB Output is correct
101 Correct 108 ms 31224 KB Output is correct
102 Correct 113 ms 35064 KB Output is correct
103 Correct 19 ms 24192 KB Output is correct
104 Correct 19 ms 24192 KB Output is correct
105 Correct 18 ms 24064 KB Output is correct
106 Correct 19 ms 24064 KB Output is correct
107 Correct 19 ms 24064 KB Output is correct
108 Correct 579 ms 66632 KB Output is correct
109 Correct 692 ms 66352 KB Output is correct
110 Correct 721 ms 72960 KB Output is correct
111 Correct 839 ms 87724 KB Output is correct
112 Correct 840 ms 81884 KB Output is correct
113 Correct 564 ms 75484 KB Output is correct
114 Correct 623 ms 55756 KB Output is correct
115 Correct 635 ms 55784 KB Output is correct
116 Correct 756 ms 62632 KB Output is correct
117 Correct 762 ms 73976 KB Output is correct
118 Correct 690 ms 58872 KB Output is correct
119 Correct 764 ms 73976 KB Output is correct
120 Correct 826 ms 82424 KB Output is correct
121 Correct 761 ms 74128 KB Output is correct
122 Correct 810 ms 67832 KB Output is correct
123 Correct 550 ms 78648 KB Output is correct
124 Correct 829 ms 72696 KB Output is correct