답안 #937068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937068 2024-03-03T10:39:09 Z Pacybwoah Security Guard (JOI23_guard) C++17
100 / 100
563 ms 85964 KB
#include<iostream>
#include<algorithm>
#include<vector>
#include<utility>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
vector<int> dsu,rep,sz;
int query(int x){
    if(x==dsu[x]) return x;
    dsu[x]=query(dsu[x]);
    return dsu[x];
}
void unite(int a,int b){
    a=query(a);
    b=query(b);
    if(a==b) return;
    if(sz[a]<sz[b]) swap(a,b);
    dsu[b]=a;
    sz[a]+=sz[b];
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,q;
    cin>>n>>m>>q;
    vector<ll> s(n+1);
    for(int i=1;i<=n;i++) cin>>s[i];
    int a,b;
    vector<pair<int,int> > ori,edges;
    for(int i=0;i<m;i++){
        cin>>a>>b;
        ori.push_back(make_pair(a,b));
    }
    sort(ori.begin(),ori.end(),[&](pair<int,int> a,pair<int,int> b){return s[a.first]+s[a.second]<s[b.first]+s[b.second];});
    vector<priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > > pq(n+1);
    set<pair<ll,int> > finpq;
    vector<bool> vis(n-1);
    dsu.resize(n+1);
    rep.resize(n+1);
    sz.resize(n+1);
    int minpos;
    auto build=[&](){
        for(int i=1;i<=n;i++){
            dsu[i]=i;
            sz[i]=1;
            rep[i]=i;
        }
        if(!edges.empty()){
            for(int i=0;i<n-1;i++){
                auto &[u,v]=edges[i];
                pq[u].push(make_pair(s[u]+s[v],i));
                pq[v].push(make_pair(s[u]+s[v],i));
            }
            for(int i=1;i<=n;i++){
                if(i==minpos) continue;
                finpq.insert(make_pair(pq[i].top().first-s[rep[i]],i));
            }
        }
    }; 
    build();
    for(auto &[u,v]:ori){
        if(query(u)!=query(v)){
            unite(u,v);
            edges.push_back(make_pair(u,v));
        }
    }
    vector<ll> ans(n);
    ll smin=2e9,smax=0;
    for(int i=1;i<=n;i++){
        if(s[i]<smin){
            smin=s[i];
            minpos=i;
        }
        smax=max(smax,s[i]);
        ans[n-1]-=s[i];
    }
    ans[n-1]+=smax;
    for(int i=1;i<=n;i++){
        if(i==minpos) continue;
        ans[n-1]+=smin+s[i];
    }
    build();
    for(int i=n-2;i>=0;i--){
        auto [sum,node]=*finpq.begin();
        finpq.erase(finpq.begin());
        ans[i]=ans[i+1]+sum-smin;
        auto [w,eid]=pq[node].top();
        vis[eid]=1;
        auto [u,v]=edges[eid];
        if(query(u)!=node) swap(u,v);
        v=query(v);u=query(u);finpq.erase(make_pair(pq[v].top().first-s[rep[v]],v));
        a=u;
        b=v;
        if(pq[a].size()<pq[b].size()) swap(a,b);
        dsu[b]=a;
        while(!pq[b].empty()){
            if(vis[pq[b].top().second]){
                pq[b].pop();
            }
            else{
                pq[a].push(pq[b].top());
                pq[b].pop();
            }
        }
        rep[a]=rep[v];
        while(!pq[a].empty()){
            if(vis[pq[a].top().second]){
                pq[a].pop();
            }
            else{
                finpq.insert(make_pair(pq[a].top().first-s[rep[a]],a));
                
                break;
            }
        }
    }
    for(int i=0;i<=q;i++){
        if(i>=n) cout<<ans[n-1]<<"\n";
        else cout<<ans[i]<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 161 ms 39760 KB Output is correct
3 Correct 152 ms 39752 KB Output is correct
4 Correct 177 ms 40144 KB Output is correct
5 Correct 190 ms 40308 KB Output is correct
6 Correct 184 ms 40220 KB Output is correct
7 Correct 180 ms 40264 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 161 ms 39760 KB Output is correct
3 Correct 152 ms 39752 KB Output is correct
4 Correct 177 ms 40144 KB Output is correct
5 Correct 190 ms 40308 KB Output is correct
6 Correct 184 ms 40220 KB Output is correct
7 Correct 180 ms 40264 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 475 ms 42164 KB Output is correct
11 Correct 165 ms 41112 KB Output is correct
12 Correct 168 ms 41108 KB Output is correct
13 Correct 141 ms 40860 KB Output is correct
14 Correct 470 ms 42312 KB Output is correct
15 Correct 479 ms 42212 KB Output is correct
16 Correct 511 ms 42064 KB Output is correct
17 Correct 516 ms 42068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 161 ms 39760 KB Output is correct
3 Correct 152 ms 39752 KB Output is correct
4 Correct 177 ms 40144 KB Output is correct
5 Correct 190 ms 40308 KB Output is correct
6 Correct 184 ms 40220 KB Output is correct
7 Correct 180 ms 40264 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 475 ms 42164 KB Output is correct
11 Correct 165 ms 41112 KB Output is correct
12 Correct 168 ms 41108 KB Output is correct
13 Correct 141 ms 40860 KB Output is correct
14 Correct 470 ms 42312 KB Output is correct
15 Correct 479 ms 42212 KB Output is correct
16 Correct 511 ms 42064 KB Output is correct
17 Correct 516 ms 42068 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 559 ms 43084 KB Output is correct
20 Correct 477 ms 43012 KB Output is correct
21 Correct 490 ms 43688 KB Output is correct
22 Correct 499 ms 43212 KB Output is correct
23 Correct 280 ms 41696 KB Output is correct
24 Correct 232 ms 41300 KB Output is correct
25 Correct 254 ms 41132 KB Output is correct
26 Correct 203 ms 41036 KB Output is correct
27 Correct 216 ms 41028 KB Output is correct
28 Correct 458 ms 43332 KB Output is correct
29 Correct 439 ms 42968 KB Output is correct
30 Correct 284 ms 42024 KB Output is correct
31 Correct 230 ms 41232 KB Output is correct
32 Correct 397 ms 43276 KB Output is correct
33 Correct 375 ms 42760 KB Output is correct
34 Correct 339 ms 67696 KB Output is correct
35 Correct 332 ms 84084 KB Output is correct
36 Correct 272 ms 69704 KB Output is correct
37 Correct 283 ms 67312 KB Output is correct
38 Correct 336 ms 42392 KB Output is correct
39 Correct 248 ms 40772 KB Output is correct
40 Correct 287 ms 41460 KB Output is correct
41 Correct 314 ms 40260 KB Output is correct
42 Correct 279 ms 40344 KB Output is correct
43 Correct 353 ms 43272 KB Output is correct
44 Correct 486 ms 43312 KB Output is correct
45 Correct 388 ms 43324 KB Output is correct
46 Correct 450 ms 43376 KB Output is correct
47 Correct 520 ms 43536 KB Output is correct
48 Correct 465 ms 43208 KB Output is correct
49 Correct 317 ms 43012 KB Output is correct
50 Correct 419 ms 42052 KB Output is correct
51 Correct 331 ms 42312 KB Output is correct
52 Correct 311 ms 40972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 161 ms 39760 KB Output is correct
3 Correct 152 ms 39752 KB Output is correct
4 Correct 177 ms 40144 KB Output is correct
5 Correct 190 ms 40308 KB Output is correct
6 Correct 184 ms 40220 KB Output is correct
7 Correct 180 ms 40264 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 475 ms 42164 KB Output is correct
11 Correct 165 ms 41112 KB Output is correct
12 Correct 168 ms 41108 KB Output is correct
13 Correct 141 ms 40860 KB Output is correct
14 Correct 470 ms 42312 KB Output is correct
15 Correct 479 ms 42212 KB Output is correct
16 Correct 511 ms 42064 KB Output is correct
17 Correct 516 ms 42068 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 559 ms 43084 KB Output is correct
20 Correct 477 ms 43012 KB Output is correct
21 Correct 490 ms 43688 KB Output is correct
22 Correct 499 ms 43212 KB Output is correct
23 Correct 280 ms 41696 KB Output is correct
24 Correct 232 ms 41300 KB Output is correct
25 Correct 254 ms 41132 KB Output is correct
26 Correct 203 ms 41036 KB Output is correct
27 Correct 216 ms 41028 KB Output is correct
28 Correct 458 ms 43332 KB Output is correct
29 Correct 439 ms 42968 KB Output is correct
30 Correct 284 ms 42024 KB Output is correct
31 Correct 230 ms 41232 KB Output is correct
32 Correct 397 ms 43276 KB Output is correct
33 Correct 375 ms 42760 KB Output is correct
34 Correct 339 ms 67696 KB Output is correct
35 Correct 332 ms 84084 KB Output is correct
36 Correct 272 ms 69704 KB Output is correct
37 Correct 283 ms 67312 KB Output is correct
38 Correct 336 ms 42392 KB Output is correct
39 Correct 248 ms 40772 KB Output is correct
40 Correct 287 ms 41460 KB Output is correct
41 Correct 314 ms 40260 KB Output is correct
42 Correct 279 ms 40344 KB Output is correct
43 Correct 353 ms 43272 KB Output is correct
44 Correct 486 ms 43312 KB Output is correct
45 Correct 388 ms 43324 KB Output is correct
46 Correct 450 ms 43376 KB Output is correct
47 Correct 520 ms 43536 KB Output is correct
48 Correct 465 ms 43208 KB Output is correct
49 Correct 317 ms 43012 KB Output is correct
50 Correct 419 ms 42052 KB Output is correct
51 Correct 331 ms 42312 KB Output is correct
52 Correct 311 ms 40972 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 522 ms 41928 KB Output is correct
55 Correct 485 ms 41844 KB Output is correct
56 Correct 505 ms 44796 KB Output is correct
57 Correct 506 ms 44988 KB Output is correct
58 Correct 333 ms 43964 KB Output is correct
59 Correct 298 ms 43724 KB Output is correct
60 Correct 254 ms 42512 KB Output is correct
61 Correct 250 ms 43468 KB Output is correct
62 Correct 269 ms 45500 KB Output is correct
63 Correct 471 ms 43080 KB Output is correct
64 Correct 464 ms 45648 KB Output is correct
65 Correct 306 ms 44484 KB Output is correct
66 Correct 302 ms 44996 KB Output is correct
67 Correct 465 ms 45248 KB Output is correct
68 Correct 392 ms 42848 KB Output is correct
69 Correct 374 ms 69596 KB Output is correct
70 Correct 361 ms 84552 KB Output is correct
71 Correct 260 ms 68444 KB Output is correct
72 Correct 282 ms 67996 KB Output is correct
73 Correct 314 ms 42308 KB Output is correct
74 Correct 239 ms 40644 KB Output is correct
75 Correct 291 ms 41592 KB Output is correct
76 Correct 293 ms 40340 KB Output is correct
77 Correct 303 ms 40336 KB Output is correct
78 Correct 365 ms 43768 KB Output is correct
79 Correct 554 ms 46956 KB Output is correct
80 Correct 450 ms 44312 KB Output is correct
81 Correct 460 ms 47132 KB Output is correct
82 Correct 458 ms 47464 KB Output is correct
83 Correct 548 ms 46560 KB Output is correct
84 Correct 401 ms 46784 KB Output is correct
85 Correct 393 ms 42080 KB Output is correct
86 Correct 339 ms 42264 KB Output is correct
87 Correct 330 ms 40780 KB Output is correct
88 Correct 502 ms 48324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 12 ms 2652 KB Output is correct
9 Correct 13 ms 2652 KB Output is correct
10 Correct 14 ms 2396 KB Output is correct
11 Correct 13 ms 2648 KB Output is correct
12 Correct 13 ms 2652 KB Output is correct
13 Correct 13 ms 2652 KB Output is correct
14 Correct 13 ms 2648 KB Output is correct
15 Correct 15 ms 2652 KB Output is correct
16 Correct 13 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 12 ms 2652 KB Output is correct
9 Correct 13 ms 2652 KB Output is correct
10 Correct 14 ms 2396 KB Output is correct
11 Correct 13 ms 2648 KB Output is correct
12 Correct 13 ms 2652 KB Output is correct
13 Correct 13 ms 2652 KB Output is correct
14 Correct 13 ms 2648 KB Output is correct
15 Correct 15 ms 2652 KB Output is correct
16 Correct 13 ms 2652 KB Output is correct
17 Correct 20 ms 3544 KB Output is correct
18 Correct 20 ms 3536 KB Output is correct
19 Correct 33 ms 4640 KB Output is correct
20 Correct 62 ms 6068 KB Output is correct
21 Correct 65 ms 7000 KB Output is correct
22 Correct 50 ms 6088 KB Output is correct
23 Correct 103 ms 9400 KB Output is correct
24 Correct 63 ms 7108 KB Output is correct
25 Correct 15 ms 3164 KB Output is correct
26 Correct 36 ms 5096 KB Output is correct
27 Correct 20 ms 4056 KB Output is correct
28 Correct 16 ms 3160 KB Output is correct
29 Correct 15 ms 3164 KB Output is correct
30 Correct 20 ms 3272 KB Output is correct
31 Correct 13 ms 1984 KB Output is correct
32 Correct 13 ms 1884 KB Output is correct
33 Correct 16 ms 3164 KB Output is correct
34 Correct 16 ms 3412 KB Output is correct
35 Correct 34 ms 4808 KB Output is correct
36 Correct 97 ms 9008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 161 ms 39760 KB Output is correct
3 Correct 152 ms 39752 KB Output is correct
4 Correct 177 ms 40144 KB Output is correct
5 Correct 190 ms 40308 KB Output is correct
6 Correct 184 ms 40220 KB Output is correct
7 Correct 180 ms 40264 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 475 ms 42164 KB Output is correct
11 Correct 165 ms 41112 KB Output is correct
12 Correct 168 ms 41108 KB Output is correct
13 Correct 141 ms 40860 KB Output is correct
14 Correct 470 ms 42312 KB Output is correct
15 Correct 479 ms 42212 KB Output is correct
16 Correct 511 ms 42064 KB Output is correct
17 Correct 516 ms 42068 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 559 ms 43084 KB Output is correct
20 Correct 477 ms 43012 KB Output is correct
21 Correct 490 ms 43688 KB Output is correct
22 Correct 499 ms 43212 KB Output is correct
23 Correct 280 ms 41696 KB Output is correct
24 Correct 232 ms 41300 KB Output is correct
25 Correct 254 ms 41132 KB Output is correct
26 Correct 203 ms 41036 KB Output is correct
27 Correct 216 ms 41028 KB Output is correct
28 Correct 458 ms 43332 KB Output is correct
29 Correct 439 ms 42968 KB Output is correct
30 Correct 284 ms 42024 KB Output is correct
31 Correct 230 ms 41232 KB Output is correct
32 Correct 397 ms 43276 KB Output is correct
33 Correct 375 ms 42760 KB Output is correct
34 Correct 339 ms 67696 KB Output is correct
35 Correct 332 ms 84084 KB Output is correct
36 Correct 272 ms 69704 KB Output is correct
37 Correct 283 ms 67312 KB Output is correct
38 Correct 336 ms 42392 KB Output is correct
39 Correct 248 ms 40772 KB Output is correct
40 Correct 287 ms 41460 KB Output is correct
41 Correct 314 ms 40260 KB Output is correct
42 Correct 279 ms 40344 KB Output is correct
43 Correct 353 ms 43272 KB Output is correct
44 Correct 486 ms 43312 KB Output is correct
45 Correct 388 ms 43324 KB Output is correct
46 Correct 450 ms 43376 KB Output is correct
47 Correct 520 ms 43536 KB Output is correct
48 Correct 465 ms 43208 KB Output is correct
49 Correct 317 ms 43012 KB Output is correct
50 Correct 419 ms 42052 KB Output is correct
51 Correct 331 ms 42312 KB Output is correct
52 Correct 311 ms 40972 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 522 ms 41928 KB Output is correct
55 Correct 485 ms 41844 KB Output is correct
56 Correct 505 ms 44796 KB Output is correct
57 Correct 506 ms 44988 KB Output is correct
58 Correct 333 ms 43964 KB Output is correct
59 Correct 298 ms 43724 KB Output is correct
60 Correct 254 ms 42512 KB Output is correct
61 Correct 250 ms 43468 KB Output is correct
62 Correct 269 ms 45500 KB Output is correct
63 Correct 471 ms 43080 KB Output is correct
64 Correct 464 ms 45648 KB Output is correct
65 Correct 306 ms 44484 KB Output is correct
66 Correct 302 ms 44996 KB Output is correct
67 Correct 465 ms 45248 KB Output is correct
68 Correct 392 ms 42848 KB Output is correct
69 Correct 374 ms 69596 KB Output is correct
70 Correct 361 ms 84552 KB Output is correct
71 Correct 260 ms 68444 KB Output is correct
72 Correct 282 ms 67996 KB Output is correct
73 Correct 314 ms 42308 KB Output is correct
74 Correct 239 ms 40644 KB Output is correct
75 Correct 291 ms 41592 KB Output is correct
76 Correct 293 ms 40340 KB Output is correct
77 Correct 303 ms 40336 KB Output is correct
78 Correct 365 ms 43768 KB Output is correct
79 Correct 554 ms 46956 KB Output is correct
80 Correct 450 ms 44312 KB Output is correct
81 Correct 460 ms 47132 KB Output is correct
82 Correct 458 ms 47464 KB Output is correct
83 Correct 548 ms 46560 KB Output is correct
84 Correct 401 ms 46784 KB Output is correct
85 Correct 393 ms 42080 KB Output is correct
86 Correct 339 ms 42264 KB Output is correct
87 Correct 330 ms 40780 KB Output is correct
88 Correct 502 ms 48324 KB Output is correct
89 Correct 1 ms 600 KB Output is correct
90 Correct 1 ms 344 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 0 ms 348 KB Output is correct
93 Correct 1 ms 344 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 1 ms 348 KB Output is correct
96 Correct 12 ms 2652 KB Output is correct
97 Correct 13 ms 2652 KB Output is correct
98 Correct 14 ms 2396 KB Output is correct
99 Correct 13 ms 2648 KB Output is correct
100 Correct 13 ms 2652 KB Output is correct
101 Correct 13 ms 2652 KB Output is correct
102 Correct 13 ms 2648 KB Output is correct
103 Correct 15 ms 2652 KB Output is correct
104 Correct 13 ms 2652 KB Output is correct
105 Correct 20 ms 3544 KB Output is correct
106 Correct 20 ms 3536 KB Output is correct
107 Correct 33 ms 4640 KB Output is correct
108 Correct 62 ms 6068 KB Output is correct
109 Correct 65 ms 7000 KB Output is correct
110 Correct 50 ms 6088 KB Output is correct
111 Correct 103 ms 9400 KB Output is correct
112 Correct 63 ms 7108 KB Output is correct
113 Correct 15 ms 3164 KB Output is correct
114 Correct 36 ms 5096 KB Output is correct
115 Correct 20 ms 4056 KB Output is correct
116 Correct 16 ms 3160 KB Output is correct
117 Correct 15 ms 3164 KB Output is correct
118 Correct 20 ms 3272 KB Output is correct
119 Correct 13 ms 1984 KB Output is correct
120 Correct 13 ms 1884 KB Output is correct
121 Correct 16 ms 3164 KB Output is correct
122 Correct 16 ms 3412 KB Output is correct
123 Correct 34 ms 4808 KB Output is correct
124 Correct 97 ms 9008 KB Output is correct
125 Correct 536 ms 45008 KB Output is correct
126 Correct 511 ms 44628 KB Output is correct
127 Correct 507 ms 45544 KB Output is correct
128 Correct 496 ms 47524 KB Output is correct
129 Correct 320 ms 45476 KB Output is correct
130 Correct 326 ms 46280 KB Output is correct
131 Correct 281 ms 45508 KB Output is correct
132 Correct 257 ms 45496 KB Output is correct
133 Correct 289 ms 45504 KB Output is correct
134 Correct 488 ms 44216 KB Output is correct
135 Correct 415 ms 44984 KB Output is correct
136 Correct 323 ms 46172 KB Output is correct
137 Correct 278 ms 47300 KB Output is correct
138 Correct 480 ms 46404 KB Output is correct
139 Correct 380 ms 43852 KB Output is correct
140 Correct 400 ms 71752 KB Output is correct
141 Correct 344 ms 85964 KB Output is correct
142 Correct 291 ms 70248 KB Output is correct
143 Correct 271 ms 67128 KB Output is correct
144 Correct 333 ms 43672 KB Output is correct
145 Correct 251 ms 42064 KB Output is correct
146 Correct 268 ms 42828 KB Output is correct
147 Correct 285 ms 41288 KB Output is correct
148 Correct 315 ms 41344 KB Output is correct
149 Correct 359 ms 44924 KB Output is correct
150 Correct 563 ms 48304 KB Output is correct
151 Correct 394 ms 45896 KB Output is correct
152 Correct 483 ms 49284 KB Output is correct
153 Correct 537 ms 48320 KB Output is correct
154 Correct 490 ms 48080 KB Output is correct
155 Correct 427 ms 47268 KB Output is correct
156 Correct 409 ms 43592 KB Output is correct
157 Correct 369 ms 44148 KB Output is correct
158 Correct 347 ms 41804 KB Output is correct
159 Correct 512 ms 49576 KB Output is correct