Submission #796136

# Submission time Handle Problem Language Result Execution time Memory
796136 2023-07-28T06:52:17 Z 반딧불(#10068) Security Guard (JOI23_guard) C++17
37 / 100
292 ms 54984 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

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

    void init(int n){
        for(int i=1; i<=n; i++){
            par[i] = i;
            groupAdd[200002] = oneAdd[200002] = 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){}
};

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

bool used[200002];
bool chk[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<pair<ll, int> > reqVec; /// � �������� �������� �󸶳� �ʿ������� ����� ��
        used[x] = 1;
        for(auto y: link[x]){
            if(!used[y]) continue;
            reqVec.push_back(make_pair(max(arr[y], arr[x] - dsu.query(y)), y));
        }
        sort(reqVec.begin(), reqVec.end());

        ll allAdd = 0;
        for(auto [w, y]: reqVec){
            if(chk[dsu.find(y)]) continue;
            chk[dsu.find(y)] = 1;
            ans += w;
            if(w > arr[y]) dsu.add(y, w-arr[y]); /// y �ʿ� �̵��� �ִ�
            else{
                ll tmp = arr[y] + dsu.query(y) - arr[x];
                allAdd += tmp;
                dsu.add(y, -tmp);
            }
        }
        for(auto [w, y]: reqVec) dsu.merge(x, y), chk[dsu.find(y)] = 0;
        dsu.add(x, allAdd);
    }

    printf("%lld", ans);
}

Compilation message

guard.cpp: In function 'int main()':
guard.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |     scanf("%d %d %d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
guard.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         scanf("%lld", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
guard.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
guard.cpp:15:45: warning: array subscript 200002 is above array bounds of 'll [200002]' {aka 'long long int [200002]'} [-Warray-bounds]
   15 |             groupAdd[200002] = oneAdd[200002] = 0;
      |                                ~~~~~~~~~~~~~^
guard.cpp:9:26: note: while referencing 'unionFind::oneAdd'
    9 |     ll groupAdd[200002], oneAdd[200002];
      |                          ^~~~~~
guard.cpp:45:3: note: defined here 'dsu'
   45 | } dsu;
      |   ^~~
guard.cpp:15:28: warning: array subscript 200002 is above array bounds of 'll [200002]' {aka 'long long int [200002]'} [-Warray-bounds]
   15 |             groupAdd[200002] = oneAdd[200002] = 0;
      |             ~~~~~~~~~~~~~~~^
guard.cpp:9:8: note: while referencing 'unionFind::groupAdd'
    9 |     ll groupAdd[200002], oneAdd[200002];
      |        ^~~~~~~~
guard.cpp:45:3: note: defined here 'dsu'
   45 | } dsu;
      |   ^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 112 ms 37764 KB Output is correct
