답안 #790360

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
790360 2023-07-22T15:09:18 Z 1075508020060209tc Mergers (JOI19_mergers) C++14
100 / 100
1274 ms 236724 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
//#define int long long

int uf[500005];
int fin(int x){
if(uf[x]==x){return x;}
uf[x]=fin(uf[x]);
return uf[x];
}
void mrg(int a,int b){
int pa=fin(a);int pb=fin(b);
if(pa==pb){return;}
uf[pa]=pb;
}



int n;int K;
vector<int>e[500005];
int dph[500005];
vector<int>igp[500005];
int rdfn[500005];
vector<int>dfn;
int rmq[22][1500005];
void dfs(int nw,int pa){
dph[nw]=dph[pa]+1;
rdfn[nw]=dfn.size();
dfn.push_back(nw);
for(int i=0;i<e[nw].size();i++){
    int v=e[nw][i];
    if(v==pa){continue;}
    dfs(v,nw);
    dfn.push_back(nw);
}
}
bool cmp(int i,int j){
if(rdfn[i]<rdfn[j]){return 1;}
return 0;
}
int lca(int a,int b){
int l=rdfn[a];int r=rdfn[b];
if(r<l){swap(l,r);}
int lg=__lg(r-l+1);
int ret=rmq[lg][l];
if(dph[ret]>dph[rmq[lg][r-(1<<lg)+1]]){
    ret=rmq[lg][r-(1<<lg)+1];
}
return ret;
}
int dp[500005];

void dfs2(int nw,int pa){
for(int i=0;i<e[nw].size();i++){
    int v=e[nw][i];
    if(v==pa){continue;}
    dfs2(v,nw);
    if(dp[v]>=1){
        mrg(nw,v);
    }
    dp[nw]+=dp[v];
}
}

set<int>ne[500005];

