Submission #893032

# Submission time Handle Problem Language Result Execution time Memory
893032 2023-12-26T11:42:36 Z alexander707070 Security Guard (JOI23_guard) C++14
58 / 100
4000 ms 56236 KB
#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;

struct edge{
    int from,to,cost;
};

bool cmp(edge fr,edge sc){
    return fr.cost<sc.cost;
}

int n,m,q,a,b,s[MAXN],c,d;
vector<edge> w;
unordered_set<int> tree[MAXN];
long long ans;

int dsu[MAXN],sz[MAXN],maxs;

int root(int x){
    if(dsu[x]==x)return x;
    dsu[x]=root(dsu[x]);
    return dsu[x];
}

void mergev(int x,int y){
    int rootx=root(x);
    int rooty=root(y);

    if(sz[rooty]>sz[rootx])swap(rootx,rooty);
    sz[rootx]+=sz[rooty];
    dsu[rooty]=dsu[rootx];
}

int best(int x,int y){
    if(s[x]<s[y])return x;
    return y;
}

int dfs(int x,int p){
    int res=x;

    for(int e:tree[x]){
        if(e==p)continue;
        res=best(res,dfs(e,x));
    }

    return res;
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m>>q;

    for(int i=1;i<=n;i++){
        cin>>s[i];
        ans-=s[i];
        maxs=max(maxs,s[i]);

        dsu[i]=i; sz[i]=1;
    }

    for(int i=1;i<=m;i++){
        cin>>a>>b;
        w.push_back({a,b,s[a]+s[b]});
    }

    sort(w.begin(),w.end(),cmp);

    for(int i=0;i<w.size();i++){
        if(root(w[i].from)==root(w[i].to))continue;
        mergev(w[i].from,w[i].to);

        tree[w[i].from].insert(w[i].to);
        tree[w[i].to].insert(w[i].from);

        ans+=s[w[i].from]+s[w[i].to];
    }

    ans+=maxs;

    cout<<ans<<"\n";

    for(int i=1;i<=q;i++){

        maxs=0;

        for(int f=1;f<=n;f++){
            for(int e:tree[f]){
                if(e<f)continue;

                int sx=dfs(f,e);
                int sy=dfs(e,f);
                
                if(s[f]+s[e]-s[sx]-s[sy]>maxs){
                    maxs=s[f]+s[e]-s[sx]-s[sy];
                    a=f; b=e; c=sx; d=sy;
                }
            }
        }

        if(maxs!=0){
            ans-=maxs;
            tree[a].erase(b);
            tree[b].erase(a);

            tree[c].insert(d);
            tree[d].insert(c);
        }

        cout<<ans<<"\n";
    }


    return 0;
}

Compilation message

