답안 #620039

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
620039 2022-08-02T19:53:56 Z AbdelmagedNour Mergers (JOI19_mergers) C++17
100 / 100
1082 ms 204024 KB
#include<bits/stdc++.h>
using namespace std;
const int N=500005;
long long p[N][20],val[N],dep[N],st[N],id;
vector<vector<int>>adj;
void pre_dfs(int v,int par){
    p[v][0]=par;
    st[v]=id++;
    for(int i=1;i<20;i++)p[v][i]=p[p[v][i-1]][i-1];
    for(auto&u:adj[v]){
        if(u==par)continue;
        dep[u]=dep[v]+1;
        pre_dfs(u,v);
    }
}
int P[N],sz[N];
int f(int x){
    return (P[x]==x?x:P[x]=f(P[x]));
}
void join(int a, int b){
	a=f(a),b=f(b);
	if(a==b)return;
	if(sz[a]<sz[b])swap(a, b);
	P[b]=a;
	sz[a]+=sz[b];
}
void dfs_up(int v,int par){
    for(auto&u:adj[v]){
        if(u==par)continue;
        dfs_up(u,v);
        val[v]+=val[u];
    }
    if(val[v])join(v,par);
}
int LCA(int u,int v){
    if(u==v)return u;
    if(dep[u]<dep[v])swap(u,v);
    int k=dep[u]-dep[v];
    for(int i=__lg(k+1);i>=0;i--){
        if(k&(1<<i))u=p[u][i];
    }
    if(u==v)return u;
    for(int i=__lg(dep[u]+1);i>=0;i--){
        if(p[u][i]==p[v][i])continue;
        u=p[u][i];
        v=p[v][i];
    }
    return p[u][0];
}
int cnt(int v,int par){
    int res=0;
    for(auto&u:adj[v]){
        if(u==par)continue;
        res+=cnt(u,v);
    }
    return max(res,(val[v]==0?1:0));
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n>>k;
    adj.resize(n+1);
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    pre_dfs(1,0);
    vector<vector<int>>state(k+1);
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        state[x].push_back(i);
    }
    for(int i=1;i<=k;i++){
        sort(state[i].begin(),state[i].end(),[](int i,int j){
                return st[i]<st[j];
             });
        for(int j=1;j<state[i].size();j++){
            int u=state[i][j-1],v=state[i][j];
            val[u]++;val[v]++;
            val[LCA(u,v)]-=2;
        }
    }
    iota(P,P+n+1,0);
    fill(sz,sz+n+1,1);
    dfs_up(1,0);
    vector<vector<int>>adj2(n+1);
    for(int i=1;i<=n;i++){
        for(auto&u:adj[i]){
            if(f(i)!=f(u))adj2[f(i)].push_back(f(u));
        }
    }
    int res=0;
    for(int i=1;i<=n;i++)res+=(adj2[i].size()==1);
    cout<<(res+1)/2;
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for(int j=1;j<state[i].size();j++){
      |                     ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 1304 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 3 ms 1500 KB Output is correct
29 Correct 3 ms 1396 KB Output is correct
30 Correct 2 ms 1108 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 3 ms 1504 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 2 ms 1236 KB Output is correct
35 Correct 3 ms 1364 KB Output is correct
36 Correct 2 ms 1120 KB Output is correct
37 Correct 3 ms 1236 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 2 ms 1236 KB Output is correct
40 Correct 2 ms 1128 KB Output is correct
41 Correct 2 ms 1236 KB Output is correct
42 Correct 2 ms 1252 KB Output is correct
43 Correct 3 ms 1376 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 3 ms 1308 KB Output is correct
46 Correct 2 ms 1236 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 1236 KB Output is correct
49 Correct 2 ms 1380 KB Output is correct
50 Correct 3 ms 1504 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1236 KB Output is correct
53 Correct 2 ms 1236 KB Output is correct
54 Correct 2 ms 1124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 88 ms 29352 KB Output is correct
27 Correct 119 ms 29016 KB Output is correct
28 Correct 2 ms 1236 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 103 ms 28936 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 140 ms 34804 KB Output is correct
34 Correct 119 ms 29148 KB Output is correct
35 Correct 3 ms 1108 KB Output is correct
36 Correct 121 ms 29504 KB Output is correct
37 Correct 2 ms 1236 KB Output is correct
38 Correct 3 ms 1236 KB Output is correct
39 Correct 80 ms 29252 KB Output is correct
40 Correct 3 ms 1364 KB Output is correct
41 Correct 104 ms 28880 KB Output is correct
42 Correct 132 ms 30796 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 126 ms 34872 KB Output is correct
45 Correct 138 ms 31944 KB Output is correct
46 Correct 2 ms 1236 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 28484 KB Output is correct
2 Correct 81 ms 36008 KB Output is correct
3 Correct 3 ms 1364 KB Output is correct
4 Correct 3 ms 1236 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 2 ms 1236 KB Output is correct
8 Correct 124 ms 31908 KB Output is correct
9 Correct 2 ms 1132 KB Output is correct
10 Correct 109 ms 29308 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 111 ms 29132 KB Output is correct
13 Correct 116 ms 32096 KB Output is correct
14 Correct 107 ms 37452 KB Output is correct
15 Correct 77 ms 29252 KB Output is correct
16 Correct 2 ms 1236 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 88 ms 36860 KB Output is correct
19 Correct 136 ms 40508 KB Output is correct
20 Correct 3 ms 1260 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 84 ms 32416 KB Output is correct
23 Correct 2 ms 1236 KB Output is correct
24 Correct 119 ms 29956 KB Output is correct
25 Correct 161 ms 39452 KB Output is correct
26 Correct 2 ms 1364 KB Output is correct
27 Correct 3 ms 1492 KB Output is correct
28 Correct 4 ms 1236 KB Output is correct
29 Correct 2 ms 1252 KB Output is correct
30 Correct 4 ms 1252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 1304 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
28 Correct 3 ms 1500 KB Output is correct
29 Correct 3 ms 1396 KB Output is correct
30 Correct 2 ms 1108 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 3 ms 1504 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 2 ms 1236 KB Output is correct
35 Correct 3 ms 1364 KB Output is correct
36 Correct 2 ms 1120 KB Output is correct
37 Correct 3 ms 1236 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 2 ms 1236 KB Output is correct
40 Correct 2 ms 1128 KB Output is correct
41 Correct 2 ms 1236 KB Output is correct
42 Correct 2 ms 1252 KB Output is correct
43 Correct 3 ms 1376 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 3 ms 1308 KB Output is correct
46 Correct 2 ms 1236 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 1236 KB Output is correct
49 Correct 2 ms 1380 KB Output is correct
50 Correct 3 ms 1504 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1236 KB Output is correct
53 Correct 2 ms 1236 KB Output is correct
54 Correct 2 ms 1124 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 88 ms 29352 KB Output is correct
57 Correct 119 ms 29016 KB Output is correct
58 Correct 2 ms 1236 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 103 ms 28936 KB Output is correct
62 Correct 3 ms 1364 KB Output is correct
63 Correct 140 ms 34804 KB Output is correct
64 Correct 119 ms 29148 KB Output is correct
65 Correct 3 ms 1108 KB Output is correct
66 Correct 121 ms 29504 KB Output is correct
67 Correct 2 ms 1236 KB Output is correct
68 Correct 3 ms 1236 KB Output is correct
69 Correct 80 ms 29252 KB Output is correct
70 Correct 3 ms 1364 KB Output is correct
71 Correct 104 ms 28880 KB Output is correct
72 Correct 132 ms 30796 KB Output is correct
73 Correct 1 ms 340 KB Output is correct
74 Correct 126 ms 34872 KB Output is correct
75 Correct 138 ms 31944 KB Output is correct
76 Correct 2 ms 1236 KB Output is correct
77 Correct 2 ms 1236 KB Output is correct
78 Correct 82 ms 28484 KB Output is correct
79 Correct 81 ms 36008 KB Output is correct
80 Correct 3 ms 1364 KB Output is correct
81 Correct 3 ms 1236 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 0 ms 336 KB Output is correct
84 Correct 2 ms 1236 KB Output is correct
85 Correct 124 ms 31908 KB Output is correct
86 Correct 2 ms 1132 KB Output is correct
87 Correct 109 ms 29308 KB Output is correct
88 Correct 0 ms 340 KB Output is correct
89 Correct 111 ms 29132 KB Output is correct
90 Correct 116 ms 32096 KB Output is correct
91 Correct 107 ms 37452 KB Output is correct
92 Correct 77 ms 29252 KB Output is correct
93 Correct 2 ms 1236 KB Output is correct
94 Correct 1 ms 336 KB Output is correct
95 Correct 88 ms 36860 KB Output is correct
96 Correct 136 ms 40508 KB Output is correct
97 Correct 3 ms 1260 KB Output is correct
98 Correct 1 ms 340 KB Output is correct
99 Correct 84 ms 32416 KB Output is correct
100 Correct 2 ms 1236 KB Output is correct
101 Correct 119 ms 29956 KB Output is correct
102 Correct 161 ms 39452 KB Output is correct
103 Correct 2 ms 1364 KB Output is correct
104 Correct 3 ms 1492 KB Output is correct
105 Correct 4 ms 1236 KB Output is correct
106 Correct 2 ms 1252 KB Output is correct
107 Correct 4 ms 1252 KB Output is correct
108 Correct 708 ms 158060 KB Output is correct
109 Correct 1070 ms 157844 KB Output is correct
110 Correct 1010 ms 166828 KB Output is correct
111 Correct 1033 ms 204024 KB Output is correct
112 Correct 1082 ms 191004 KB Output is correct
113 Correct 693 ms 184240 KB Output is correct
114 Correct 856 ms 143620 KB Output is correct
115 Correct 839 ms 143620 KB Output is correct
116 Correct 944 ms 150676 KB Output is correct
117 Correct 796 ms 187452 KB Output is correct
118 Correct 763 ms 142708 KB Output is correct
119 Correct 796 ms 187464 KB Output is correct
120 Correct 977 ms 197920 KB Output is correct
121 Correct 837 ms 187448 KB Output is correct
122 Correct 1040 ms 163204 KB Output is correct
123 Correct 619 ms 186464 KB Output is correct
124 Correct 990 ms 177664 KB Output is correct