Submission #164137

# Submission time Handle Problem Language Result Execution time Memory
164137 2019-11-17T15:30:37 Z combi1k1 Mergers (JOI19_mergers) C++14
70 / 100
1760 ms 149012 KB
#include<bits/stdc++.h>

using namespace std;

const int   N   = 5e5 + 1;

vector<int> g[N];
vector<int> a[N];
vector<int> s[N];

int st[N], en[N];
int tot = 0;

int f[N];
int h[N];
int p[N][20];

int dfs(int u)  {
    st[u] = ++tot;
    for(int i = 0 ; i < 16 ; ++i)
        p[u][i + 1] = p[p[u][i]][i];
    for(int v : g[u])   if (v != p[u][0])   {
        h[v] = h[u] + 1;
        p[v][0] = u;
        dfs(v);
    }
    en[u] = tot;

    return  1;
}
int par(int u,int d)    {
    for(int i = 0 ; i < 17 ; ++i)   if (d >> i & 1)
        u = p[u][i];
    return  u;
}
int lca(int u,int v)    {
    if (h[u] < h[v])
        swap(u,v);
    u = par(u,h[u] - h[v]);

    if (u == v) return  u;

    for(int i = 16 ; i >= 0 ; --i)  if (p[u][i] != p[v][i]) {
        u = p[u][i];
        v = p[v][i];
    }

    return  p[u][0];
}
int upd(int u)  {
    for(int v : g[u])   if (v != p[u][0])
        upd(v), f[u] += f[v];
    return  1;
}
int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;  cin >> n;
    int k;  cin >> k;

    for(int i = 2 ; i <= n ; ++i)   {
        int x;  cin >> x;
        int y;  cin >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i = 1 ; i <= n ; ++i)   {
        int x;  cin >> x;
        s[x].push_back(i);
    }

    dfs(1);

    auto cmp = [&](int x,int y) {
        return  st[x] < st[y];
    };
    auto con = [&](vector<int> v,int x,int y)   {
        sort(v.begin(),v.end(),cmp);    v.erase(unique(v.begin(),v.end()),v.end());

        for(int j = 1, K = v.size() ; j < K ; ++j)
            v.push_back(lca(v[j - 1],v[j]));

        sort(v.begin(),v.end(),cmp);    v.erase(unique(v.begin(),v.end()),v.end());

        stack<int>  T;

        for(int a : v)  {
            for(; T.size() && en[T.top()] < en[a] ; T.pop());
            if (T.size())
                f[T.top()] += x,
                f[a] += y;
            T.push(a);
        }
    };

    for(int i = 1 ; i <= k ; ++i)   con(s[i],-1,1);

    upd(1);

    vector<int> vec;

    for(int i = 2 ; i <= n ; ++i)   if(!f[i])
        vec.push_back(i),
        vec.push_back(p[i][0]);

    memset(f,0,sizeof f);

    con(vec,1,1);

    int cnt = 0;

    for(int i = 1 ; i <= n ; ++i)   if (f[i] == 1)
        cnt++;

    cout << (cnt + 1) / 2 << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37632 KB Output is correct