guard.cpp: In function 'int main()':
guard.cpp:74:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for(int i=0;i<w.size();i++){
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 131 ms 50340 KB Output is correct
3 Correct 114 ms 50496 KB Output is correct
4 Correct 156 ms 50536 KB Output is correct
5 Correct 145 ms 50356 KB Output is correct
6 Correct 142 ms 50364 KB Output is correct
7 Correct 151 ms 50360 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 131 ms 50340 KB Output is correct
3 Correct 114 ms 50496 KB Output is correct
4 Correct 156 ms 50536 KB Output is correct
5 Correct 145 ms 50356 KB Output is correct
6 Correct 142 ms 50364 KB Output is correct
7 Correct 151 ms 50360 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 215 ms 50324 KB Output is correct
11 Correct 128 ms 50420 KB Output is correct
12 Correct 139 ms 50364 KB Output is correct
13 Correct 112 ms 50392 KB Output is correct
14 Correct 226 ms 50360 KB Output is correct
15 Correct 227 ms 50748 KB Output is correct
16 Correct 214 ms 50844 KB Output is correct
17 Correct 204 ms 50424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 131 ms 50340 KB Output is correct
3 Correct 114 ms 50496 KB Output is correct
4 Correct 156 ms 50536 KB Output is correct
5 Correct 145 ms 50356 KB Output is correct
6 Correct 142 ms 50364 KB Output is correct
7 Correct 151 ms 50360 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 215 ms 50324 KB Output is correct
11 Correct 128 ms 50420 KB Output is correct
12 Correct 139 ms 50364 KB Output is correct
13 Correct 112 ms 50392 KB Output is correct
14 Correct 226 ms 50360 KB Output is correct
15 Correct 227 ms 50748 KB Output is correct
16 Correct 214 ms 50844 KB Output is correct
17 Correct 204 ms 50424 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 253 ms 51004 KB Output is correct
20 Correct 250 ms 50992 KB Output is correct
21 Correct 254 ms 50540 KB Output is correct
22 Correct 258 ms 50452 KB Output is correct
23 Correct 308 ms 52616 KB Output is correct
24 Correct 328 ms 52244 KB Output is correct
25 Correct 317 ms 52540 KB Output is correct
26 Correct 321 ms 53164 KB Output is correct
27 Correct 251 ms 53132 KB Output is correct
28 Correct 286 ms 50880 KB Output is correct
29 Correct 295 ms 51292 KB Output is correct
30 Correct 332 ms 52212 KB Output is correct
31 Correct 242 ms 53180 KB Output is correct
32 Correct 280 ms 51128 KB Output is correct
33 Correct 253 ms 50616 KB Output is correct
34 Correct 252 ms 50316 KB Output is correct
35 Correct 243 ms 50732 KB Output is correct
36 Correct 286 ms 52556 KB Output is correct
37 Correct 347 ms 52576 KB Output is correct
38 Correct 263 ms 51128 KB Output is correct
39 Correct 258 ms 51900 KB Output is correct
40 Correct 234 ms 51172 KB Output is correct
41 Correct 230 ms 50364 KB Output is correct
42 Correct 192 ms 50364 KB Output is correct
43 Correct 261 ms 50440 KB Output is correct
44 Correct 252 ms 50360 KB Output is correct
45 Correct 248 ms 50364 KB Output is correct
46 Correct 255 ms 50420 KB Output is correct
47 Correct 242 ms 50368 KB Output is correct
48 Correct 230 ms 51044 KB Output is correct
49 Correct 231 ms 50356 KB Output is correct
50 Correct 220 ms 50540 KB Output is correct
51 Correct 249 ms 50544 KB Output is correct
52 Correct 209 ms 50356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 131 ms 50340 KB Output is correct
3 Correct 114 ms 50496 KB Output is correct
4 Correct 156 ms 50536 KB Output is correct
5 Correct 145 ms 50356 KB Output is correct
6 Correct 142 ms 50364 KB Output is correct
7 Correct 151 ms 50360 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 215 ms 50324 KB Output is correct
11 Correct 128 ms 50420 KB Output is correct
12 Correct 139 ms 50364 KB Output is correct
13 Correct 112 ms 50392 KB Output is correct
14 Correct 226 ms 50360 KB Output is correct
15 Correct 227 ms 50748 KB Output is correct
16 Correct 214 ms 50844 KB Output is correct
17 Correct 204 ms 50424 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 253 ms 51004 KB Output is correct
20 Correct 250 ms 50992 KB Output is correct
21 Correct 254 ms 50540 KB Output is correct
22 Correct 258 ms 50452 KB Output is correct
23 Correct 308 ms 52616 KB Output is correct
24 Correct 328 ms 52244 KB Output is correct
25 Correct 317 ms 52540 KB Output is correct
26 Correct 321 ms 53164 KB Output is correct
27 Correct 251 ms 53132 KB Output is correct
28 Correct 286 ms 50880 KB Output is correct
29 Correct 295 ms 51292 KB Output is correct
30 Correct 332 ms 52212 KB Output is correct
31 Correct 242 ms 53180 KB Output is correct
32 Correct 280 ms 51128 KB Output is correct
33 Correct 253 ms 50616 KB Output is correct
34 Correct 252 ms 50316 KB Output is correct
35 Correct 243 ms 50732 KB Output is correct
36 Correct 286 ms 52556 KB Output is correct
37 Correct 347 ms 52576 KB Output is correct
38 Correct 263 ms 51128 KB Output is correct
39 Correct 258 ms 51900 KB Output is correct
40 Correct 234 ms 51172 KB Output is correct
41 Correct 230 ms 50364 KB Output is correct
42 Correct 192 ms 50364 KB Output is correct
43 Correct 261 ms 50440 KB Output is correct
44 Correct 252 ms 50360 KB Output is correct
45 Correct 248 ms 50364 KB Output is correct
46 Correct 255 ms 50420 KB Output is correct
47 Correct 242 ms 50368 KB Output is correct
48 Correct 230 ms 51044 KB Output is correct
49 Correct 231 ms 50356 KB Output is correct
50 Correct 220 ms 50540 KB Output is correct
51 Correct 249 ms 50544 KB Output is correct
52 Correct 209 ms 50356 KB Output is correct
53 Correct 3 ms 12636 KB Output is correct
54 Correct 224 ms 50640 KB Output is correct
55 Correct 230 ms 50848 KB Output is correct
56 Correct 257 ms 51376 KB Output is correct
57 Correct 270 ms 54168 KB Output is correct
58 Correct 280 ms 55472 KB Output is correct
59 Correct 308 ms 55496 KB Output is correct
60 Correct 304 ms 54724 KB Output is correct
61 Correct 326 ms 56236 KB Output is correct
62 Correct 285 ms 55712 KB Output is correct
63 Correct 278 ms 50872 KB Output is correct
64 Correct 288 ms 53860 KB Output is correct
65 Correct 313 ms 54456 KB Output is correct
66 Correct 298 ms 56192 KB Output is correct
67 Correct 340 ms 53460 KB Output is correct
68 Correct 204 ms 50628 KB Output is correct
69 Correct 229 ms 52904 KB Output is correct
70 Correct 212 ms 50976 KB Output is correct
71 Correct 230 ms 52668 KB Output is correct
72 Correct 273 ms 52548 KB Output is correct
73 Correct 226 ms 51072 KB Output is correct
74 Correct 223 ms 52200 KB Output is correct
75 Correct 234 ms 51080 KB Output is correct
76 Correct 171 ms 50616 KB Output is correct
77 Correct 172 ms 51148 KB Output is correct
78 Correct 212 ms 51040 KB Output is correct
79 Correct 272 ms 54076 KB Output is correct
80 Correct 219 ms 51128 KB Output is correct
81 Correct 243 ms 52664 KB Output is correct
82 Correct 252 ms 52660 KB Output is correct
83 Correct 254 ms 54196 KB Output is correct
84 Correct 240 ms 53144 KB Output is correct
85 Correct 185 ms 50368 KB Output is correct
86 Correct 214 ms 50544 KB Output is correct
87 Correct 214 ms 50368 KB Output is correct
88 Correct 279 ms 52660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12832 KB Output is correct
4 Correct 3 ms 12728 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 229 ms 14968 KB Output is correct
9 Correct 232 ms 14948 KB Output is correct
10 Correct 231 ms 14660 KB Output is correct
11 Correct 226 ms 14916 KB Output is correct
12 Correct 226 ms 14808 KB Output is correct
13 Correct 232 ms 15008 KB Output is correct
14 Correct 221 ms 14932 KB Output is correct
15 Correct 230 ms 14932 KB Output is correct
16 Correct 222 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12832 KB Output is correct
4 Correct 3 ms 12728 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 229 ms 14968 KB Output is correct
9 Correct 232 ms 14948 KB Output is correct
10 Correct 231 ms 14660 KB Output is correct
11 Correct 226 ms 14916 KB Output is correct
12 Correct 226 ms 14808 KB Output is correct
13 Correct 232 ms 15008 KB Output is correct
14 Correct 221 ms 14932 KB Output is correct
15 Correct 230 ms 14932 KB Output is correct
16 Correct 222 ms 14936 KB Output is correct
17 Execution timed out 4035 ms 13912 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 131 ms 50340 KB Output is correct
3 Correct 114 ms 50496 KB Output is correct
4 Correct 156 ms 50536 KB Output is correct
5 Correct 145 ms 50356 KB Output is correct
6 Correct 142 ms 50364 KB Output is correct
7 Correct 151 ms 50360 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 215 ms 50324 KB Output is correct
11 Correct 128 ms 50420 KB Output is correct
12 Correct 139 ms 50364 KB Output is correct
13 Correct 112 ms 50392 KB Output is correct
14 Correct 226 ms 50360 KB Output is correct
15 Correct 227 ms 50748 KB Output is correct
16 Correct 214 ms 50844 KB Output is correct
17 Correct 204 ms 50424 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 253 ms 51004 KB Output is correct
20 Correct 250 ms 50992 KB Output is correct
21 Correct 254 ms 50540 KB Output is correct
22 Correct 258 ms 50452 KB Output is correct
23 Correct 308 ms 52616 KB Output is correct
24 Correct 328 ms 52244 KB Output is correct
25 Correct 317 ms 52540 KB Output is correct
26 Correct 321 ms 53164 KB Output is correct
27 Correct 251 ms 53132 KB Output is correct
28 Correct 286 ms 50880 KB Output is correct
29 Correct 295 ms 51292 KB Output is correct
30 Correct 332 ms 52212 KB Output is correct
31 Correct 242 ms 53180 KB Output is correct
32 Correct 280 ms 51128 KB Output is correct
33 Correct 253 ms 50616 KB Output is correct
34 Correct 252 ms 50316 KB Output is correct
35 Correct 243 ms 50732 KB Output is correct
36 Correct 286 ms 52556 KB Output is correct
37 Correct 347 ms 52576 KB Output is correct
38 Correct 263 ms 51128 KB Output is correct
39 Correct 258 ms 51900 KB Output is correct
40 Correct 234 ms 51172 KB Output is correct
41 Correct 230 ms 50364 KB Output is correct
42 Correct 192 ms 50364 KB Output is correct
43 Correct 261 ms 50440 KB Output is correct
44 Correct 252 ms 50360 KB Output is correct
45 Correct 248 ms 50364 KB Output is correct
46 Correct 255 ms 50420 KB Output is correct
47 Correct 242 ms 50368 KB Output is correct
48 Correct 230 ms 51044 KB Output is correct
49 Correct 231 ms 50356 KB Output is correct
50 Correct 220 ms 50540 KB Output is correct
51 Correct 249 ms 50544 KB Output is correct
52 Correct 209 ms 50356 KB Output is correct
53 Correct 3 ms 12636 KB Output is correct
54 Correct 224 ms 50640 KB Output is correct
55 Correct 230 ms 50848 KB Output is correct
56 Correct 257 ms 51376 KB Output is correct
57 Correct 270 ms 54168 KB Output is correct
58 Correct 280 ms 55472 KB Output is correct
59 Correct 308 ms 55496 KB Output is correct
60 Correct 304 ms 54724 KB Output is correct
61 Correct 326 ms 56236 KB Output is correct
62 Correct 285 ms 55712 KB Output is correct
63 Correct 278 ms 50872 KB Output is correct
64 Correct 288 ms 53860 KB Output is correct
65 Correct 313 ms 54456 KB Output is correct
66 Correct 298 ms 56192 KB Output is correct
67 Correct 340 ms 53460 KB Output is correct
68 Correct 204 ms 50628 KB Output is correct
69 Correct 229 ms 52904 KB Output is correct
70 Correct 212 ms 50976 KB Output is correct
71 Correct 230 ms 52668 KB Output is correct
72 Correct 273 ms 52548 KB Output is correct
73 Correct 226 ms 51072 KB Output is correct
74 Correct 223 ms 52200 KB Output is correct
75 Correct 234 ms 51080 KB Output is correct
76 Correct 171 ms 50616 KB Output is correct
77 Correct 172 ms 51148 KB Output is correct
78 Correct 212 ms 51040 KB Output is correct
79 Correct 272 ms 54076 KB Output is correct
80 Correct 219 ms 51128 KB Output is correct
81 Correct 243 ms 52664 KB Output is correct
82 Correct 252 ms 52660 KB Output is correct
83 Correct 254 ms 54196 KB Output is correct
84 Correct 240 ms 53144 KB Output is correct
85 Correct 185 ms 50368 KB Output is correct
86 Correct 214 ms 50544 KB Output is correct
87 Correct 214 ms 50368 KB Output is correct
88 Correct 279 ms 52660 KB Output is correct
89 Correct 3 ms 12636 KB Output is correct
90 Correct 3 ms 12636 KB Output is correct
91 Correct 3 ms 12832 KB Output is correct
92 Correct 3 ms 12728 KB Output is correct
93 Correct 3 ms 12636 KB Output is correct
94 Correct 3 ms 12636 KB Output is correct
95 Correct 3 ms 12636 KB Output is correct
96 Correct 229 ms 14968 KB Output is correct
97 Correct 232 ms 14948 KB Output is correct
98 Correct 231 ms 14660 KB Output is correct
99 Correct 226 ms 14916 KB Output is correct
100 Correct 226 ms 14808 KB Output is correct
101 Correct 232 ms 15008 KB Output is correct
102 Correct 221 ms 14932 KB Output is correct
103 Correct 230 ms 14932 KB Output is correct
104 Correct 222 ms 14936 KB Output is correct
105 Execution timed out 4035 ms 13912 KB Time limit exceeded
106 Halted 0 ms 0 KB -