답안 #919356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
919356 2024-01-31T16:13:40 Z imarn Mergers (JOI19_mergers) C++14
70 / 100
3000 ms 108748 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define vp vector<pii>
using namespace std;
const int N=5e5+5;
vector<int>g[N];
set<int>gg[N];
int sz[N],id[N],pr[N],head[N],dep[N],cur=0,t[4*N],lz[4*N],pos[N],cp=1,n;
void push(int i,int l,int r){
    if(lz[i]==1e9)return;
    t[i]=min(t[i],lz[i]);
    if(l<r){
        lz[2*i]=min(lz[2*i],lz[i]);
        lz[2*i+1]=min(lz[2*i+1],lz[i]);
    }lz[i]=1e9;
}
void upd(int i,int l,int r,int tl,int tr,int id){
    push(i,l,r);
    if(r<tl||l>tr)return;
    if(r<=tr&&l>=tl){
        lz[i]=min(lz[i],id);push(i,l,r);
        return;
    }int m=(l+r)>>1;
    upd(2*i,l,m,tl,tr,id);upd(2*i+1,m+1,r,tl,tr,id);
    t[i]=min(t[2*i],t[2*i+1]);
}
int qr(int i,int l,int r,int tl,int tr){
    push(i,l,r);
    if(r<tl||l>tr)return 1e9;
    if(r<=tr&&l>=tl)return t[i];
    int m=(l+r)>>1;
    return min(qr(2*i,l,m,tl,tr),qr(2*i+1,m+1,r,tl,tr));
}
void update(int x,int y,int z){
    while(head[x]!=head[y]){
        if(dep[head[x]]<dep[head[y]])swap(x,y);
        upd(1,1,n,id[head[x]],id[x],z);
        x=pr[head[x]];
    }if(dep[x]>dep[y])swap(x,y);
    upd(1,1,n,id[x],id[y],z);
}
int query(int x,int y,int res=1e9){
    while(head[x]!=head[y]){
        if(dep[head[x]]<dep[head[y]])swap(x,y);
        res=min(qr(1,1,n,id[head[x]],id[x]),res);
        x=pr[head[x]];
    }if(dep[x]>dep[y])swap(x,y);
    res=min(res,qr(1,1,n,id[x],id[y]));return res;
}
void dfssz(int u,int p,int l){
    sz[u]=1;dep[u]=l;pr[u]=p;
    for(auto v:g[u]){
        if(v==p)continue;dfssz(v,u,l+1);
        sz[u]+=sz[v];
    }
}
void hld(int u,int p,int tp){
    head[u]=tp;id[u]=++cur;
    int hc=-1,hv=-1;
    for(auto v:g[u]){
        if(v==p)continue;
        if(sz[v]>hc)hv=v,hc=sz[v];
    }if(hv==-1)return;
    hld(hv,u,tp);
    for(auto v:g[u]){
        if(v==p||v==hv)continue;
        hld(v,u,v);
    }
}vector<int>gr[N];
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int k;cin>>n>>k;
    for(int i=0;i<4*N;i++)t[i]=lz[i]=1e9;
    for(int i=1;i<=n-1;i++){
        int u,v;cin>>u>>v;g[u].pb(v);g[v].pb(u);
    }dfssz(1,1,0);hld(1,1,1);bool ok=1;
    for(int i=1,x;i<=n;i++)cin>>x,gr[x].pb(i);
    for(int i=1;i<=k;i++){
        if(gr[i].empty())continue;
        int top=gr[i][0];int res=cp;
        for(int j=0;j<gr[i].size();j++)res=min(res,query(top,gr[i][j]));
        for(int j=0;j<gr[i].size();j++)update(top,gr[i][j],res);
        cp++;
    }
    for(int i=1;i<=k;i++){
        if(gr[i].empty())continue;
        int top=gr[i][0];int res=1e9;
        for(int j=0;j<gr[i].size();j++)res=min(res,query(top,gr[i][j]));
        for(int j=0;j<gr[i].size();j++)update(top,gr[i][j],res);
    }
    for(int i=1;i<=k;i++){
        if(gr[i].empty())continue;
        int top=gr[i][0];int res=1e9;
        for(int j=0;j<gr[i].size();j++)res=min(res,query(top,gr[i][j]));
        for(int j=0;j<gr[i].size();j++)update(top,gr[i][j],res);
    }
    for(int i=1;i<=n;i++)pos[i]=qr(1,1,n,id[i],id[i]);
    for(int i=1;i<=n;i++){
        for(auto v:g[i]){
            if(pos[v]==pos[i])continue;
            gg[pos[v]].insert(pos[i]);
            gg[pos[i]].insert(pos[v]);
        }
    }int ct=0;
    for(int i=1;i<=n;i++){
        if(gg[i].empty())continue;
        if(gg[i].size()==1)ct++;
    }cout<<(ct+1)/2;
}