3 Correct 37 ms 37624 KB Output is correct
4 Correct 38 ms 37596 KB Output is correct
5 Correct 38 ms 37496 KB Output is correct
6 Correct 38 ms 37628 KB Output is correct
7 Correct 39 ms 37496 KB Output is correct
8 Correct 38 ms 37496 KB Output is correct
9 Correct 37 ms 37596 KB Output is correct
10 Correct 37 ms 37700 KB Output is correct
11 Correct 38 ms 37624 KB Output is correct
12 Correct 38 ms 37596 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 37 ms 37624 KB Output is correct
16 Correct 38 ms 37496 KB Output is correct
17 Correct 37 ms 37496 KB Output is correct
18 Correct 36 ms 37624 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 36 ms 37624 KB Output is correct
21 Correct 38 ms 37496 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 41 ms 37624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37632 KB Output is correct
3 Correct 37 ms 37624 KB Output is correct
4 Correct 38 ms 37596 KB Output is correct
5 Correct 38 ms 37496 KB Output is correct
6 Correct 38 ms 37628 KB Output is correct
7 Correct 39 ms 37496 KB Output is correct
8 Correct 38 ms 37496 KB Output is correct
9 Correct 37 ms 37596 KB Output is correct
10 Correct 37 ms 37700 KB Output is correct
11 Correct 38 ms 37624 KB Output is correct
12 Correct 38 ms 37596 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 37 ms 37624 KB Output is correct
16 Correct 38 ms 37496 KB Output is correct
17 Correct 37 ms 37496 KB Output is correct
18 Correct 36 ms 37624 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 36 ms 37624 KB Output is correct
21 Correct 38 ms 37496 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 41 ms 37624 KB Output is correct
25 Correct 37 ms 37496 KB Output is correct
26 Correct 41 ms 38008 KB Output is correct
27 Correct 44 ms 38008 KB Output is correct
28 Correct 45 ms 38136 KB Output is correct
29 Correct 43 ms 38136 KB Output is correct
30 Correct 44 ms 38008 KB Output is correct
31 Correct 41 ms 37596 KB Output is correct
32 Correct 44 ms 38108 KB Output is correct
33 Correct 45 ms 37624 KB Output is correct
34 Correct 45 ms 38008 KB Output is correct
35 Correct 43 ms 38264 KB Output is correct
36 Correct 40 ms 38008 KB Output is correct
37 Correct 40 ms 38008 KB Output is correct
38 Correct 37 ms 37624 KB Output is correct
39 Correct 41 ms 38008 KB Output is correct
40 Correct 41 ms 38136 KB Output is correct
41 Correct 40 ms 38028 KB Output is correct
42 Correct 40 ms 38008 KB Output is correct
43 Correct 40 ms 38108 KB Output is correct
44 Correct 37 ms 37624 KB Output is correct
45 Correct 40 ms 38136 KB Output is correct
46 Correct 40 ms 38008 KB Output is correct
47 Correct 38 ms 37624 KB Output is correct
48 Correct 40 ms 38008 KB Output is correct
49 Correct 39 ms 38136 KB Output is correct
50 Correct 41 ms 38136 KB Output is correct
51 Correct 40 ms 38012 KB Output is correct
52 Correct 40 ms 38008 KB Output is correct
53 Correct 40 ms 38008 KB Output is correct
54 Correct 39 ms 37980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37632 KB Output is correct
3 Correct 37 ms 37624 KB Output is correct
4 Correct 38 ms 37596 KB Output is correct
5 Correct 38 ms 37496 KB Output is correct
6 Correct 38 ms 37628 KB Output is correct
7 Correct 39 ms 37496 KB Output is correct
8 Correct 38 ms 37496 KB Output is correct
9 Correct 37 ms 37596 KB Output is correct
10 Correct 37 ms 37700 KB Output is correct
11 Correct 38 ms 37624 KB Output is correct
12 Correct 38 ms 37596 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 37 ms 37624 KB Output is correct
16 Correct 38 ms 37496 KB Output is correct
17 Correct 37 ms 37496 KB Output is correct
18 Correct 36 ms 37624 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 36 ms 37624 KB Output is correct
21 Correct 38 ms 37496 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 41 ms 37624 KB Output is correct
25 Correct 37 ms 37496 KB Output is correct
26 Correct 139 ms 52008 KB Output is correct
27 Correct 185 ms 51740 KB Output is correct
28 Correct 39 ms 38008 KB Output is correct
29 Correct 37 ms 37496 KB Output is correct
30 Correct 37 ms 37596 KB Output is correct
31 Correct 264 ms 51696 KB Output is correct
32 Correct 39 ms 38008 KB Output is correct
33 Correct 220 ms 56056 KB Output is correct
34 Correct 266 ms 51848 KB Output is correct
35 Correct 39 ms 38008 KB Output is correct
36 Correct 192 ms 52060 KB Output is correct
37 Correct 39 ms 38008 KB Output is correct
38 Correct 40 ms 38012 KB Output is correct
39 Correct 145 ms 52024 KB Output is correct
40 Correct 46 ms 38136 KB Output is correct
41 Correct 194 ms 51820 KB Output is correct
42 Correct 197 ms 53240 KB Output is correct
43 Correct 42 ms 37496 KB Output is correct
44 Correct 228 ms 56500 KB Output is correct
45 Correct 197 ms 54032 KB Output is correct
46 Correct 39 ms 37952 KB Output is correct
47 Correct 40 ms 38012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 52080 KB Output is correct
2 Correct 200 ms 57576 KB Output is correct
3 Correct 41 ms 38056 KB Output is correct
4 Correct 39 ms 38008 KB Output is correct
5 Correct 37 ms 37508 KB Output is correct
6 Correct 37 ms 37496 KB Output is correct
7 Correct 39 ms 38008 KB Output is correct
8 Correct 210 ms 53460 KB Output is correct
9 Correct 43 ms 38008 KB Output is correct
10 Correct 172 ms 51960 KB Output is correct
11 Correct 37 ms 37624 KB Output is correct
12 Correct 183 ms 51960 KB Output is correct
13 Correct 220 ms 53624 KB Output is correct
14 Correct 264 ms 57196 KB Output is correct
15 Correct 138 ms 51948 KB Output is correct
16 Correct 40 ms 38084 KB Output is correct
17 Correct 38 ms 37624 KB Output is correct
18 Correct 212 ms 56804 KB Output is correct
19 Correct 277 ms 59412 KB Output is correct
20 Correct 40 ms 38008 KB Output is correct
21 Correct 41 ms 37528 KB Output is correct
22 Correct 179 ms 53872 KB Output is correct
23 Correct 44 ms 38008 KB Output is correct
24 Correct 207 ms 52600 KB Output is correct
25 Correct 277 ms 58700 KB Output is correct
26 Correct 45 ms 38136 KB Output is correct
27 Correct 46 ms 38136 KB Output is correct
28 Correct 44 ms 38008 KB Output is correct
29 Correct 44 ms 38008 KB Output is correct
30 Correct 40 ms 38008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 38 ms 37632 KB Output is correct
3 Correct 37 ms 37624 KB Output is correct
4 Correct 38 ms 37596 KB Output is correct
5 Correct 38 ms 37496 KB Output is correct
6 Correct 38 ms 37628 KB Output is correct
7 Correct 39 ms 37496 KB Output is correct
8 Correct 38 ms 37496 KB Output is correct
9 Correct 37 ms 37596 KB Output is correct
10 Correct 37 ms 37700 KB Output is correct
11 Correct 38 ms 37624 KB Output is correct
12 Correct 38 ms 37596 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 36 ms 37624 KB Output is correct
15 Correct 37 ms 37624 KB Output is correct
16 Correct 38 ms 37496 KB Output is correct
17 Correct 37 ms 37496 KB Output is correct
18 Correct 36 ms 37624 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 36 ms 37624 KB Output is correct
21 Correct 38 ms 37496 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 41 ms 37624 KB Output is correct
25 Correct 37 ms 37496 KB Output is correct
26 Correct 41 ms 38008 KB Output is correct
27 Correct 44 ms 38008 KB Output is correct
28 Correct 45 ms 38136 KB Output is correct
29 Correct 43 ms 38136 KB Output is correct
30 Correct 44 ms 38008 KB Output is correct
31 Correct 41 ms 37596 KB Output is correct
32 Correct 44 ms 38108 KB Output is correct
33 Correct 45 ms 37624 KB Output is correct
34 Correct 45 ms 38008 KB Output is correct
35 Correct 43 ms 38264 KB Output is correct
36 Correct 40 ms 38008 KB Output is correct
37 Correct 40 ms 38008 KB Output is correct
38 Correct 37 ms 37624 KB Output is correct
39 Correct 41 ms 38008 KB Output is correct
40 Correct 41 ms 38136 KB Output is correct
41 Correct 40 ms 38028 KB Output is correct
42 Correct 40 ms 38008 KB Output is correct
43 Correct 40 ms 38108 KB Output is correct
44 Correct 37 ms 37624 KB Output is correct
45 Correct 40 ms 38136 KB Output is correct
46 Correct 40 ms 38008 KB Output is correct
47 Correct 38 ms 37624 KB Output is correct
48 Correct 40 ms 38008 KB Output is correct
49 Correct 39 ms 38136 KB Output is correct
50 Correct 41 ms 38136 KB Output is correct
51 Correct 40 ms 38012 KB Output is correct
52 Correct 40 ms 38008 KB Output is correct
53 Correct 40 ms 38008 KB Output is correct
54 Correct 39 ms 37980 KB Output is correct
55 Correct 37 ms 37496 KB Output is correct
56 Correct 139 ms 52008 KB Output is correct
57 Correct 185 ms 51740 KB Output is correct
58 Correct 39 ms 38008 KB Output is correct
59 Correct 37 ms 37496 KB Output is correct
60 Correct 37 ms 37596 KB Output is correct
61 Correct 264 ms 51696 KB Output is correct
62 Correct 39 ms 38008 KB Output is correct
63 Correct 220 ms 56056 KB Output is correct
64 Correct 266 ms 51848 KB Output is correct
65 Correct 39 ms 38008 KB Output is correct
66 Correct 192 ms 52060 KB Output is correct
67 Correct 39 ms 38008 KB Output is correct
68 Correct 40 ms 38012 KB Output is correct
69 Correct 145 ms 52024 KB Output is correct
70 Correct 46 ms 38136 KB Output is correct
71 Correct 194 ms 51820 KB Output is correct
72 Correct 197 ms 53240 KB Output is correct
73 Correct 42 ms 37496 KB Output is correct
74 Correct 228 ms 56500 KB Output is correct
75 Correct 197 ms 54032 KB Output is correct
76 Correct 39 ms 37952 KB Output is correct
77 Correct 40 ms 38012 KB Output is correct
78 Correct 144 ms 52080 KB Output is correct
79 Correct 200 ms 57576 KB Output is correct
80 Correct 41 ms 38056 KB Output is correct
81 Correct 39 ms 38008 KB Output is correct
82 Correct 37 ms 37508 KB Output is correct
83 Correct 37 ms 37496 KB Output is correct
84 Correct 39 ms 38008 KB Output is correct
85 Correct 210 ms 53460 KB Output is correct
86 Correct 43 ms 38008 KB Output is correct
87 Correct 172 ms 51960 KB Output is correct
88 Correct 37 ms 37624 KB Output is correct
89 Correct 183 ms 51960 KB Output is correct
90 Correct 220 ms 53624 KB Output is correct
91 Correct 264 ms 57196 KB Output is correct
92 Correct 138 ms 51948 KB Output is correct
93 Correct 40 ms 38084 KB Output is correct
94 Correct 38 ms 37624 KB Output is correct
95 Correct 212 ms 56804 KB Output is correct
96 Correct 277 ms 59412 KB Output is correct
97 Correct 40 ms 38008 KB Output is correct
98 Correct 41 ms 37528 KB Output is correct
99 Correct 179 ms 53872 KB Output is correct
100 Correct 44 ms 38008 KB Output is correct
101 Correct 207 ms 52600 KB Output is correct
102 Correct 277 ms 58700 KB Output is correct
103 Correct 45 ms 38136 KB Output is correct
104 Correct 46 ms 38136 KB Output is correct
105 Correct 44 ms 38008 KB Output is correct
106 Correct 44 ms 38008 KB Output is correct
107 Correct 40 ms 38008 KB Output is correct
108 Correct 939 ms 117816 KB Output is correct
109 Correct 1408 ms 121932 KB Output is correct
110 Correct 1358 ms 131308 KB Output is correct
111 Incorrect 1760 ms 149012 KB Output isn't correct
112 Halted 0 ms 0 KB -