답안 #786703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
786703 2023-07-18T11:29:35 Z winter0101 Mergers (JOI19_mergers) C++14
100 / 100
849 ms 114084 KB
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=5e5+9;
int st[maxn][20];
int dep[maxn];
vector<int>a[maxn];
int s[maxn];
int last[maxn];
int anc[maxn];
int in[maxn];
int ans=0,tme=0;
void dfs(int u,int par){
in[u]=++tme;
for (auto v:a[u]){
    if (v==par)continue;
    dep[v]=dep[u]+1;
    st[v][0]=u;
    for1(i,1,19)st[v][i]=st[st[v][i-1]][i-1];
    dfs(v,u);
}
}
int lca(int u,int v){
if (u==0)return v;
if (v==0)return u;
if (dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for1(i,0,19){
if (k>>i&1)u=st[u][i];
}
if(u==v)return u;
for2(i,19,0){
if (!st[u][i]||!st[v][i])continue;
if (st[u][i]!=st[v][i]){
    u=st[u][i];
    v=st[v][i];
}
}
return st[u][0];
}
int f[maxn];
int findset(int u){
if (f[u]<0)return u;
return f[u]=findset(f[u]);
}
void unite(int u,int v){
u=findset(u),v=findset(v);
if (u==v)return ;
if (f[u]>f[v])swap(u,v);
f[u]+=f[v];
f[v]=u;
}
void redfs(int u,int par){
anc[u]=last[s[u]];
for (auto v:a[u]){
    if (v==par)continue;
    redfs(v,u);
    anc[u]=lca(anc[u],anc[v]);
}
if (anc[u]==u&&u!=1){
    return;
}
else {
    //cerr<<u<<" "<<par<<'\n';
    if (par!=0)unite(u,par);
}
}
int deg[maxn];
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("temp.INP","r",stdin);
    //freopen("temp.OUT","w",stdout);
    int n,k;
    cin>>n>>k;
    for1(i,1,n)f[i]=-1;
    for1(i,1,n-1){
    int u,v;
    cin>>u>>v;
    a[u].pb(v);
    a[v].pb(u);
    }
    dfs(1,0);
    for1(i,1,n){
    cin>>s[i];
    last[s[i]]=lca(last[s[i]],i);
    }
    redfs(1,0);
    for1(i,2,n){
    if (anc[i]==i){
        deg[findset(i)]++;
        deg[findset(st[i][0])]++;
    }
    }
    for1(i,1,n){
    if (f[i]<0&&deg[i]==1)ans++;
    }
    cout<<(ans+1)/2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12264 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 12116 KB Output is correct
5 Correct 5 ms 12116 KB Output is correct
6 Correct 5 ms 12116 KB Output is correct
7 Correct 5 ms 12116 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 5 ms 12116 KB Output is correct
12 Correct 5 ms 12116 KB Output is correct
13 Correct 5 ms 12116 KB Output is correct
14 Correct 5 ms 12116 KB Output is correct
15 Correct 5 ms 12116 KB Output is correct
16 Correct 5 ms 12116 KB Output is correct
17 Correct 5 ms 12116 KB Output is correct
18 Correct 5 ms 12116 KB Output is correct
19 Correct 5 ms 12116 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12116 KB Output is correct
22 Correct 5 ms 12116 KB Output is correct
23 Correct 5 ms 12116 KB Output is correct
24 Correct 6 ms 12116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12264 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 12116 KB Output is correct
5 Correct 5 ms 12116 KB Output is correct
6 Correct 5 ms 12116 KB Output is correct
7 Correct 5 ms 12116 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 5 ms 12116 KB Output is correct
12 Correct 5 ms 12116 KB Output is correct
13 Correct 5 ms 12116 KB Output is correct
14 Correct 5 ms 12116 KB Output is correct
15 Correct 5 ms 12116 KB Output is correct
16 Correct 5 ms 12116 KB Output is correct
17 Correct 5 ms 12116 KB Output is correct
18 Correct 5 ms 12116 KB Output is correct
19 Correct 5 ms 12116 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12116 KB Output is correct
22 Correct 5 ms 12116 KB Output is correct
23 Correct 5 ms 12116 KB Output is correct
24 Correct 6 ms 12116 KB Output is correct
25 Correct 5 ms 12116 KB Output is correct
26 Correct 7 ms 12424 KB Output is correct
27 Correct 7 ms 12500 KB Output is correct
28 Correct 7 ms 12628 KB Output is correct
29 Correct 6 ms 12500 KB Output is correct
30 Correct 7 ms 12444 KB Output is correct
31 Correct 5 ms 12116 KB Output is correct
32 Correct 7 ms 12628 KB Output is correct
33 Correct 6 ms 12116 KB Output is correct
34 Correct 6 ms 12500 KB Output is correct
35 Correct 7 ms 12500 KB Output is correct
36 Correct 7 ms 12440 KB Output is correct
37 Correct 7 ms 12476 KB Output is correct
38 Correct 6 ms 12076 KB Output is correct
39 Correct 7 ms 12564 KB Output is correct
40 Correct 7 ms 12472 KB Output is correct
41 Correct 7 ms 12472 KB Output is correct
42 Correct 7 ms 12500 KB Output is correct
43 Correct 7 ms 12748 KB Output is correct
44 Correct 5 ms 12116 KB Output is correct
45 Correct 7 ms 12500 KB Output is correct
46 Correct 7 ms 12480 KB Output is correct
47 Correct 6 ms 12040 KB Output is correct
48 Correct 8 ms 12476 KB Output is correct
49 Correct 7 ms 12500 KB Output is correct
50 Correct 7 ms 12608 KB Output is correct
51 Correct 9 ms 12500 KB Output is correct
52 Correct 7 ms 12500 KB Output is correct
53 Correct 6 ms 12500 KB Output is correct
54 Correct 7 ms 12480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12264 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 12116 KB Output is correct
5 Correct 5 ms 12116 KB Output is correct
6 Correct 5 ms 12116 KB Output is correct
7 Correct 5 ms 12116 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 5 ms 12116 KB Output is correct
12 Correct 5 ms 12116 KB Output is correct
13 Correct 5 ms 12116 KB Output is correct
14 Correct 5 ms 12116 KB Output is correct
15 Correct 5 ms 12116 KB Output is correct
16 Correct 5 ms 12116 KB Output is correct
17 Correct 5 ms 12116 KB Output is correct
18 Correct 5 ms 12116 KB Output is correct
19 Correct 5 ms 12116 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12116 KB Output is correct
22 Correct 5 ms 12116 KB Output is correct
23 Correct 5 ms 12116 KB Output is correct
24 Correct 6 ms 12116 KB Output is correct
25 Correct 6 ms 12128 KB Output is correct
26 Correct 50 ms 25356 KB Output is correct
27 Correct 76 ms 25100 KB Output is correct
28 Correct 8 ms 12544 KB Output is correct
29 Correct 5 ms 12116 KB Output is correct
30 Correct 6 ms 12116 KB Output is correct
31 Correct 68 ms 25240 KB Output is correct
32 Correct 7 ms 12500 KB Output is correct
33 Correct 93 ms 32204 KB Output is correct
34 Correct 67 ms 25036 KB Output is correct
35 Correct 7 ms 12500 KB Output is correct
36 Correct 77 ms 27340 KB Output is correct
37 Correct 7 ms 12472 KB Output is correct
38 Correct 7 ms 12472 KB Output is correct
39 Correct 51 ms 26708 KB Output is correct
40 Correct 7 ms 12756 KB Output is correct
41 Correct 68 ms 26444 KB Output is correct
42 Correct 90 ms 28860 KB Output is correct
43 Correct 6 ms 12016 KB Output is correct
44 Correct 91 ms 33892 KB Output is correct
45 Correct 87 ms 30156 KB Output is correct
46 Correct 7 ms 12556 KB Output is correct
47 Correct 7 ms 12500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 25412 KB Output is correct
2 Correct 54 ms 26136 KB Output is correct
3 Correct 7 ms 12500 KB Output is correct
4 Correct 7 ms 12500 KB Output is correct
5 Correct 5 ms 12016 KB Output is correct
6 Correct 5 ms 12116 KB Output is correct
7 Correct 7 ms 12456 KB Output is correct
8 Correct 82 ms 25616 KB Output is correct
9 Correct 7 ms 12520 KB Output is correct
10 Correct 75 ms 27004 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 76 ms 26952 KB Output is correct
13 Correct 73 ms 27312 KB Output is correct
14 Correct 68 ms 27640 KB Output is correct
15 Correct 51 ms 26692 KB Output is correct
16 Correct 7 ms 12500 KB Output is correct
17 Correct 6 ms 12116 KB Output is correct
18 Correct 59 ms 27820 KB Output is correct
19 Correct 93 ms 31436 KB Output is correct
20 Correct 7 ms 12472 KB Output is correct
21 Correct 5 ms 12076 KB Output is correct
22 Correct 56 ms 27644 KB Output is correct
23 Correct 6 ms 12500 KB Output is correct
24 Correct 78 ms 27032 KB Output is correct
25 Correct 101 ms 30128 KB Output is correct
26 Correct 7 ms 12500 KB Output is correct
27 Correct 7 ms 12628 KB Output is correct
28 Correct 6 ms 12556 KB Output is correct
29 Correct 7 ms 12500 KB Output is correct
30 Correct 6 ms 12500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12264 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 12116 KB Output is correct
5 Correct 5 ms 12116 KB Output is correct
6 Correct 5 ms 12116 KB Output is correct
7 Correct 5 ms 12116 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 5 ms 12116 KB Output is correct
12 Correct 5 ms 12116 KB Output is correct
13 Correct 5 ms 12116 KB Output is correct
14 Correct 5 ms 12116 KB Output is correct
15 Correct 5 ms 12116 KB Output is correct
16 Correct 5 ms 12116 KB Output is correct
17 Correct 5 ms 12116 KB Output is correct
18 Correct 5 ms 12116 KB Output is correct
19 Correct 5 ms 12116 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12116 KB Output is correct
22 Correct 5 ms 12116 KB Output is correct
23 Correct 5 ms 12116 KB Output is correct
24 Correct 6 ms 12116 KB Output is correct
25 Correct 5 ms 12116 KB Output is correct
26 Correct 7 ms 12424 KB Output is correct
27 Correct 7 ms 12500 KB Output is correct
28 Correct 7 ms 12628 KB Output is correct
29 Correct 6 ms 12500 KB Output is correct
30 Correct 7 ms 12444 KB Output is correct
31 Correct 5 ms 12116 KB Output is correct
32 Correct 7 ms 12628 KB Output is correct
33 Correct 6 ms 12116 KB Output is correct
34 Correct 6 ms 12500 KB Output is correct
35 Correct 7 ms 12500 KB Output is correct
36 Correct 7 ms 12440 KB Output is correct
37 Correct 7 ms 12476 KB Output is correct
38 Correct 6 ms 12076 KB Output is correct
39 Correct 7 ms 12564 KB Output is correct
40 Correct 7 ms 12472 KB Output is correct
41 Correct 7 ms 12472 KB Output is correct
42 Correct 7 ms 12500 KB Output is correct
43 Correct 7 ms 12748 KB Output is correct
44 Correct 5 ms 12116 KB Output is correct
45 Correct 7 ms 12500 KB Output is correct
46 Correct 7 ms 12480 KB Output is correct
47 Correct 6 ms 12040 KB Output is correct
48 Correct 8 ms 12476 KB Output is correct
49 Correct 7 ms 12500 KB Output is correct
50 Correct 7 ms 12608 KB Output is correct
51 Correct 9 ms 12500 KB Output is correct
52 Correct 7 ms 12500 KB Output is correct
53 Correct 6 ms 12500 KB Output is correct
54 Correct 7 ms 12480 KB Output is correct
55 Correct 6 ms 12128 KB Output is correct
56 Correct 50 ms 25356 KB Output is correct
57 Correct 76 ms 25100 KB Output is correct
58 Correct 8 ms 12544 KB Output is correct
59 Correct 5 ms 12116 KB Output is correct
60 Correct 6 ms 12116 KB Output is correct
61 Correct 68 ms 25240 KB Output is correct
62 Correct 7 ms 12500 KB Output is correct
63 Correct 93 ms 32204 KB Output is correct
64 Correct 67 ms 25036 KB Output is correct
65 Correct 7 ms 12500 KB Output is correct
66 Correct 77 ms 27340 KB Output is correct
67 Correct 7 ms 12472 KB Output is correct
68 Correct 7 ms 12472 KB Output is correct
69 Correct 51 ms 26708 KB Output is correct
70 Correct 7 ms 12756 KB Output is correct
71 Correct 68 ms 26444 KB Output is correct
72 Correct 90 ms 28860 KB Output is correct
73 Correct 6 ms 12016 KB Output is correct
74 Correct 91 ms 33892 KB Output is correct
75 Correct 87 ms 30156 KB Output is correct
76 Correct 7 ms 12556 KB Output is correct
77 Correct 7 ms 12500 KB Output is correct
78 Correct 49 ms 25412 KB Output is correct
79 Correct 54 ms 26136 KB Output is correct
80 Correct 7 ms 12500 KB Output is correct
81 Correct 7 ms 12500 KB Output is correct
82 Correct 5 ms 12016 KB Output is correct
83 Correct 5 ms 12116 KB Output is correct
84 Correct 7 ms 12456 KB Output is correct
85 Correct 82 ms 25616 KB Output is correct
86 Correct 7 ms 12520 KB Output is correct
87 Correct 75 ms 27004 KB Output is correct
88 Correct 6 ms 12116 KB Output is correct
89 Correct 76 ms 26952 KB Output is correct
90 Correct 73 ms 27312 KB Output is correct
91 Correct 68 ms 27640 KB Output is correct
92 Correct 51 ms 26692 KB Output is correct
93 Correct 7 ms 12500 KB Output is correct
94 Correct 6 ms 12116 KB Output is correct
95 Correct 59 ms 27820 KB Output is correct
96 Correct 93 ms 31436 KB Output is correct
97 Correct 7 ms 12472 KB Output is correct
98 Correct 5 ms 12076 KB Output is correct
99 Correct 56 ms 27644 KB Output is correct
100 Correct 6 ms 12500 KB Output is correct
101 Correct 78 ms 27032 KB Output is correct
102 Correct 101 ms 30128 KB Output is correct
103 Correct 7 ms 12500 KB Output is correct
104 Correct 7 ms 12628 KB Output is correct
105 Correct 6 ms 12556 KB Output is correct
106 Correct 7 ms 12500 KB Output is correct
107 Correct 6 ms 12500 KB Output is correct
108 Correct 397 ms 91220 KB Output is correct
109 Correct 845 ms 102796 KB Output is correct
110 Correct 849 ms 113920 KB Output is correct
111 Correct 797 ms 114084 KB Output is correct
112 Correct 806 ms 104284 KB Output is correct
113 Correct 445 ms 92352 KB Output is correct
114 Correct 587 ms 84776 KB Output is correct
115 Correct 614 ms 84872 KB Output is correct
116 Correct 674 ms 88824 KB Output is correct
117 Correct 632 ms 91148 KB Output is correct
118 Correct 595 ms 85684 KB Output is correct
119 Correct 664 ms 90964 KB Output is correct
120 Correct 778 ms 105132 KB Output is correct
121 Correct 632 ms 91040 KB Output is correct
122 Correct 680 ms 90196 KB Output is correct
123 Correct 410 ms 92556 KB Output is correct
124 Correct 765 ms 89256 KB Output is correct