Compilation message

mergers.cpp: In function 'void dfssz(int, int, int)':
mergers.cpp:61:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   61 |         if(v==p)continue;dfssz(v,u,l+1);
      |         ^~
mergers.cpp:61:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   61 |         if(v==p)continue;dfssz(v,u,l+1);
      |                          ^~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j=0;j<gr[i].size();j++)res=min(res,query(top,gr[i][j]));
      |                     ~^~~~~~~~~~~~~
mergers.cpp:90:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for(int j=0;j<gr[i].size();j++)update(top,gr[i][j],res);
      |                     ~^~~~~~~~~~~~~
mergers.cpp:96:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for(int j=0;j<gr[i].size();j++)res=min(res,query(top,gr[i][j]));
      |                     ~^~~~~~~~~~~~~
mergers.cpp:97:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |         for(int j=0;j<gr[i].size();j++)update(top,gr[i][j],res);
      |                     ~^~~~~~~~~~~~~
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 j=0;j<gr[i].size();j++)res=min(res,query(top,gr[i][j]));
      |                     ~^~~~~~~~~~~~~
mergers.cpp:103:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for(int j=0;j<gr[i].size();j++)update(top,gr[i][j],res);
      |                     ~^~~~~~~~~~~~~
mergers.cpp:84:35: warning: unused variable 'ok' [-Wunused-variable]
   84 |     }dfssz(1,1,0);hld(1,1,1);bool ok=1;
      |                                   ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74580 KB Output is correct
