Submission #796201

# Submission time Handle Problem Language Result Execution time Memory
796201 2023-07-28T07:46:16 Z 반딧불(#10068) Security Guard (JOI23_guard) C++17
37 / 100
315 ms 47468 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);
        if(x==y) return;
        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];

int main(){
    scanf("%d %d %d", &n, &m, &q);
    for(int i=1; i<=n; i++){
        scanf("%lld", &arr[i]);
    }
    for(int i=1; i<=m; i++){
        int x, y;
        scanf("%d %d", &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;
            dsu.merge(x, y);
        }
        dsu.add(x, allAdd);
    }

    printf("%lld", ans);
}

Compilation message

guard.cpp: In function 'int main()':
guard.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%d %d %d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
guard.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         scanf("%lld", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
guard.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 92 ms 35908 KB Output is correct
3 Correct 94 ms 35996 KB Output is correct
4 Correct 113 ms 39408 KB Output is correct
5 Correct 133 ms 39368 KB Output is correct
6 Correct 107 ms 39336 KB Output is correct
7 Correct 108 ms 39448 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 92 ms 35908 KB Output is correct
3 Correct 94 ms 35996 KB Output is correct
4 Correct 113 ms 39408 KB Output is correct
5 Correct 133 ms 39368 KB Output is correct
6 Correct 107 ms 39336 KB Output is correct
7 Correct 108 ms 39448 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 217 ms 45580 KB Output is correct
11 Correct 103 ms 35940 KB Output is correct
12 Correct 97 ms 35904 KB Output is correct
13 Correct 97 ms 36248 KB Output is correct
14 Correct 208 ms 45324 KB Output is correct
15 Correct 204 ms 46164 KB Output is correct
16 Correct 201 ms 45728 KB Output is correct
17 Correct 252 ms 45564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 92 ms 35908 KB Output is correct
3 Correct 94 ms 35996 KB Output is correct
4 Correct 113 ms 39408 KB Output is correct
5 Correct 133 ms 39368 KB Output is correct
6 Correct 107 ms 39336 KB Output is correct
7 Correct 108 ms 39448 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 217 ms 45580 KB Output is correct
11 Correct 103 ms 35940 KB Output is correct
12 Correct 97 ms 35904 KB Output is correct
13 Correct 97 ms 36248 KB Output is correct
14 Correct 208 ms 45324 KB Output is correct
15 Correct 204 ms 46164 KB Output is correct
16 Correct 201 ms 45728 KB Output is correct
17 Correct 252 ms 45564 KB Output is correct
18 Correct 6 ms 9732 KB Output is correct
19 Correct 283 ms 45260 KB Output is correct
20 Correct 245 ms 44844 KB Output is correct
21 Correct 315 ms 43076 KB Output is correct
22 Correct 226 ms 41212 KB Output is correct
23 Correct 203 ms 42324 KB Output is correct
24 Correct 212 ms 41272 KB Output is correct
25 Correct 231 ms 41416 KB Output is correct
26 Correct 180 ms 43820 KB Output is correct
27 Correct 186 ms 45600 KB Output is correct
28 Correct 267 ms 44992 KB Output is correct
29 Correct 220 ms 43756 KB Output is correct
30 Correct 218 ms 41312 KB Output is correct
31 Correct 187 ms 45652 KB Output is correct
32 Correct 211 ms 39764 KB Output is correct
33 Correct 238 ms 47468 KB Output is correct
34 Correct 197 ms 41812 KB Output is correct
35 Correct 184 ms 41456 KB Output is correct
36 Correct 161 ms 40280 KB Output is correct
37 Correct 160 ms 40336 KB Output is correct
38 Correct 200 ms 44140 KB Output is correct
39 Correct 175 ms 37024 KB Output is correct
40 Correct 213 ms 43944 KB Output is correct
41 Correct 261 ms 35968 KB Output is correct
42 Correct 258 ms 35936 KB Output is correct
43 Correct 237 ms 40496 KB Output is correct
44 Correct 229 ms 40424 KB Output is correct
45 Correct 229 ms 40892 KB Output is correct
46 Correct 249 ms 40388 KB Output is correct
47 Correct 221 ms 40836 KB Output is correct
48 Correct 256 ms 40808 KB Output is correct
49 Correct 225 ms 36144 KB Output is correct
50 Correct 228 ms 44232 KB Output is correct
51 Correct 188 ms 41040 KB Output is correct
52 Correct 178 ms 40516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 92 ms 35908 KB Output is correct
3 Correct 94 ms 35996 KB Output is correct
4 Correct 113 ms 39408 KB Output is correct
5 Correct 133 ms 39368 KB Output is correct
6 Correct 107 ms 39336 KB Output is correct
7 Correct 108 ms 39448 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 217 ms 45580 KB Output is correct
11 Correct 103 ms 35940 KB Output is correct
12 Correct 97 ms 35904 KB Output is correct
13 Correct 97 ms 36248 KB Output is correct
14 Correct 208 ms 45324 KB Output is correct
15 Correct 204 ms 46164 KB Output is correct
16 Correct 201 ms 45728 KB Output is correct
17 Correct 252 ms 45564 KB Output is correct
18 Correct 6 ms 9732 KB Output is correct
19 Correct 283 ms 45260 KB Output is correct
20 Correct 245 ms 44844 KB Output is correct
21 Correct 315 ms 43076 KB Output is correct
22 Correct 226 ms 41212 KB Output is correct
23 Correct 203 ms 42324 KB Output is correct
24 Correct 212 ms 41272 KB Output is correct
25 Correct 231 ms 41416 KB Output is correct
26 Correct 180 ms 43820 KB Output is correct
27 Correct 186 ms 45600 KB Output is correct
28 Correct 267 ms 44992 KB Output is correct
29 Correct 220 ms 43756 KB Output is correct
30 Correct 218 ms 41312 KB Output is correct
31 Correct 187 ms 45652 KB Output is correct
32 Correct 211 ms 39764 KB Output is correct
33 Correct 238 ms 47468 KB Output is correct
34 Correct 197 ms 41812 KB Output is correct
35 Correct 184 ms 41456 KB Output is correct
36 Correct 161 ms 40280 KB Output is correct
37 Correct 160 ms 40336 KB Output is correct
38 Correct 200 ms 44140 KB Output is correct
39 Correct 175 ms 37024 KB Output is correct
40 Correct 213 ms 43944 KB Output is correct
41 Correct 261 ms 35968 KB Output is correct
42 Correct 258 ms 35936 KB Output is correct
43 Correct 237 ms 40496 KB Output is correct
44 Correct 229 ms 40424 KB Output is correct
45 Correct 229 ms 40892 KB Output is correct
46 Correct 249 ms 40388 KB Output is correct
47 Correct 221 ms 40836 KB Output is correct
48 Correct 256 ms 40808 KB Output is correct
49 Correct 225 ms 36144 KB Output is correct
50 Correct 228 ms 44232 KB Output is correct
51 Correct 188 ms 41040 KB Output is correct
52 Correct 178 ms 40516 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Incorrect 242 ms 44320 KB Output isn't correct
55 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 5 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 5 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 92 ms 35908 KB Output is correct
3 Correct 94 ms 35996 KB Output is correct
4 Correct 113 ms 39408 KB Output is correct
5 Correct 133 ms 39368 KB Output is correct
6 Correct 107 ms 39336 KB Output is correct
7 Correct 108 ms 39448 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 217 ms 45580 KB Output is correct
11 Correct 103 ms 35940 KB Output is correct
12 Correct 97 ms 35904 KB Output is correct
13 Correct 97 ms 36248 KB Output is correct
14 Correct 208 ms 45324 KB Output is correct
15 Correct 204 ms 46164 KB Output is correct
16 Correct 201 ms 45728 KB Output is correct
17 Correct 252 ms 45564 KB Output is correct
18 Correct 6 ms 9732 KB Output is correct
19 Correct 283 ms 45260 KB Output is correct
20 Correct 245 ms 44844 KB Output is correct
21 Correct 315 ms 43076 KB Output is correct
22 Correct 226 ms 41212 KB Output is correct
23 Correct 203 ms 42324 KB Output is correct
24 Correct 212 ms 41272 KB Output is correct
25 Correct 231 ms 41416 KB Output is correct
26 Correct 180 ms 43820 KB Output is correct
27 Correct 186 ms 45600 KB Output is correct
28 Correct 267 ms 44992 KB Output is correct
29 Correct 220 ms 43756 KB Output is correct
30 Correct 218 ms 41312 KB Output is correct
31 Correct 187 ms 45652 KB Output is correct
32 Correct 211 ms 39764 KB Output is correct
33 Correct 238 ms 47468 KB Output is correct
34 Correct 197 ms 41812 KB Output is correct
35 Correct 184 ms 41456 KB Output is correct
36 Correct 161 ms 40280 KB Output is correct
37 Correct 160 ms 40336 KB Output is correct
38 Correct 200 ms 44140 KB Output is correct
39 Correct 175 ms 37024 KB Output is correct
40 Correct 213 ms 43944 KB Output is correct
41 Correct 261 ms 35968 KB Output is correct
42 Correct 258 ms 35936 KB Output is correct
43 Correct 237 ms 40496 KB Output is correct
44 Correct 229 ms 40424 KB Output is correct
45 Correct 229 ms 40892 KB Output is correct
46 Correct 249 ms 40388 KB Output is correct
47 Correct 221 ms 40836 KB Output is correct
48 Correct 256 ms 40808 KB Output is correct
49 Correct 225 ms 36144 KB Output is correct
50 Correct 228 ms 44232 KB Output is correct
51 Correct 188 ms 41040 KB Output is correct
52 Correct 178 ms 40516 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Incorrect 242 ms 44320 KB Output isn't correct
55 Halted 0 ms 0 KB -