3 Correct 94 ms 37776 KB Output is correct
4 Correct 114 ms 38640 KB Output is correct
5 Correct 107 ms 38624 KB Output is correct
6 Correct 108 ms 38572 KB Output is correct
7 Correct 108 ms 38560 KB Output is correct
8 Correct 4 ms 9692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 112 ms 37764 KB Output is correct
3 Correct 94 ms 37776 KB Output is correct
4 Correct 114 ms 38640 KB Output is correct
5 Correct 107 ms 38624 KB Output is correct
6 Correct 108 ms 38572 KB Output is correct
7 Correct 108 ms 38560 KB Output is correct
8 Correct 4 ms 9692 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 209 ms 50692 KB Output is correct
11 Correct 103 ms 37668 KB Output is correct
12 Correct 100 ms 37696 KB Output is correct
13 Correct 94 ms 37848 KB Output is correct
14 Correct 243 ms 50116 KB Output is correct
15 Correct 220 ms 51796 KB Output is correct
16 Correct 200 ms 51080 KB Output is correct
17 Correct 230 ms 50696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 112 ms 37764 KB Output is correct
3 Correct 94 ms 37776 KB Output is correct
4 Correct 114 ms 38640 KB Output is correct
5 Correct 107 ms 38624 KB Output is correct
6 Correct 108 ms 38572 KB Output is correct
7 Correct 108 ms 38560 KB Output is correct
8 Correct 4 ms 9692 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 209 ms 50692 KB Output is correct
11 Correct 103 ms 37668 KB Output is correct
12 Correct 100 ms 37696 KB Output is correct
13 Correct 94 ms 37848 KB Output is correct
14 Correct 243 ms 50116 KB Output is correct
15 Correct 220 ms 51796 KB Output is correct
16 Correct 200 ms 51080 KB Output is correct
17 Correct 230 ms 50696 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 279 ms 49812 KB Output is correct
20 Correct 249 ms 49100 KB Output is correct
21 Correct 292 ms 45788 KB Output is correct
22 Correct 239 ms 42020 KB Output is correct
23 Correct 204 ms 43060 KB Output is correct
24 Correct 238 ms 40376 KB Output is correct
25 Correct 206 ms 39620 KB Output is correct
26 Correct 158 ms 40984 KB Output is correct
27 Correct 152 ms 41444 KB Output is correct
28 Correct 274 ms 49136 KB Output is correct
29 Correct 210 ms 46604 KB Output is correct
30 Correct 204 ms 40788 KB Output is correct
31 Correct 161 ms 41420 KB Output is correct
32 Correct 206 ms 38560 KB Output is correct
33 Correct 225 ms 54984 KB Output is correct
34 Correct 179 ms 42888 KB Output is correct
35 Correct 170 ms 41148 KB Output is correct
36 Correct 149 ms 38980 KB Output is correct
37 Correct 145 ms 38956 KB Output is correct
38 Correct 188 ms 46124 KB Output is correct
39 Correct 177 ms 38136 KB Output is correct
40 Correct 187 ms 44652 KB Output is correct
41 Correct 212 ms 37728 KB Output is correct
42 Correct 210 ms 37676 KB Output is correct
43 Correct 203 ms 40360 KB Output is correct
44 Correct 230 ms 40296 KB Output is correct
45 Correct 222 ms 41240 KB Output is correct
46 Correct 204 ms 40300 KB Output is correct
47 Correct 233 ms 41012 KB Output is correct
48 Correct 263 ms 40992 KB Output is correct
49 Correct 221 ms 37900 KB Output is correct
50 Correct 246 ms 48344 KB Output is correct
51 Correct 236 ms 41780 KB Output is correct
52 Correct 165 ms 40488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 112 ms 37764 KB Output is correct
3 Correct 94 ms 37776 KB Output is correct
4 Correct 114 ms 38640 KB Output is correct
5 Correct 107 ms 38624 KB Output is correct
6 Correct 108 ms 38572 KB Output is correct
7 Correct 108 ms 38560 KB Output is correct
8 Correct 4 ms 9692 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 209 ms 50692 KB Output is correct
11 Correct 103 ms 37668 KB Output is correct
12 Correct 100 ms 37696 KB Output is correct
13 Correct 94 ms 37848 KB Output is correct
14 Correct 243 ms 50116 KB Output is correct
15 Correct 220 ms 51796 KB Output is correct
16 Correct 200 ms 51080 KB Output is correct
17 Correct 230 ms 50696 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 279 ms 49812 KB Output is correct
20 Correct 249 ms 49100 KB Output is correct
21 Correct 292 ms 45788 KB Output is correct
22 Correct 239 ms 42020 KB Output is correct
23 Correct 204 ms 43060 KB Output is correct
24 Correct 238 ms 40376 KB Output is correct
25 Correct 206 ms 39620 KB Output is correct
26 Correct 158 ms 40984 KB Output is correct
27 Correct 152 ms 41444 KB Output is correct
28 Correct 274 ms 49136 KB Output is correct
29 Correct 210 ms 46604 KB Output is correct
30 Correct 204 ms 40788 KB Output is correct
31 Correct 161 ms 41420 KB Output is correct
32 Correct 206 ms 38560 KB Output is correct
33 Correct 225 ms 54984 KB Output is correct
34 Correct 179 ms 42888 KB Output is correct
35 Correct 170 ms 41148 KB Output is correct
36 Correct 149 ms 38980 KB Output is correct
37 Correct 145 ms 38956 KB Output is correct
38 Correct 188 ms 46124 KB Output is correct
39 Correct 177 ms 38136 KB Output is correct
40 Correct 187 ms 44652 KB Output is correct
41 Correct 212 ms 37728 KB Output is correct
42 Correct 210 ms 37676 KB Output is correct
43 Correct 203 ms 40360 KB Output is correct
44 Correct 230 ms 40296 KB Output is correct
45 Correct 222 ms 41240 KB Output is correct
46 Correct 204 ms 40300 KB Output is correct
47 Correct 233 ms 41012 KB Output is correct
48 Correct 263 ms 40992 KB Output is correct
49 Correct 221 ms 37900 KB Output is correct
50 Correct 246 ms 48344 KB Output is correct
51 Correct 236 ms 41780 KB Output is correct
52 Correct 165 ms 40488 KB Output is correct
53 Correct 4 ms 9684 KB Output is correct
54 Incorrect 266 ms 48020 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 4 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 4 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 112 ms 37764 KB Output is correct
3 Correct 94 ms 37776 KB Output is correct
4 Correct 114 ms 38640 KB Output is correct
5 Correct 107 ms 38624 KB Output is correct
6 Correct 108 ms 38572 KB Output is correct
7 Correct 108 ms 38560 KB Output is correct
8 Correct 4 ms 9692 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 209 ms 50692 KB Output is correct
11 Correct 103 ms 37668 KB Output is correct
12 Correct 100 ms 37696 KB Output is correct
13 Correct 94 ms 37848 KB Output is correct
14 Correct 243 ms 50116 KB Output is correct
15 Correct 220 ms 51796 KB Output is correct
16 Correct 200 ms 51080 KB Output is correct
17 Correct 230 ms 50696 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 279 ms 49812 KB Output is correct
20 Correct 249 ms 49100 KB Output is correct
21 Correct 292 ms 45788 KB Output is correct
22 Correct 239 ms 42020 KB Output is correct
23 Correct 204 ms 43060 KB Output is correct
24 Correct 238 ms 40376 KB Output is correct
25 Correct 206 ms 39620 KB Output is correct
26 Correct 158 ms 40984 KB Output is correct
27 Correct 152 ms 41444 KB Output is correct
28 Correct 274 ms 49136 KB Output is correct
29 Correct 210 ms 46604 KB Output is correct
30 Correct 204 ms 40788 KB Output is correct
31 Correct 161 ms 41420 KB Output is correct
32 Correct 206 ms 38560 KB Output is correct
33 Correct 225 ms 54984 KB Output is correct
34 Correct 179 ms 42888 KB Output is correct
35 Correct 170 ms 41148 KB Output is correct
36 Correct 149 ms 38980 KB Output is correct
37 Correct 145 ms 38956 KB Output is correct
38 Correct 188 ms 46124 KB Output is correct
39 Correct 177 ms 38136 KB Output is correct
40 Correct 187 ms 44652 KB Output is correct
41 Correct 212 ms 37728 KB Output is correct
42 Correct 210 ms 37676 KB Output is correct
43 Correct 203 ms 40360 KB Output is correct
44 Correct 230 ms 40296 KB Output is correct
45 Correct 222 ms 41240 KB Output is correct
46 Correct 204 ms 40300 KB Output is correct
47 Correct 233 ms 41012 KB Output is correct
48 Correct 263 ms 40992 KB Output is correct
49 Correct 221 ms 37900 KB Output is correct
50 Correct 246 ms 48344 KB Output is correct
51 Correct 236 ms 41780 KB Output is correct
52 Correct 165 ms 40488 KB Output is correct
53 Correct 4 ms 9684 KB Output is correct
54 Incorrect 266 ms 48020 KB Output isn't correct
55 Halted 0 ms 0 KB -