signed main()
{
    cin>>n>>K;
    for(int i=1;i<=n-1;i++){
        int a;int b;
        cin>>a>>b;
        e[a].push_back(b);
        e[b].push_back(a);
    }

    dfn.push_back(0);
    dfs(1,0);
    for(int i=1;i<=n;i++){
        int typ;
        cin>>typ;
        igp[typ].push_back(i);
    }
    for(int i=1;i<=K;i++){
        sort(igp[i].begin(),igp[i].end(),cmp);
    }
    for(int i=1;i<dfn.size();i++){
        rmq[0][i]=dfn[i];
    }
    for(int i=1;i<=21;i++){
        for(int j=1;(j+(1<<i)-1)<dfn.size();j++){
            rmq[i][j]=rmq[i-1][j];
            if(dph[rmq[i][j]]>dph[rmq[i-1][j+(1<<(i-1))]]){
                rmq[i][j]=rmq[i-1][j+(1<<(i-1))];
            }
        }
    }

    for(int k=1;k<=K;k++){
        for(int i=1;i<igp[k].size();i++){
            int a=igp[k][i-1];int b=igp[k][i];
            dp[a]++;dp[b]++;
            dp[lca(a,b)]-=2;
        }
        dp[igp[k][0]]++;
        dp[igp[k].back()]++;
        dp[lca(igp[k][0],igp[k].back())]-=2;
    }
    for(int i=1;i<=n;i++){
        uf[i]=i;
    }
    dfs2(1,0);
    for(int i=1;i<=n;i++){
            int nw=fin(i);
        for(int j=0;j<e[i].size();j++){
            int v=fin(e[i][j]);
            if(nw==v){continue;}
            ne[nw].insert(v);
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        if(ne[i].size()==1){ans++;}
    }
    ans=(ans+1)/2;
    cout<<ans<<endl;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:32:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(int, int)':
mergers.cpp:56:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:89:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for(int i=1;i<dfn.size();i++){
      |                 ~^~~~~~~~~~~
mergers.cpp:93:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for(int j=1;(j+(1<<i)-1)<dfn.size();j++){
      |                     ~~~~~~~~~~~~^~~~~~~~~~~
mergers.cpp:102:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for(int i=1;i<igp[k].size();i++){
      |                     ~^~~~~~~~~~~~~~
mergers.cpp:117:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |         for(int j=0;j<e[i].size();j++){
      |                     ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 47188 KB Output is correct
2 Correct 25 ms 47304 KB Output is correct
3 Correct 27 ms 47296 KB Output is correct
4 Correct 25 ms 47316 KB Output is correct
5 Correct 26 ms 47304 KB Output is correct
6 Correct 25 ms 47252 KB Output is correct
7 Correct 25 ms 47252 KB Output is correct
8 Correct 24 ms 47344 KB Output is correct
9 Correct 28 ms 47236 KB Output is correct
10 Correct 24 ms 47268 KB Output is correct
11 Correct 25 ms 47312 KB Output is correct
12 Correct 25 ms 47340 KB Output is correct
13 Correct 27 ms 47296 KB Output is correct
14 Correct 27 ms 47272 KB Output is correct
15 Correct 24 ms 47284 KB Output is correct
16 Correct 27 ms 47228 KB Output is correct
17 Correct 25 ms 47256 KB Output is correct
18 Correct 25 ms 47268 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 25 ms 47292 KB Output is correct
21 Correct 24 ms 47344 KB Output is correct
22 Correct 25 ms 47296 KB Output is correct
23 Correct 24 ms 47336 KB Output is correct
24 Correct 25 ms 47240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 47188 KB Output is correct
2 Correct 25 ms 47304 KB Output is correct
3 Correct 27 ms 47296 KB Output is correct
4 Correct 25 ms 47316 KB Output is correct
5 Correct 26 ms 47304 KB Output is correct
6 Correct 25 ms 47252 KB Output is correct
7 Correct 25 ms 47252 KB Output is correct
8 Correct 24 ms 47344 KB Output is correct
9 Correct 28 ms 47236 KB Output is correct
10 Correct 24 ms 47268 KB Output is correct
11 Correct 25 ms 47312 KB Output is correct
12 Correct 25 ms 47340 KB Output is correct
13 Correct 27 ms 47296 KB Output is correct
14 Correct 27 ms 47272 KB Output is correct
15 Correct 24 ms 47284 KB Output is correct
16 Correct 27 ms 47228 KB Output is correct
17 Correct 25 ms 47256 KB Output is correct
18 Correct 25 ms 47268 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 25 ms 47292 KB Output is correct
21 Correct 24 ms 47344 KB Output is correct
22 Correct 25 ms 47296 KB Output is correct
23 Correct 24 ms 47336 KB Output is correct
24 Correct 25 ms 47240 KB Output is correct
25 Correct 29 ms 47348 KB Output is correct
26 Correct 27 ms 48032 KB Output is correct
27 Correct 26 ms 47816 KB Output is correct
28 Correct 30 ms 48196 KB Output is correct
29 Correct 29 ms 48180 KB Output is correct
30 Correct 27 ms 47828 KB Output is correct
31 Correct 24 ms 47312 KB Output is correct
32 Correct 27 ms 48084 KB Output is correct
33 Correct 27 ms 47288 KB Output is correct
34 Correct 26 ms 47928 KB Output is correct
35 Correct 27 ms 48212 KB Output is correct
36 Correct 29 ms 47780 KB Output is correct
37 Correct 28 ms 47956 KB Output is correct
38 Correct 26 ms 47356 KB Output is correct
39 Correct 27 ms 47884 KB Output is correct
40 Correct 27 ms 47828 KB Output is correct
41 Correct 27 ms 47784 KB Output is correct
42 Correct 28 ms 47876 KB Output is correct
43 Correct 27 ms 48044 KB Output is correct
44 Correct 24 ms 47316 KB Output is correct
45 Correct 27 ms 47964 KB Output is correct
46 Correct 28 ms 47884 KB Output is correct
47 Correct 26 ms 47252 KB Output is correct
48 Correct 27 ms 47904 KB Output is correct
49 Correct 28 ms 48212 KB Output is correct
50 Correct 31 ms 48204 KB Output is correct
51 Correct 27 ms 47920 KB Output is correct
52 Correct 29 ms 47944 KB Output is correct
53 Correct 25 ms 47940 KB Output is correct
54 Correct 28 ms 47864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 47188 KB Output is correct
2 Correct 25 ms 47304 KB Output is correct
3 Correct 27 ms 47296 KB Output is correct
4 Correct 25 ms 47316 KB Output is correct
5 Correct 26 ms 47304 KB Output is correct
6 Correct 25 ms 47252 KB Output is correct
7 Correct 25 ms 47252 KB Output is correct
8 Correct 24 ms 47344 KB Output is correct
9 Correct 28 ms 47236 KB Output is correct
10 Correct 24 ms 47268 KB Output is correct
11 Correct 25 ms 47312 KB Output is correct
12 Correct 25 ms 47340 KB Output is correct
13 Correct 27 ms 47296 KB Output is correct
14 Correct 27 ms 47272 KB Output is correct
15 Correct 24 ms 47284 KB Output is correct
16 Correct 27 ms 47228 KB Output is correct
17 Correct 25 ms 47256 KB Output is correct
18 Correct 25 ms 47268 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 25 ms 47292 KB Output is correct
21 Correct 24 ms 47344 KB Output is correct
22 Correct 25 ms 47296 KB Output is correct
23 Correct 24 ms 47336 KB Output is correct
24 Correct 25 ms 47240 KB Output is correct
25 Correct 26 ms 47252 KB Output is correct
26 Correct 147 ms 68712 KB Output is correct
27 Correct 146 ms 68004 KB Output is correct
28 Correct 33 ms 47820 KB Output is correct
29 Correct 22 ms 47316 KB Output is correct
30 Correct 23 ms 47316 KB Output is correct
31 Correct 131 ms 67924 KB Output is correct
32 Correct 28 ms 47820 KB Output is correct
33 Correct 134 ms 73648 KB Output is correct
34 Correct 127 ms 68032 KB Output is correct
35 Correct 27 ms 47868 KB Output is correct
36 Correct 123 ms 68548 KB Output is correct
37 Correct 26 ms 47828 KB Output is correct
38 Correct 29 ms 47804 KB Output is correct
39 Correct 110 ms 68736 KB Output is correct
40 Correct 28 ms 47988 KB Output is correct
41 Correct 129 ms 68240 KB Output is correct
42 Correct 153 ms 69836 KB Output is correct
43 Correct 23 ms 47316 KB Output is correct
44 Correct 149 ms 74376 KB Output is correct
45 Correct 150 ms 71012 KB Output is correct
46 Correct 30 ms 47828 KB Output is correct
47 Correct 28 ms 47812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 68724 KB Output is correct
2 Correct 169 ms 80596 KB Output is correct
3 Correct 32 ms 48056 KB Output is correct
4 Correct 28 ms 47792 KB Output is correct
5 Correct 25 ms 47368 KB Output is correct
6 Correct 22 ms 47308 KB Output is correct
7 Correct 30 ms 47796 KB Output is correct
8 Correct 156 ms 70384 KB Output is correct
9 Correct 31 ms 47836 KB Output is correct
10 Correct 154 ms 68144 KB Output is correct
11 Correct 30 ms 47300 KB Output is correct
12 Correct 166 ms 68136 KB Output is correct
13 Correct 157 ms 71180 KB Output is correct
14 Correct 153 ms 80312 KB Output is correct
15 Correct 131 ms 68812 KB Output is correct
16 Correct 27 ms 47944 KB Output is correct
17 Correct 26 ms 47336 KB Output is correct
18 Correct 159 ms 78516 KB Output is correct
19 Correct 151 ms 83416 KB Output is correct
20 Correct 26 ms 47956 KB Output is correct
21 Correct 24 ms 47348 KB Output is correct
22 Correct 130 ms 72220 KB Output is correct
23 Correct 28 ms 47888 KB Output is correct
24 Correct 164 ms 69296 KB Output is correct
25 Correct 206 ms 82348 KB Output is correct
26 Correct 35 ms 48200 KB Output is correct
27 Correct 29 ms 48212 KB Output is correct
28 Correct 28 ms 47912 KB Output is correct
29 Correct 26 ms 47904 KB Output is correct
30 Correct 27 ms 47956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 47188 KB Output is correct
2 Correct 25 ms 47304 KB Output is correct
3 Correct 27 ms 47296 KB Output is correct
4 Correct 25 ms 47316 KB Output is correct
5 Correct 26 ms 47304 KB Output is correct
6 Correct 25 ms 47252 KB Output is correct
7 Correct 25 ms 47252 KB Output is correct
8 Correct 24 ms 47344 KB Output is correct
9 Correct 28 ms 47236 KB Output is correct
10 Correct 24 ms 47268 KB Output is correct
11 Correct 25 ms 47312 KB Output is correct
12 Correct 25 ms 47340 KB Output is correct
13 Correct 27 ms 47296 KB Output is correct
14 Correct 27 ms 47272 KB Output is correct
15 Correct 24 ms 47284 KB Output is correct
16 Correct 27 ms 47228 KB Output is correct
17 Correct 25 ms 47256 KB Output is correct
18 Correct 25 ms 47268 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 25 ms 47292 KB Output is correct
21 Correct 24 ms 47344 KB Output is correct
22 Correct 25 ms 47296 KB Output is correct
23 Correct 24 ms 47336 KB Output is correct
24 Correct 25 ms 47240 KB Output is correct
25 Correct 29 ms 47348 KB Output is correct
26 Correct 27 ms 48032 KB Output is correct
27 Correct 26 ms 47816 KB Output is correct
28 Correct 30 ms 48196 KB Output is correct
29 Correct 29 ms 48180 KB Output is correct
30 Correct 27 ms 47828 KB Output is correct
31 Correct 24 ms 47312 KB Output is correct
32 Correct 27 ms 48084 KB Output is correct
33 Correct 27 ms 47288 KB Output is correct
34 Correct 26 ms 47928 KB Output is correct
35 Correct 27 ms 48212 KB Output is correct
36 Correct 29 ms 47780 KB Output is correct
37 Correct 28 ms 47956 KB Output is correct
38 Correct 26 ms 47356 KB Output is correct
39 Correct 27 ms 47884 KB Output is correct
40 Correct 27 ms 47828 KB Output is correct
41 Correct 27 ms 47784 KB Output is correct
42 Correct 28 ms 47876 KB Output is correct
43 Correct 27 ms 48044 KB Output is correct
44 Correct 24 ms 47316 KB Output is correct
45 Correct 27 ms 47964 KB Output is correct
46 Correct 28 ms 47884 KB Output is correct
47 Correct 26 ms 47252 KB Output is correct
48 Correct 27 ms 47904 KB Output is correct
49 Correct 28 ms 48212 KB Output is correct
50 Correct 31 ms 48204 KB Output is correct
51 Correct 27 ms 47920 KB Output is correct
52 Correct 29 ms 47944 KB Output is correct
53 Correct 25 ms 47940 KB Output is correct
54 Correct 28 ms 47864 KB Output is correct
55 Correct 26 ms 47252 KB Output is correct
56 Correct 147 ms 68712 KB Output is correct
57 Correct 146 ms 68004 KB Output is correct
58 Correct 33 ms 47820 KB Output is correct
59 Correct 22 ms 47316 KB Output is correct
60 Correct 23 ms 47316 KB Output is correct
61 Correct 131 ms 67924 KB Output is correct
62 Correct 28 ms 47820 KB Output is correct
63 Correct 134 ms 73648 KB Output is correct
64 Correct 127 ms 68032 KB Output is correct
65 Correct 27 ms 47868 KB Output is correct
66 Correct 123 ms 68548 KB Output is correct
67 Correct 26 ms 47828 KB Output is correct
68 Correct 29 ms 47804 KB Output is correct
69 Correct 110 ms 68736 KB Output is correct
70 Correct 28 ms 47988 KB Output is correct
71 Correct 129 ms 68240 KB Output is correct
72 Correct 153 ms 69836 KB Output is correct
73 Correct 23 ms 47316 KB Output is correct
74 Correct 149 ms 74376 KB Output is correct
75 Correct 150 ms 71012 KB Output is correct
76 Correct 30 ms 47828 KB Output is correct
77 Correct 28 ms 47812 KB Output is correct
78 Correct 120 ms 68724 KB Output is correct
79 Correct 169 ms 80596 KB Output is correct
80 Correct 32 ms 48056 KB Output is correct
81 Correct 28 ms 47792 KB Output is correct
82 Correct 25 ms 47368 KB Output is correct
83 Correct 22 ms 47308 KB Output is correct
84 Correct 30 ms 47796 KB Output is correct
85 Correct 156 ms 70384 KB Output is correct
86 Correct 31 ms 47836 KB Output is correct
87 Correct 154 ms 68144 KB Output is correct
88 Correct 30 ms 47300 KB Output is correct
89 Correct 166 ms 68136 KB Output is correct
90 Correct 157 ms 71180 KB Output is correct
91 Correct 153 ms 80312 KB Output is correct
92 Correct 131 ms 68812 KB Output is correct
93 Correct 27 ms 47944 KB Output is correct
94 Correct 26 ms 47336 KB Output is correct
95 Correct 159 ms 78516 KB Output is correct
96 Correct 151 ms 83416 KB Output is correct
97 Correct 26 ms 47956 KB Output is correct
98 Correct 24 ms 47348 KB Output is correct
99 Correct 130 ms 72220 KB Output is correct
100 Correct 28 ms 47888 KB Output is correct
101 Correct 164 ms 69296 KB Output is correct
102 Correct 206 ms 82348 KB Output is correct
103 Correct 35 ms 48200 KB Output is correct
104 Correct 29 ms 48212 KB Output is correct
105 Correct 28 ms 47912 KB Output is correct
106 Correct 26 ms 47904 KB Output is correct
107 Correct 27 ms 47956 KB Output is correct
108 Correct 731 ms 173968 KB Output is correct
109 Correct 917 ms 175132 KB Output is correct
110 Correct 923 ms 184416 KB Output is correct
111 Correct 1125 ms 236724 KB Output is correct
112 Correct 1139 ms 210644 KB Output is correct
113 Correct 995 ms 213424 KB Output is correct
114 Correct 770 ms 160440 KB Output is correct
115 Correct 844 ms 161156 KB Output is correct
116 Correct 933 ms 163932 KB Output is correct
117 Correct 1012 ms 221976 KB Output is correct
118 Correct 764 ms 158880 KB Output is correct
119 Correct 983 ms 222044 KB Output is correct
120 Correct 1274 ms 233104 KB Output is correct
121 Correct 966 ms 221824 KB Output is correct
122 Correct 998 ms 176964 KB Output is correct
123 Correct 1095 ms 223612 KB Output is correct
124 Correct 1010 ms 202260 KB Output is correct