2 Correct 19 ms 74780 KB Output is correct
3 Correct 19 ms 74584 KB Output is correct
4 Correct 19 ms 74584 KB Output is correct
5 Correct 20 ms 74584 KB Output is correct
6 Correct 20 ms 74584 KB Output is correct
7 Correct 20 ms 74584 KB Output is correct
8 Correct 19 ms 74584 KB Output is correct
9 Correct 21 ms 74812 KB Output is correct
10 Correct 20 ms 74816 KB Output is correct
11 Correct 20 ms 74584 KB Output is correct
12 Correct 19 ms 74588 KB Output is correct
13 Correct 19 ms 74812 KB Output is correct
14 Correct 19 ms 74584 KB Output is correct
15 Correct 20 ms 74584 KB Output is correct
16 Correct 26 ms 74584 KB Output is correct
17 Correct 26 ms 74788 KB Output is correct
18 Correct 21 ms 74812 KB Output is correct
19 Correct 19 ms 74588 KB Output is correct
20 Correct 19 ms 74744 KB Output is correct
21 Correct 21 ms 74840 KB Output is correct
22 Correct 20 ms 74584 KB Output is correct
23 Correct 20 ms 74800 KB Output is correct
24 Correct 21 ms 74800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74580 KB Output is correct
2 Correct 19 ms 74780 KB Output is correct
3 Correct 19 ms 74584 KB Output is correct
4 Correct 19 ms 74584 KB Output is correct
5 Correct 20 ms 74584 KB Output is correct
6 Correct 20 ms 74584 KB Output is correct
7 Correct 20 ms 74584 KB Output is correct
8 Correct 19 ms 74584 KB Output is correct
9 Correct 21 ms 74812 KB Output is correct
10 Correct 20 ms 74816 KB Output is correct
11 Correct 20 ms 74584 KB Output is correct
12 Correct 19 ms 74588 KB Output is correct
13 Correct 19 ms 74812 KB Output is correct
14 Correct 19 ms 74584 KB Output is correct
15 Correct 20 ms 74584 KB Output is correct
16 Correct 26 ms 74584 KB Output is correct
17 Correct 26 ms 74788 KB Output is correct
18 Correct 21 ms 74812 KB Output is correct
19 Correct 19 ms 74588 KB Output is correct
20 Correct 19 ms 74744 KB Output is correct
21 Correct 21 ms 74840 KB Output is correct
22 Correct 20 ms 74584 KB Output is correct
23 Correct 20 ms 74800 KB Output is correct
24 Correct 21 ms 74800 KB Output is correct
25 Correct 19 ms 75016 KB Output is correct
26 Correct 25 ms 75096 KB Output is correct
27 Correct 26 ms 74840 KB Output is correct
28 Correct 27 ms 75096 KB Output is correct
29 Correct 25 ms 75096 KB Output is correct
30 Correct 37 ms 75092 KB Output is correct
31 Correct 19 ms 74584 KB Output is correct
32 Correct 26 ms 75172 KB Output is correct
33 Correct 19 ms 74584 KB Output is correct
34 Correct 29 ms 74844 KB Output is correct
35 Correct 24 ms 75244 KB Output is correct
36 Correct 40 ms 74920 KB Output is correct
37 Correct 31 ms 74840 KB Output is correct
38 Correct 20 ms 74784 KB Output is correct
39 Correct 34 ms 74964 KB Output is correct
40 Correct 32 ms 74840 KB Output is correct
41 Correct 36 ms 74936 KB Output is correct
42 Correct 29 ms 74844 KB Output is correct
43 Correct 24 ms 75060 KB Output is correct
44 Correct 21 ms 74592 KB Output is correct
45 Correct 24 ms 74840 KB Output is correct
46 Correct 32 ms 75100 KB Output is correct
47 Correct 19 ms 74584 KB Output is correct
48 Correct 26 ms 74812 KB Output is correct
49 Correct 25 ms 75276 KB Output is correct
50 Correct 24 ms 75096 KB Output is correct
51 Correct 26 ms 74844 KB Output is correct
52 Correct 34 ms 75096 KB Output is correct
53 Correct 29 ms 74840 KB Output is correct
54 Correct 43 ms 74840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74580 KB Output is correct
2 Correct 19 ms 74780 KB Output is correct
3 Correct 19 ms 74584 KB Output is correct
4 Correct 19 ms 74584 KB Output is correct
5 Correct 20 ms 74584 KB Output is correct
6 Correct 20 ms 74584 KB Output is correct
7 Correct 20 ms 74584 KB Output is correct
8 Correct 19 ms 74584 KB Output is correct
9 Correct 21 ms 74812 KB Output is correct
10 Correct 20 ms 74816 KB Output is correct
11 Correct 20 ms 74584 KB Output is correct
12 Correct 19 ms 74588 KB Output is correct
13 Correct 19 ms 74812 KB Output is correct
14 Correct 19 ms 74584 KB Output is correct
15 Correct 20 ms 74584 KB Output is correct
16 Correct 26 ms 74584 KB Output is correct
17 Correct 26 ms 74788 KB Output is correct
18 Correct 21 ms 74812 KB Output is correct
19 Correct 19 ms 74588 KB Output is correct
20 Correct 19 ms 74744 KB Output is correct
21 Correct 21 ms 74840 KB Output is correct
22 Correct 20 ms 74584 KB Output is correct
23 Correct 20 ms 74800 KB Output is correct
24 Correct 21 ms 74800 KB Output is correct
25 Correct 20 ms 74584 KB Output is correct
26 Correct 462 ms 78860 KB Output is correct
27 Correct 1358 ms 78584 KB Output is correct
28 Correct 28 ms 74932 KB Output is correct
29 Correct 19 ms 74584 KB Output is correct
30 Correct 20 ms 74584 KB Output is correct
31 Correct 923 ms 78540 KB Output is correct
32 Correct 28 ms 74740 KB Output is correct
33 Correct 403 ms 85744 KB Output is correct
34 Correct 1327 ms 78620 KB Output is correct
35 Correct 29 ms 75096 KB Output is correct
36 Correct 400 ms 79264 KB Output is correct
37 Correct 31 ms 74936 KB Output is correct
38 Correct 33 ms 74840 KB Output is correct
39 Correct 489 ms 78872 KB Output is correct
40 Correct 25 ms 75152 KB Output is correct
41 Correct 846 ms 78672 KB Output is correct
42 Correct 499 ms 81132 KB Output is correct
43 Correct 20 ms 74584 KB Output is correct
44 Correct 281 ms 85972 KB Output is correct
45 Correct 481 ms 82308 KB Output is correct
46 Correct 28 ms 74840 KB Output is correct
47 Correct 39 ms 74840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 499 ms 78916 KB Output is correct
2 Correct 424 ms 90940 KB Output is correct
3 Correct 26 ms 75052 KB Output is correct
4 Correct 26 ms 75184 KB Output is correct
5 Correct 21 ms 74840 KB Output is correct
6 Correct 19 ms 74800 KB Output is correct
7 Correct 28 ms 74736 KB Output is correct
8 Correct 484 ms 80392 KB Output is correct
9 Correct 30 ms 74832 KB Output is correct
10 Correct 541 ms 80208 KB Output is correct
11 Correct 20 ms 74768 KB Output is correct
12 Correct 341 ms 80096 KB Output is correct
13 Correct 367 ms 83056 KB Output is correct
14 Correct 357 ms 92112 KB Output is correct
15 Correct 486 ms 80260 KB Output is correct
16 Correct 27 ms 75020 KB Output is correct
17 Correct 21 ms 74840 KB Output is correct
18 Correct 304 ms 90416 KB Output is correct
19 Correct 406 ms 96112 KB Output is correct
20 Correct 28 ms 74792 KB Output is correct
21 Correct 19 ms 74588 KB Output is correct
22 Correct 454 ms 83172 KB Output is correct
23 Correct 26 ms 75096 KB Output is correct
24 Correct 345 ms 81232 KB Output is correct
25 Correct 380 ms 94804 KB Output is correct
26 Correct 26 ms 75352 KB Output is correct
27 Correct 25 ms 75176 KB Output is correct
28 Correct 26 ms 74780 KB Output is correct
29 Correct 30 ms 74844 KB Output is correct
30 Correct 30 ms 75052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74580 KB Output is correct
2 Correct 19 ms 74780 KB Output is correct
3 Correct 19 ms 74584 KB Output is correct
4 Correct 19 ms 74584 KB Output is correct
5 Correct 20 ms 74584 KB Output is correct
6 Correct 20 ms 74584 KB Output is correct
7 Correct 20 ms 74584 KB Output is correct
8 Correct 19 ms 74584 KB Output is correct
9 Correct 21 ms 74812 KB Output is correct
10 Correct 20 ms 74816 KB Output is correct
11 Correct 20 ms 74584 KB Output is correct
12 Correct 19 ms 74588 KB Output is correct
13 Correct 19 ms 74812 KB Output is correct
14 Correct 19 ms 74584 KB Output is correct
15 Correct 20 ms 74584 KB Output is correct
16 Correct 26 ms 74584 KB Output is correct
17 Correct 26 ms 74788 KB Output is correct
18 Correct 21 ms 74812 KB Output is correct
19 Correct 19 ms 74588 KB Output is correct
20 Correct 19 ms 74744 KB Output is correct
21 Correct 21 ms 74840 KB Output is correct
22 Correct 20 ms 74584 KB Output is correct
23 Correct 20 ms 74800 KB Output is correct
24 Correct 21 ms 74800 KB Output is correct
25 Correct 19 ms 75016 KB Output is correct
26 Correct 25 ms 75096 KB Output is correct
27 Correct 26 ms 74840 KB Output is correct
28 Correct 27 ms 75096 KB Output is correct
29 Correct 25 ms 75096 KB Output is correct
30 Correct 37 ms 75092 KB Output is correct
31 Correct 19 ms 74584 KB Output is correct
32 Correct 26 ms 75172 KB Output is correct
33 Correct 19 ms 74584 KB Output is correct
34 Correct 29 ms 74844 KB Output is correct
35 Correct 24 ms 75244 KB Output is correct
36 Correct 40 ms 74920 KB Output is correct
37 Correct 31 ms 74840 KB Output is correct
38 Correct 20 ms 74784 KB Output is correct
39 Correct 34 ms 74964 KB Output is correct
40 Correct 32 ms 74840 KB Output is correct
41 Correct 36 ms 74936 KB Output is correct
42 Correct 29 ms 74844 KB Output is correct
43 Correct 24 ms 75060 KB Output is correct
44 Correct 21 ms 74592 KB Output is correct
45 Correct 24 ms 74840 KB Output is correct
46 Correct 32 ms 75100 KB Output is correct
47 Correct 19 ms 74584 KB Output is correct
48 Correct 26 ms 74812 KB Output is correct
49 Correct 25 ms 75276 KB Output is correct
50 Correct 24 ms 75096 KB Output is correct
51 Correct 26 ms 74844 KB Output is correct
52 Correct 34 ms 75096 KB Output is correct
53 Correct 29 ms 74840 KB Output is correct
54 Correct 43 ms 74840 KB Output is correct
55 Correct 20 ms 74584 KB Output is correct
56 Correct 462 ms 78860 KB Output is correct
57 Correct 1358 ms 78584 KB Output is correct
58 Correct 28 ms 74932 KB Output is correct
59 Correct 19 ms 74584 KB Output is correct
60 Correct 20 ms 74584 KB Output is correct
61 Correct 923 ms 78540 KB Output is correct
62 Correct 28 ms 74740 KB Output is correct
63 Correct 403 ms 85744 KB Output is correct
64 Correct 1327 ms 78620 KB Output is correct
65 Correct 29 ms 75096 KB Output is correct
66 Correct 400 ms 79264 KB Output is correct
67 Correct 31 ms 74936 KB Output is correct
68 Correct 33 ms 74840 KB Output is correct
69 Correct 489 ms 78872 KB Output is correct
70 Correct 25 ms 75152 KB Output is correct
71 Correct 846 ms 78672 KB Output is correct
72 Correct 499 ms 81132 KB Output is correct
73 Correct 20 ms 74584 KB Output is correct
74 Correct 281 ms 85972 KB Output is correct
75 Correct 481 ms 82308 KB Output is correct
76 Correct 28 ms 74840 KB Output is correct
77 Correct 39 ms 74840 KB Output is correct
78 Correct 499 ms 78916 KB Output is correct
79 Correct 424 ms 90940 KB Output is correct
80 Correct 26 ms 75052 KB Output is correct
81 Correct 26 ms 75184 KB Output is correct
82 Correct 21 ms 74840 KB Output is correct
83 Correct 19 ms 74800 KB Output is correct
84 Correct 28 ms 74736 KB Output is correct
85 Correct 484 ms 80392 KB Output is correct
86 Correct 30 ms 74832 KB Output is correct
87 Correct 541 ms 80208 KB Output is correct
88 Correct 20 ms 74768 KB Output is correct
89 Correct 341 ms 80096 KB Output is correct
90 Correct 367 ms 83056 KB Output is correct
91 Correct 357 ms 92112 KB Output is correct
92 Correct 486 ms 80260 KB Output is correct
93 Correct 27 ms 75020 KB Output is correct
94 Correct 21 ms 74840 KB Output is correct
95 Correct 304 ms 90416 KB Output is correct
96 Correct 406 ms 96112 KB Output is correct
97 Correct 28 ms 74792 KB Output is correct
98 Correct 19 ms 74588 KB Output is correct
99 Correct 454 ms 83172 KB Output is correct
100 Correct 26 ms 75096 KB Output is correct
101 Correct 345 ms 81232 KB Output is correct
102 Correct 380 ms 94804 KB Output is correct
103 Correct 26 ms 75352 KB Output is correct
104 Correct 25 ms 75176 KB Output is correct
105 Correct 26 ms 74780 KB Output is correct
106 Correct 30 ms 74844 KB Output is correct
107 Correct 30 ms 75052 KB Output is correct
108 Execution timed out 3058 ms 108748 KB Time limit exceeded
109 Halted 0 ms 0 KB -