답안 #949260

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949260 2024-03-19T04:25:06 Z Tuanlinh123 Mergers (JOI19_mergers) C++17
100 / 100
645 ms 241324 KB
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
 
const ll maxn=500005;
pll sp[20][maxn*2];
vector <pll> euler;
vector <ll> A[maxn];
ll P[maxn], col[maxn], lca[maxn], pa[maxn], dsu[maxn];
 
void dfs(ll u)
{
    lca[u]=sz(euler)+1, euler.pb({lca[u], u});
    for (ll v:A[u])
        if (v!=pa[u])
            pa[v]=u, dfs(v), euler.pb({lca[u], u});
}
 
void initlca()
{
    ll n=sz(euler);
    for (ll i=0; i<n; i++)
        sp[0][i+1]=euler[i];
    for (ll i=1; i<20; i++)
        for (ll j=1; j+(1<<i)<=n+1; j++)
            sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
}
 
ll LCA(ll u, ll v)
{
    if (!u || !v) return u+v;
    ll l=lca[u], r=lca[v];
    if (l>r) swap(l, r);
    ll j=__lg(r-l+1);
    return min(sp[j][l], sp[j][r-(1<<j)+1]).se;
}
 
ll Find(ll i)
{
    if (dsu[i]<0) return i;
    return dsu[i]=Find(dsu[i]);
}
 
void Union(ll a, ll b)
{
    a=Find(a), b=Find(b);
    if (a==b) return;
    if (dsu[a]>dsu[b]) swap(a, b);
    P[a]=LCA(P[a], P[b]), dsu[a]+=dsu[b], dsu[b]=a;
}
 
void dfs1(ll u)
{
    for (ll v:A[u])
        if (v!=pa[u])
            dfs1(v), P[col[u]]=LCA(P[col[u]], P[col[v]]);
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n, k; cin >> n >> k;
    for (ll i=1; i<n; i++)
    {
        ll u, v; cin >> u >> v;
        A[u].pb(v), A[v].pb(u);
    }
    dfs(1), initlca();
    for (ll i=1; i<=n; i++)
        cin >> col[i], P[col[i]]=LCA(P[col[i]], i), dsu[i]=-1;
    dfs1(1);
    for (ll i=1; i<=n; i++)
        if (i!=P[col[i]])
            Union(col[i], col[pa[i]]);
    for (ll i=1; i<=n; i++) col[i]=Find(col[i]);
    for (ll i=1; i<=k; i++) P[i]=0;
    for (ll i=1; i<=n; i++)
        for (ll j:A[i])
            if (col[i]!=col[j])
            {
                if (P[col[i]]==0) P[col[i]]=col[j];
                else if (P[col[i]]!=col[j]) P[col[i]]=-1;
            }
    ll cnt=0;
    for (ll i=1; i<=k; i++)
        cnt+=P[i]>0;
    cout << (cnt+1)/2;
 
}

Compilation message

mergers.cpp: In function 'void initlca()':
mergers.cpp:33:53: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   33 |             sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
      |                                                    ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 7 ms 33116 KB Output is correct
