Submission #796234

# Submission time Handle Problem Language Result Execution time Memory
796234 2023-07-28T08:15:02 Z 반딧불(#10068) Security Guard (JOI23_guard) C++17
37 / 100
267 ms 77084 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct unionFind{
    int par[200002];
    ll groupAdd[200002], oneAdd[200002];
    vector<int> vec[200002];

    void init(int n){
        for(int i=1; i<=n; i++){
            par[i] = i;
            groupAdd[i] = oneAdd[i] = 0;
            vec[i].push_back(i);
        }
    }

    int find(int x){
        if(x==par[x]) return x;
        return par[x] = find(par[x]);
    }

    void merge(int x, int y){
        x = find(x), y = find(y);
        assert(x!=y);
        if(vec[x].size() < vec[y].size()) swap(x, y);
        for(int p: vec[y]){
            vec[x].push_back(p);
            oneAdd[p] += groupAdd[y] - groupAdd[x];
        }
        groupAdd[y] = 0;
        par[y] = x;
        vec[y].clear();
    }

    inline ll query(int x){
        return oneAdd[x] + groupAdd[find(x)];
    }

    inline void add(int x, ll y){
        groupAdd[find(x)] += y;
    }
} dsu;

struct dat{
    int y; ll cost, profit;
    dat(){}
    dat(int y, ll cost, ll profit): y(y), cost(cost), profit(profit){}
    bool operator<(const dat &r)const{
        if(cost != r.cost) return cost < r.cost;
        return profit > r.profit;
    }
};

int n, m, q;
ll arr[200002], ex[400002], ey[400002];
vector<int> link[200002];
ll ans;

bool used[200002];
bool chk[200002];
ll yp[200002], yc[200002];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> q;
    for(int i=1; i<=n; i++){
        cin >> arr[i];
    }
    for(int i=1; i<=m; i++){
        int x, y;
        cin >> x >> y;
        ex[i] = x, ey[i] = y;
        link[x].push_back(y);
        link[y].push_back(x);
    }

    dsu.init(n);

    vector<pair<ll, int> > vec;
    for(int i=1; i<=n; i++) vec.push_back(make_pair(arr[i], i));
    sort(vec.begin(), vec.end());

    for(auto [v, x]: vec){
        vector<dat> reqVec; /// � �������� �������� �󸶳� �ʿ������� ����� ��
        used[x] = 1;
        for(auto y: link[x]){
            if(!used[y]) continue;
            ll w = arr[x] - dsu.query(y);
            reqVec.push_back(dat(y, max(arr[y], w), w > arr[y] ? 0 : arr[y] - w));
        }
//        sort(reqVec.begin(), reqVec.end());

        ll allAdd = 0;
        for(dat pr: reqVec){
            int y = pr.y; ll w = pr.cost, p = pr.profit;
            if(chk[dsu.find(y)]){
                assert(-w+p <= -yc[dsu.find(y)]+yp[dsu.find(y)]);
                continue;
            }
            chk[dsu.find(y)] = 1, yp[dsu.find(y)] = p, yc[dsu.find(y)] = w;
            ans += w;
            if(w > arr[y]) dsu.add(y, w-arr[y]); /// y �ʿ� �̵��� �ִ�
            else{
                ll tmp = p;
                allAdd += tmp;
                dsu.add(y, -tmp);
            }
        }
        for(auto [y, w, p]: reqVec){
            if(!chk[dsu.find(y)]) continue;
            chk[dsu.find(y)] = 0;
        }
        for(auto [y, w, p]: reqVec){
            if(dsu.find(x) == dsu.find(y)) continue;
            dsu.merge(x, y);
        }
        dsu.add(x, allAdd);
    }

    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 91 ms 35912 KB Output is correct
3 Correct 81 ms 35952 KB Output is correct
4 Correct 94 ms 39448 KB Output is correct
5 Correct 100 ms 39424 KB Output is correct
6 Correct 97 ms 39368 KB Output is correct
7 Correct 96 ms 39404 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 91 ms 35912 KB Output is correct
3 Correct 81 ms 35952 KB Output is correct
4 Correct 94 ms 39448 KB Output is correct
5 Correct 100 ms 39424 KB Output is correct
6 Correct 97 ms 39368 KB Output is correct
7 Correct 96 ms 39404 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 207 ms 45524 KB Output is correct
11 Correct 95 ms 35988 KB Output is correct
12 Correct 88 ms 36024 KB Output is correct
13 Correct 86 ms 36188 KB Output is correct
14 Correct 201 ms 45364 KB Output is correct
15 Correct 188 ms 46128 KB Output is correct
16 Correct 192 ms 45752 KB Output is correct
17 Correct 229 ms 45704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 91 ms 35912 KB Output is correct
3 Correct 81 ms 35952 KB Output is correct
4 Correct 94 ms 39448 KB Output is correct
5 Correct 100 ms 39424 KB Output is correct
6 Correct 97 ms 39368 KB Output is correct
7 Correct 96 ms 39404 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 207 ms 45524 KB Output is correct
11 Correct 95 ms 35988 KB Output is correct
12 Correct 88 ms 36024 KB Output is correct
13 Correct 86 ms 36188 KB Output is correct
14 Correct 201 ms 45364 KB Output is correct
15 Correct 188 ms 46128 KB Output is correct
16 Correct 192 ms 45752 KB Output is correct
17 Correct 229 ms 45704 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 250 ms 45208 KB Output is correct
20 Correct 229 ms 44808 KB Output is correct
21 Correct 232 ms 43184 KB Output is correct
22 Correct 236 ms 41292 KB Output is correct
23 Correct 209 ms 42404 KB Output is correct
24 Correct 184 ms 41340 KB Output is correct
25 Correct 194 ms 41464 KB Output is correct
26 Correct 176 ms 43760 KB Output is correct
27 Correct 150 ms 45616 KB Output is correct
28 Correct 226 ms 45004 KB Output is correct
29 Correct 243 ms 43892 KB Output is correct
30 Correct 213 ms 41416 KB Output is correct
31 Correct 149 ms 45688 KB Output is correct
32 Correct 206 ms 39676 KB Output is correct
33 Correct 263 ms 47600 KB Output is correct
34 Correct 190 ms 41836 KB Output is correct
35 Correct 176 ms 41520 KB Output is correct
36 Correct 151 ms 40340 KB Output is correct
37 Correct 141 ms 40292 KB Output is correct
38 Correct 197 ms 44184 KB Output is correct
39 Correct 172 ms 37016 KB Output is correct
40 Correct 203 ms 44024 KB Output is correct
41 Correct 214 ms 36076 KB Output is correct
42 Correct 201 ms 35996 KB Output is correct
43 Correct 213 ms 40592 KB Output is correct
44 Correct 219 ms 40580 KB Output is correct
45 Correct 215 ms 41008 KB Output is correct
46 Correct 245 ms 40492 KB Output is correct
47 Correct 221 ms 40876 KB Output is correct
48 Correct 236 ms 40860 KB Output is correct
49 Correct 226 ms 36132 KB Output is correct
50 Correct 267 ms 44320 KB Output is correct
51 Correct 191 ms 41068 KB Output is correct
52 Correct 186 ms 40488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 91 ms 35912 KB Output is correct
3 Correct 81 ms 35952 KB Output is correct
4 Correct 94 ms 39448 KB Output is correct
5 Correct 100 ms 39424 KB Output is correct
6 Correct 97 ms 39368 KB Output is correct
7 Correct 96 ms 39404 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 207 ms 45524 KB Output is correct
11 Correct 95 ms 35988 KB Output is correct
12 Correct 88 ms 36024 KB Output is correct
13 Correct 86 ms 36188 KB Output is correct
14 Correct 201 ms 45364 KB Output is correct
15 Correct 188 ms 46128 KB Output is correct
16 Correct 192 ms 45752 KB Output is correct
17 Correct 229 ms 45704 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 250 ms 45208 KB Output is correct
20 Correct 229 ms 44808 KB Output is correct
21 Correct 232 ms 43184 KB Output is correct
22 Correct 236 ms 41292 KB Output is correct
23 Correct 209 ms 42404 KB Output is correct
24 Correct 184 ms 41340 KB Output is correct
25 Correct 194 ms 41464 KB Output is correct
26 Correct 176 ms 43760 KB Output is correct
27 Correct 150 ms 45616 KB Output is correct
28 Correct 226 ms 45004 KB Output is correct
29 Correct 243 ms 43892 KB Output is correct
30 Correct 213 ms 41416 KB Output is correct
31 Correct 149 ms 45688 KB Output is correct
32 Correct 206 ms 39676 KB Output is correct
33 Correct 263 ms 47600 KB Output is correct
34 Correct 190 ms 41836 KB Output is correct
35 Correct 176 ms 41520 KB Output is correct
36 Correct 151 ms 40340 KB Output is correct
37 Correct 141 ms 40292 KB Output is correct
38 Correct 197 ms 44184 KB Output is correct
39 Correct 172 ms 37016 KB Output is correct
40 Correct 203 ms 44024 KB Output is correct
41 Correct 214 ms 36076 KB Output is correct
42 Correct 201 ms 35996 KB Output is correct
43 Correct 213 ms 40592 KB Output is correct
44 Correct 219 ms 40580 KB Output is correct
45 Correct 215 ms 41008 KB Output is correct
46 Correct 245 ms 40492 KB Output is correct
47 Correct 221 ms 40876 KB Output is correct
48 Correct 236 ms 40860 KB Output is correct
49 Correct 226 ms 36132 KB Output is correct
50 Correct 267 ms 44320 KB Output is correct
51 Correct 191 ms 41068 KB Output is correct
52 Correct 186 ms 40488 KB Output is correct
53 Correct 4 ms 9660 KB Output is correct
54 Runtime error 174 ms 77084 KB Execution killed with signal 6
55 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9712 KB Output is correct
2 Incorrect 5 ms 9776 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9712 KB Output is correct
2 Incorrect 5 ms 9776 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 91 ms 35912 KB Output is correct
3 Correct 81 ms 35952 KB Output is correct
4 Correct 94 ms 39448 KB Output is correct
5 Correct 100 ms 39424 KB Output is correct
6 Correct 97 ms 39368 KB Output is correct
7 Correct 96 ms 39404 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 207 ms 45524 KB Output is correct
11 Correct 95 ms 35988 KB Output is correct
12 Correct 88 ms 36024 KB Output is correct
13 Correct 86 ms 36188 KB Output is correct
14 Correct 201 ms 45364 KB Output is correct
15 Correct 188 ms 46128 KB Output is correct
16 Correct 192 ms 45752 KB Output is correct
17 Correct 229 ms 45704 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 250 ms 45208 KB Output is correct
20 Correct 229 ms 44808 KB Output is correct
21 Correct 232 ms 43184 KB Output is correct
22 Correct 236 ms 41292 KB Output is correct
23 Correct 209 ms 42404 KB Output is correct
24 Correct 184 ms 41340 KB Output is correct
25 Correct 194 ms 41464 KB Output is correct
26 Correct 176 ms 43760 KB Output is correct
27 Correct 150 ms 45616 KB Output is correct
28 Correct 226 ms 45004 KB Output is correct
29 Correct 243 ms 43892 KB Output is correct
30 Correct 213 ms 41416 KB Output is correct
31 Correct 149 ms 45688 KB Output is correct
32 Correct 206 ms 39676 KB Output is correct
33 Correct 263 ms 47600 KB Output is correct
34 Correct 190 ms 41836 KB Output is correct
35 Correct 176 ms 41520 KB Output is correct
36 Correct 151 ms 40340 KB Output is correct
37 Correct 141 ms 40292 KB Output is correct
38 Correct 197 ms 44184 KB Output is correct
39 Correct 172 ms 37016 KB Output is correct
40 Correct 203 ms 44024 KB Output is correct
41 Correct 214 ms 36076 KB Output is correct
42 Correct 201 ms 35996 KB Output is correct
43 Correct 213 ms 40592 KB Output is correct
44 Correct 219 ms 40580 KB Output is correct
45 Correct 215 ms 41008 KB Output is correct
46 Correct 245 ms 40492 KB Output is correct
47 Correct 221 ms 40876 KB Output is correct
48 Correct 236 ms 40860 KB Output is correct
49 Correct 226 ms 36132 KB Output is correct
50 Correct 267 ms 44320 KB Output is correct
51 Correct 191 ms 41068 KB Output is correct
52 Correct 186 ms 40488 KB Output is correct
53 Correct 4 ms 9660 KB Output is correct
54 Runtime error 174 ms 77084 KB Execution killed with signal 6
55 Halted 0 ms 0 KB -