답안 #164139

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
164139 2019-11-17T15:35:00 Z combi1k1 Mergers (JOI19_mergers) C++14
100 / 100
1915 ms 144500 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 < 18 ; ++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 < 19 ; ++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 = 18 ; 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 36 ms 37624 KB Output is correct
3 Correct 38 ms 37624 KB Output is correct
4 Correct 37 ms 37496 KB Output is correct
5 Correct 38 ms 37624 KB Output is correct
6 Correct 37 ms 37624 KB Output is correct
7 Correct 37 ms 37592 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 41 ms 37624 KB Output is correct
10 Correct 42 ms 37628 KB Output is correct
11 Correct 37 ms 37624 KB Output is correct
12 Correct 37 ms 37624 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 38 ms 37628 KB Output is correct
15 Correct 37 ms 37496 KB Output is correct
16 Correct 37 ms 37624 KB Output is correct
17 Correct 37 ms 37628 KB Output is correct
18 Correct 61 ms 37496 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 37 ms 37624 KB Output is correct
21 Correct 37 ms 37572 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 37 ms 37500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 36 ms 37624 KB Output is correct
3 Correct 38 ms 37624 KB Output is correct
4 Correct 37 ms 37496 KB Output is correct
5 Correct 38 ms 37624 KB Output is correct
6 Correct 37 ms 37624 KB Output is correct
7 Correct 37 ms 37592 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 41 ms 37624 KB Output is correct
10 Correct 42 ms 37628 KB Output is correct
11 Correct 37 ms 37624 KB Output is correct
12 Correct 37 ms 37624 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 38 ms 37628 KB Output is correct
15 Correct 37 ms 37496 KB Output is correct
16 Correct 37 ms 37624 KB Output is correct
17 Correct 37 ms 37628 KB Output is correct
18 Correct 61 ms 37496 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 37 ms 37624 KB Output is correct
21 Correct 37 ms 37572 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 37 ms 37500 KB Output is correct
25 Correct 38 ms 37624 KB Output is correct
26 Correct 41 ms 38068 KB Output is correct
27 Correct 39 ms 38008 KB Output is correct
28 Correct 42 ms 38136 KB Output is correct
29 Correct 42 ms 38140 KB Output is correct
30 Correct 40 ms 38008 KB Output is correct
31 Correct 38 ms 37624 KB Output is correct
32 Correct 42 ms 38136 KB Output is correct
33 Correct 37 ms 37596 KB Output is correct
34 Correct 39 ms 38008 KB Output is correct
35 Correct 41 ms 38080 KB Output is correct
36 Correct 45 ms 38012 KB Output is correct
37 Correct 46 ms 38008 KB Output is correct
38 Correct 37 ms 37624 KB Output is correct
39 Correct 40 ms 38008 KB Output is correct
40 Correct 40 ms 38008 KB Output is correct
41 Correct 45 ms 37980 KB Output is correct
42 Correct 44 ms 38008 KB Output is correct
43 Correct 44 ms 38264 KB Output is correct
44 Correct 41 ms 37624 KB Output is correct
45 Correct 45 ms 38136 KB Output is correct
46 Correct 44 ms 38008 KB Output is correct
47 Correct 42 ms 37624 KB Output is correct
48 Correct 44 ms 38012 KB Output is correct
49 Correct 44 ms 38136 KB Output is correct
50 Correct 46 ms 38264 KB Output is correct
51 Correct 45 ms 38008 KB Output is correct
52 Correct 44 ms 38136 KB Output is correct
53 Correct 44 ms 38008 KB Output is correct
54 Correct 44 ms 38008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 36 ms 37624 KB Output is correct
3 Correct 38 ms 37624 KB Output is correct
4 Correct 37 ms 37496 KB Output is correct
5 Correct 38 ms 37624 KB Output is correct
6 Correct 37 ms 37624 KB Output is correct
7 Correct 37 ms 37592 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 41 ms 37624 KB Output is correct
10 Correct 42 ms 37628 KB Output is correct
11 Correct 37 ms 37624 KB Output is correct
12 Correct 37 ms 37624 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 38 ms 37628 KB Output is correct
15 Correct 37 ms 37496 KB Output is correct
16 Correct 37 ms 37624 KB Output is correct
17 Correct 37 ms 37628 KB Output is correct
18 Correct 61 ms 37496 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 37 ms 37624 KB Output is correct
21 Correct 37 ms 37572 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 37 ms 37500 KB Output is correct
25 Correct 41 ms 37628 KB Output is correct
26 Correct 145 ms 52076 KB Output is correct
27 Correct 201 ms 51704 KB Output is correct
28 Correct 44 ms 38008 KB Output is correct
29 Correct 37 ms 37624 KB Output is correct
30 Correct 41 ms 37548 KB Output is correct
31 Correct 203 ms 51704 KB Output is correct
32 Correct 49 ms 38008 KB Output is correct
33 Correct 223 ms 56060 KB Output is correct
34 Correct 195 ms 51832 KB Output is correct
35 Correct 42 ms 37980 KB Output is correct
36 Correct 194 ms 52232 KB Output is correct
37 Correct 39 ms 38008 KB Output is correct
38 Correct 40 ms 38008 KB Output is correct
39 Correct 150 ms 52092 KB Output is correct
40 Correct 41 ms 38112 KB Output is correct
41 Correct 199 ms 51760 KB Output is correct
42 Correct 207 ms 53112 KB Output is correct
43 Correct 38 ms 37596 KB Output is correct
44 Correct 229 ms 56552 KB Output is correct
45 Correct 215 ms 54008 KB Output is correct
46 Correct 39 ms 38008 KB Output is correct
47 Correct 44 ms 38008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 52132 KB Output is correct
2 Correct 207 ms 57452 KB Output is correct
3 Correct 41 ms 38136 KB Output is correct
4 Correct 39 ms 38008 KB Output is correct
5 Correct 37 ms 37628 KB Output is correct
6 Correct 37 ms 37496 KB Output is correct
7 Correct 39 ms 37980 KB Output is correct
8 Correct 206 ms 53496 KB Output is correct
9 Correct 40 ms 38008 KB Output is correct
10 Correct 176 ms 51960 KB Output is correct
11 Correct 37 ms 37624 KB Output is correct
12 Correct 180 ms 51908 KB Output is correct
13 Correct 229 ms 53628 KB Output is correct
14 Correct 309 ms 57324 KB Output is correct
15 Correct 142 ms 51952 KB Output is correct
16 Correct 44 ms 38008 KB Output is correct
17 Correct 38 ms 37628 KB Output is correct
18 Correct 233 ms 56808 KB Output is correct
19 Correct 289 ms 59508 KB Output is correct
20 Correct 40 ms 38012 KB Output is correct
21 Correct 42 ms 37624 KB Output is correct
22 Correct 172 ms 53872 KB Output is correct
23 Correct 40 ms 38012 KB Output is correct
24 Correct 200 ms 52728 KB Output is correct
25 Correct 241 ms 58732 KB Output is correct
26 Correct 40 ms 38136 KB Output is correct
27 Correct 40 ms 38136 KB Output is correct
28 Correct 39 ms 38008 KB Output is correct
29 Correct 40 ms 38012 KB Output is correct
30 Correct 44 ms 38008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 37624 KB Output is correct
2 Correct 36 ms 37624 KB Output is correct
3 Correct 38 ms 37624 KB Output is correct
4 Correct 37 ms 37496 KB Output is correct
5 Correct 38 ms 37624 KB Output is correct
6 Correct 37 ms 37624 KB Output is correct
7 Correct 37 ms 37592 KB Output is correct
8 Correct 41 ms 37624 KB Output is correct
9 Correct 41 ms 37624 KB Output is correct
10 Correct 42 ms 37628 KB Output is correct
11 Correct 37 ms 37624 KB Output is correct
12 Correct 37 ms 37624 KB Output is correct
13 Correct 37 ms 37624 KB Output is correct
14 Correct 38 ms 37628 KB Output is correct
15 Correct 37 ms 37496 KB Output is correct
16 Correct 37 ms 37624 KB Output is correct
17 Correct 37 ms 37628 KB Output is correct
18 Correct 61 ms 37496 KB Output is correct
19 Correct 37 ms 37624 KB Output is correct
20 Correct 37 ms 37624 KB Output is correct
21 Correct 37 ms 37572 KB Output is correct
22 Correct 37 ms 37624 KB Output is correct
23 Correct 37 ms 37496 KB Output is correct
24 Correct 37 ms 37500 KB Output is correct
25 Correct 38 ms 37624 KB Output is correct
26 Correct 41 ms 38068 KB Output is correct
27 Correct 39 ms 38008 KB Output is correct
28 Correct 42 ms 38136 KB Output is correct
29 Correct 42 ms 38140 KB Output is correct
30 Correct 40 ms 38008 KB Output is correct
31 Correct 38 ms 37624 KB Output is correct
32 Correct 42 ms 38136 KB Output is correct
33 Correct 37 ms 37596 KB Output is correct
34 Correct 39 ms 38008 KB Output is correct
35 Correct 41 ms 38080 KB Output is correct
36 Correct 45 ms 38012 KB Output is correct
37 Correct 46 ms 38008 KB Output is correct
38 Correct 37 ms 37624 KB Output is correct
39 Correct 40 ms 38008 KB Output is correct
40 Correct 40 ms 38008 KB Output is correct
41 Correct 45 ms 37980 KB Output is correct
42 Correct 44 ms 38008 KB Output is correct
43 Correct 44 ms 38264 KB Output is correct
44 Correct 41 ms 37624 KB Output is correct
45 Correct 45 ms 38136 KB Output is correct
46 Correct 44 ms 38008 KB Output is correct
47 Correct 42 ms 37624 KB Output is correct
48 Correct 44 ms 38012 KB Output is correct
49 Correct 44 ms 38136 KB Output is correct
50 Correct 46 ms 38264 KB Output is correct
51 Correct 45 ms 38008 KB Output is correct
52 Correct 44 ms 38136 KB Output is correct
53 Correct 44 ms 38008 KB Output is correct
54 Correct 44 ms 38008 KB Output is correct
55 Correct 41 ms 37628 KB Output is correct
56 Correct 145 ms 52076 KB Output is correct
57 Correct 201 ms 51704 KB Output is correct
58 Correct 44 ms 38008 KB Output is correct
59 Correct 37 ms 37624 KB Output is correct
60 Correct 41 ms 37548 KB Output is correct
61 Correct 203 ms 51704 KB Output is correct
62 Correct 49 ms 38008 KB Output is correct
63 Correct 223 ms 56060 KB Output is correct
64 Correct 195 ms 51832 KB Output is correct
65 Correct 42 ms 37980 KB Output is correct
66 Correct 194 ms 52232 KB Output is correct
67 Correct 39 ms 38008 KB Output is correct
68 Correct 40 ms 38008 KB Output is correct
69 Correct 150 ms 52092 KB Output is correct
70 Correct 41 ms 38112 KB Output is correct
71 Correct 199 ms 51760 KB Output is correct
72 Correct 207 ms 53112 KB Output is correct
73 Correct 38 ms 37596 KB Output is correct
74 Correct 229 ms 56552 KB Output is correct
75 Correct 215 ms 54008 KB Output is correct
76 Correct 39 ms 38008 KB Output is correct
77 Correct 44 ms 38008 KB Output is correct
78 Correct 159 ms 52132 KB Output is correct
79 Correct 207 ms 57452 KB Output is correct
80 Correct 41 ms 38136 KB Output is correct
81 Correct 39 ms 38008 KB Output is correct
82 Correct 37 ms 37628 KB Output is correct
83 Correct 37 ms 37496 KB Output is correct
84 Correct 39 ms 37980 KB Output is correct
85 Correct 206 ms 53496 KB Output is correct
86 Correct 40 ms 38008 KB Output is correct
87 Correct 176 ms 51960 KB Output is correct
88 Correct 37 ms 37624 KB Output is correct
89 Correct 180 ms 51908 KB Output is correct
90 Correct 229 ms 53628 KB Output is correct
91 Correct 309 ms 57324 KB Output is correct
92 Correct 142 ms 51952 KB Output is correct
93 Correct 44 ms 38008 KB Output is correct
94 Correct 38 ms 37628 KB Output is correct
95 Correct 233 ms 56808 KB Output is correct
96 Correct 289 ms 59508 KB Output is correct
97 Correct 40 ms 38012 KB Output is correct
98 Correct 42 ms 37624 KB Output is correct
99 Correct 172 ms 53872 KB Output is correct
100 Correct 40 ms 38012 KB Output is correct
101 Correct 200 ms 52728 KB Output is correct
102 Correct 241 ms 58732 KB Output is correct
103 Correct 40 ms 38136 KB Output is correct
104 Correct 40 ms 38136 KB Output is correct
105 Correct 39 ms 38008 KB Output is correct
106 Correct 40 ms 38012 KB Output is correct
107 Correct 44 ms 38008 KB Output is correct
108 Correct 941 ms 109668 KB Output is correct
109 Correct 1363 ms 116340 KB Output is correct
110 Correct 1466 ms 125776 KB Output is correct
111 Correct 1915 ms 141124 KB Output is correct
112 Correct 1635 ms 139072 KB Output is correct
113 Correct 1122 ms 134060 KB Output is correct
114 Correct 1158 ms 111432 KB Output is correct
115 Correct 1155 ms 112524 KB Output is correct
116 Correct 1171 ms 113348 KB Output is correct
117 Correct 1618 ms 136544 KB Output is correct
118 Correct 983 ms 109176 KB Output is correct
119 Correct 1779 ms 136292 KB Output is correct
120 Correct 1618 ms 144500 KB Output is correct
121 Correct 1677 ms 136248 KB Output is correct
122 Correct 1389 ms 120296 KB Output is correct
123 Correct 1083 ms 137844 KB Output is correct
124 Correct 1514 ms 130004 KB Output is correct