4 Correct 4 ms 26972 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 6 ms 33116 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 5 ms 33116 KB Output is correct
9 Correct 5 ms 26972 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 6 ms 33276 KB Output is correct
12 Correct 6 ms 33368 KB Output is correct
13 Correct 5 ms 26972 KB Output is correct
14 Correct 6 ms 33212 KB Output is correct
15 Correct 6 ms 33116 KB Output is correct
16 Correct 5 ms 24924 KB Output is correct
17 Correct 5 ms 25104 KB Output is correct
18 Correct 6 ms 33316 KB Output is correct
19 Correct 5 ms 24988 KB Output is correct
20 Correct 4 ms 24924 KB Output is correct
21 Correct 6 ms 33116 KB Output is correct
22 Correct 5 ms 33116 KB Output is correct
23 Correct 5 ms 26968 KB Output is correct
24 Correct 5 ms 26972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 7 ms 33116 KB Output is correct
4 Correct 4 ms 26972 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 6 ms 33116 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 5 ms 33116 KB Output is correct
9 Correct 5 ms 26972 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 6 ms 33276 KB Output is correct
12 Correct 6 ms 33368 KB Output is correct
13 Correct 5 ms 26972 KB Output is correct
14 Correct 6 ms 33212 KB Output is correct
15 Correct 6 ms 33116 KB Output is correct
16 Correct 5 ms 24924 KB Output is correct
17 Correct 5 ms 25104 KB Output is correct
18 Correct 6 ms 33316 KB Output is correct
19 Correct 5 ms 24988 KB Output is correct
20 Correct 4 ms 24924 KB Output is correct
21 Correct 6 ms 33116 KB Output is correct
22 Correct 5 ms 33116 KB Output is correct
23 Correct 5 ms 26968 KB Output is correct
24 Correct 5 ms 26972 KB Output is correct
25 Correct 5 ms 31068 KB Output is correct
26 Correct 8 ms 43608 KB Output is correct
27 Correct 9 ms 43608 KB Output is correct
28 Correct 8 ms 43872 KB Output is correct
29 Correct 8 ms 43608 KB Output is correct
30 Correct 8 ms 43612 KB Output is correct
31 Correct 5 ms 31260 KB Output is correct
32 Correct 8 ms 43868 KB Output is correct
33 Correct 5 ms 31068 KB Output is correct
34 Correct 8 ms 43612 KB Output is correct
35 Correct 8 ms 43612 KB Output is correct
36 Correct 8 ms 43612 KB Output is correct
37 Correct 9 ms 43764 KB Output is correct
38 Correct 5 ms 33116 KB Output is correct
39 Correct 8 ms 43612 KB Output is correct
40 Correct 8 ms 43608 KB Output is correct
41 Correct 8 ms 43612 KB Output is correct
42 Correct 7 ms 43612 KB Output is correct
43 Correct 8 ms 44008 KB Output is correct
44 Correct 6 ms 33116 KB Output is correct
45 Correct 8 ms 43844 KB Output is correct
46 Correct 8 ms 43612 KB Output is correct
47 Correct 5 ms 33116 KB Output is correct
48 Correct 8 ms 43612 KB Output is correct
49 Correct 7 ms 43612 KB Output is correct
50 Correct 8 ms 43864 KB Output is correct
51 Correct 8 ms 43864 KB Output is correct
52 Correct 8 ms 43608 KB Output is correct
53 Correct 8 ms 43612 KB Output is correct
54 Correct 8 ms 43612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 7 ms 33116 KB Output is correct
4 Correct 4 ms 26972 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 6 ms 33116 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 5 ms 33116 KB Output is correct
9 Correct 5 ms 26972 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 6 ms 33276 KB Output is correct
12 Correct 6 ms 33368 KB Output is correct
13 Correct 5 ms 26972 KB Output is correct
14 Correct 6 ms 33212 KB Output is correct
15 Correct 6 ms 33116 KB Output is correct
16 Correct 5 ms 24924 KB Output is correct
17 Correct 5 ms 25104 KB Output is correct
18 Correct 6 ms 33316 KB Output is correct
19 Correct 5 ms 24988 KB Output is correct
20 Correct 4 ms 24924 KB Output is correct
21 Correct 6 ms 33116 KB Output is correct
22 Correct 5 ms 33116 KB Output is correct
23 Correct 5 ms 26968 KB Output is correct
24 Correct 5 ms 26972 KB Output is correct
25 Correct 5 ms 31068 KB Output is correct
26 Correct 49 ms 84304 KB Output is correct
27 Correct 57 ms 83844 KB Output is correct
28 Correct 8 ms 43612 KB Output is correct
29 Correct 5 ms 31068 KB Output is correct
30 Correct 5 ms 31068 KB Output is correct
31 Correct 59 ms 83980 KB Output is correct
32 Correct 9 ms 43612 KB Output is correct
33 Correct 70 ms 92612 KB Output is correct
34 Correct 57 ms 83908 KB Output is correct
35 Correct 9 ms 43608 KB Output is correct
36 Correct 60 ms 84744 KB Output is correct
37 Correct 8 ms 43608 KB Output is correct
38 Correct 8 ms 43612 KB Output is correct
39 Correct 57 ms 84324 KB Output is correct
40 Correct 8 ms 43868 KB Output is correct
41 Correct 56 ms 83908 KB Output is correct
42 Correct 61 ms 86720 KB Output is correct
43 Correct 5 ms 33112 KB Output is correct
44 Correct 71 ms 92864 KB Output is correct
45 Correct 82 ms 88708 KB Output is correct
46 Correct 7 ms 43608 KB Output is correct
47 Correct 8 ms 43612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 84364 KB Output is correct
2 Correct 59 ms 84512 KB Output is correct
3 Correct 8 ms 43612 KB Output is correct
4 Correct 8 ms 43612 KB Output is correct
5 Correct 5 ms 31176 KB Output is correct
6 Correct 5 ms 31068 KB Output is correct
7 Correct 8 ms 43608 KB Output is correct
8 Correct 68 ms 84132 KB Output is correct
9 Correct 8 ms 43608 KB Output is correct
10 Correct 62 ms 83868 KB Output is correct
11 Correct 6 ms 33116 KB Output is correct
12 Correct 70 ms 83820 KB Output is correct
13 Correct 71 ms 84040 KB Output is correct
14 Correct 61 ms 84412 KB Output is correct
15 Correct 52 ms 84420 KB Output is correct
16 Correct 8 ms 43608 KB Output is correct
17 Correct 6 ms 33116 KB Output is correct
18 Correct 50 ms 84652 KB Output is correct
19 Correct 66 ms 89008 KB Output is correct
20 Correct 8 ms 43608 KB Output is correct
21 Correct 6 ms 33112 KB Output is correct
22 Correct 56 ms 84296 KB Output is correct
23 Correct 8 ms 43608 KB Output is correct
24 Correct 72 ms 83940 KB Output is correct
25 Correct 61 ms 87488 KB Output is correct
26 Correct 8 ms 43608 KB Output is correct
27 Correct 7 ms 43712 KB Output is correct
28 Correct 9 ms 43612 KB Output is correct
29 Correct 7 ms 43612 KB Output is correct
30 Correct 7 ms 43612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 7 ms 33116 KB Output is correct
4 Correct 4 ms 26972 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 6 ms 33116 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 5 ms 33116 KB Output is correct
9 Correct 5 ms 26972 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 6 ms 33276 KB Output is correct
12 Correct 6 ms 33368 KB Output is correct
13 Correct 5 ms 26972 KB Output is correct
14 Correct 6 ms 33212 KB Output is correct
15 Correct 6 ms 33116 KB Output is correct
16 Correct 5 ms 24924 KB Output is correct
17 Correct 5 ms 25104 KB Output is correct
18 Correct 6 ms 33316 KB Output is correct
19 Correct 5 ms 24988 KB Output is correct
20 Correct 4 ms 24924 KB Output is correct
21 Correct 6 ms 33116 KB Output is correct
22 Correct 5 ms 33116 KB Output is correct
23 Correct 5 ms 26968 KB Output is correct
24 Correct 5 ms 26972 KB Output is correct
25 Correct 5 ms 31068 KB Output is correct
26 Correct 8 ms 43608 KB Output is correct
27 Correct 9 ms 43608 KB Output is correct
28 Correct 8 ms 43872 KB Output is correct
29 Correct 8 ms 43608 KB Output is correct
30 Correct 8 ms 43612 KB Output is correct
31 Correct 5 ms 31260 KB Output is correct
32 Correct 8 ms 43868 KB Output is correct
33 Correct 5 ms 31068 KB Output is correct
34 Correct 8 ms 43612 KB Output is correct
35 Correct 8 ms 43612 KB Output is correct
36 Correct 8 ms 43612 KB Output is correct
37 Correct 9 ms 43764 KB Output is correct
38 Correct 5 ms 33116 KB Output is correct
39 Correct 8 ms 43612 KB Output is correct
40 Correct 8 ms 43608 KB Output is correct
41 Correct 8 ms 43612 KB Output is correct
42 Correct 7 ms 43612 KB Output is correct
43 Correct 8 ms 44008 KB Output is correct
44 Correct 6 ms 33116 KB Output is correct
45 Correct 8 ms 43844 KB Output is correct
46 Correct 8 ms 43612 KB Output is correct
47 Correct 5 ms 33116 KB Output is correct
48 Correct 8 ms 43612 KB Output is correct
49 Correct 7 ms 43612 KB Output is correct
50 Correct 8 ms 43864 KB Output is correct
51 Correct 8 ms 43864 KB Output is correct
52 Correct 8 ms 43608 KB Output is correct
53 Correct 8 ms 43612 KB Output is correct
54 Correct 8 ms 43612 KB Output is correct
55 Correct 5 ms 31068 KB Output is correct
56 Correct 49 ms 84304 KB Output is correct
57 Correct 57 ms 83844 KB Output is correct
58 Correct 8 ms 43612 KB Output is correct
59 Correct 5 ms 31068 KB Output is correct
60 Correct 5 ms 31068 KB Output is correct
61 Correct 59 ms 83980 KB Output is correct
62 Correct 9 ms 43612 KB Output is correct
63 Correct 70 ms 92612 KB Output is correct
64 Correct 57 ms 83908 KB Output is correct
65 Correct 9 ms 43608 KB Output is correct
66 Correct 60 ms 84744 KB Output is correct
67 Correct 8 ms 43608 KB Output is correct
68 Correct 8 ms 43612 KB Output is correct
69 Correct 57 ms 84324 KB Output is correct
70 Correct 8 ms 43868 KB Output is correct
71 Correct 56 ms 83908 KB Output is correct
72 Correct 61 ms 86720 KB Output is correct
73 Correct 5 ms 33112 KB Output is correct
74 Correct 71 ms 92864 KB Output is correct
75 Correct 82 ms 88708 KB Output is correct
76 Correct 7 ms 43608 KB Output is correct
77 Correct 8 ms 43612 KB Output is correct
78 Correct 52 ms 84364 KB Output is correct
79 Correct 59 ms 84512 KB Output is correct
80 Correct 8 ms 43612 KB Output is correct
81 Correct 8 ms 43612 KB Output is correct
82 Correct 5 ms 31176 KB Output is correct
83 Correct 5 ms 31068 KB Output is correct
84 Correct 8 ms 43608 KB Output is correct
85 Correct 68 ms 84132 KB Output is correct
86 Correct 8 ms 43608 KB Output is correct
87 Correct 62 ms 83868 KB Output is correct
88 Correct 6 ms 33116 KB Output is correct
89 Correct 70 ms 83820 KB Output is correct
90 Correct 71 ms 84040 KB Output is correct
91 Correct 61 ms 84412 KB Output is correct
92 Correct 52 ms 84420 KB Output is correct
93 Correct 8 ms 43608 KB Output is correct
94 Correct 6 ms 33116 KB Output is correct
95 Correct 50 ms 84652 KB Output is correct
96 Correct 66 ms 89008 KB Output is correct
97 Correct 8 ms 43608 KB Output is correct
98 Correct 6 ms 33112 KB Output is correct
99 Correct 56 ms 84296 KB Output is correct
100 Correct 8 ms 43608 KB Output is correct
101 Correct 72 ms 83940 KB Output is correct
102 Correct 61 ms 87488 KB Output is correct
103 Correct 8 ms 43608 KB Output is correct
104 Correct 7 ms 43712 KB Output is correct
105 Correct 9 ms 43612 KB Output is correct
106 Correct 7 ms 43612 KB Output is correct
107 Correct 7 ms 43612 KB Output is correct
108 Correct 362 ms 200604 KB Output is correct
109 Correct 606 ms 220276 KB Output is correct
110 Correct 581 ms 241324 KB Output is correct
111 Correct 591 ms 236716 KB Output is correct
112 Correct 575 ms 225216 KB Output is correct
113 Correct 302 ms 210836 KB Output is correct
114 Correct 473 ms 206260 KB Output is correct
115 Correct 490 ms 206040 KB Output is correct
116 Correct 508 ms 208512 KB Output is correct
117 Correct 522 ms 209308 KB Output is correct
118 Correct 480 ms 206252 KB Output is correct
119 Correct 487 ms 209072 KB Output is correct
120 Correct 583 ms 226220 KB Output is correct
121 Correct 475 ms 209312 KB Output is correct
122 Correct 570 ms 209040 KB Output is correct
123 Correct 277 ms 210484 KB Output is correct
124 Correct 645 ms 208312 KB Output is correct