답안 #796124

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796124 2023-07-28T06:43:41 Z 반딧불(#10068) Security Guard (JOI23_guard) C++17
37 / 100
299 ms 54712 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;

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

bool used[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<int, ll> > 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<int, ll> > 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){
            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);
        dsu.add(x, allAdd);
    }

    printf("%lld", ans);
}

Compilation message

guard.cpp: In function 'int main()':
guard.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |     scanf("%d %d %d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
guard.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         scanf("%lld", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
guard.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         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;
      |   ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 93 ms 37740 KB Output is correct
3 Correct 92 ms 37756 KB Output is correct
4 Correct 105 ms 38404 KB Output is correct
5 Correct 106 ms 38468 KB Output is correct
6 Correct 104 ms 38356 KB Output is correct
7 Correct 112 ms 38408 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 93 ms 37740 KB Output is correct
3 Correct 92 ms 37756 KB Output is correct
4 Correct 105 ms 38404 KB Output is correct
5 Correct 106 ms 38468 KB Output is correct
6 Correct 104 ms 38356 KB Output is correct
7 Correct 112 ms 38408 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 197 ms 50396 KB Output is correct
11 Correct 97 ms 37684 KB Output is correct
12 Correct 98 ms 37772 KB Output is correct
13 Correct 93 ms 37684 KB Output is correct
14 Correct 192 ms 49936 KB Output is correct
15 Correct 239 ms 51600 KB Output is correct
16 Correct 209 ms 50920 KB Output is correct
17 Correct 197 ms 50628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 93 ms 37740 KB Output is correct
3 Correct 92 ms 37756 KB Output is correct
4 Correct 105 ms 38404 KB Output is correct
5 Correct 106 ms 38468 KB Output is correct
6 Correct 104 ms 38356 KB Output is correct
7 Correct 112 ms 38408 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 197 ms 50396 KB Output is correct
11 Correct 97 ms 37684 KB Output is correct
12 Correct 98 ms 37772 KB Output is correct
13 Correct 93 ms 37684 KB Output is correct
14 Correct 192 ms 49936 KB Output is correct
15 Correct 239 ms 51600 KB Output is correct
16 Correct 209 ms 50920 KB Output is correct
17 Correct 197 ms 50628 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 257 ms 49688 KB Output is correct
20 Correct 247 ms 49012 KB Output is correct
21 Correct 226 ms 45628 KB Output is correct
22 Correct 217 ms 41808 KB Output is correct
23 Correct 205 ms 42796 KB Output is correct
24 Correct 184 ms 40244 KB Output is correct
25 Correct 194 ms 39492 KB Output is correct
26 Correct 168 ms 40728 KB Output is correct
27 Correct 153 ms 41160 KB Output is correct
28 Correct 218 ms 49036 KB Output is correct
29 Correct 222 ms 46484 KB Output is correct
30 Correct 187 ms 40624 KB Output is correct
31 Correct 169 ms 41196 KB Output is correct
32 Correct 224 ms 38300 KB Output is correct
33 Correct 222 ms 54712 KB Output is correct
34 Correct 181 ms 42676 KB Output is correct
35 Correct 183 ms 40972 KB Output is correct
36 Correct 154 ms 38828 KB Output is correct
37 Correct 168 ms 38836 KB Output is correct
38 Correct 186 ms 45956 KB Output is correct
39 Correct 174 ms 38076 KB Output is correct
40 Correct 190 ms 44512 KB Output is correct
41 Correct 205 ms 37664 KB Output is correct
42 Correct 200 ms 37716 KB Output is correct
43 Correct 198 ms 40248 KB Output is correct
44 Correct 231 ms 40176 KB Output is correct
45 Correct 200 ms 41020 KB Output is correct
46 Correct 217 ms 40032 KB Output is correct
47 Correct 299 ms 40896 KB Output is correct
48 Correct 232 ms 40756 KB Output is correct
49 Correct 229 ms 38024 KB Output is correct
50 Correct 217 ms 48232 KB Output is correct
51 Correct 191 ms 41604 KB Output is correct
52 Correct 171 ms 40256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 93 ms 37740 KB Output is correct
3 Correct 92 ms 37756 KB Output is correct
4 Correct 105 ms 38404 KB Output is correct
5 Correct 106 ms 38468 KB Output is correct
6 Correct 104 ms 38356 KB Output is correct
7 Correct 112 ms 38408 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 197 ms 50396 KB Output is correct
11 Correct 97 ms 37684 KB Output is correct
12 Correct 98 ms 37772 KB Output is correct
13 Correct 93 ms 37684 KB Output is correct
14 Correct 192 ms 49936 KB Output is correct
15 Correct 239 ms 51600 KB Output is correct
16 Correct 209 ms 50920 KB Output is correct
17 Correct 197 ms 50628 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 257 ms 49688 KB Output is correct
20 Correct 247 ms 49012 KB Output is correct
21 Correct 226 ms 45628 KB Output is correct
22 Correct 217 ms 41808 KB Output is correct
23 Correct 205 ms 42796 KB Output is correct
24 Correct 184 ms 40244 KB Output is correct
25 Correct 194 ms 39492 KB Output is correct
26 Correct 168 ms 40728 KB Output is correct
27 Correct 153 ms 41160 KB Output is correct
28 Correct 218 ms 49036 KB Output is correct
29 Correct 222 ms 46484 KB Output is correct
30 Correct 187 ms 40624 KB Output is correct
31 Correct 169 ms 41196 KB Output is correct
32 Correct 224 ms 38300 KB Output is correct
33 Correct 222 ms 54712 KB Output is correct
34 Correct 181 ms 42676 KB Output is correct
35 Correct 183 ms 40972 KB Output is correct
36 Correct 154 ms 38828 KB Output is correct
37 Correct 168 ms 38836 KB Output is correct
38 Correct 186 ms 45956 KB Output is correct
39 Correct 174 ms 38076 KB Output is correct
40 Correct 190 ms 44512 KB Output is correct
41 Correct 205 ms 37664 KB Output is correct
42 Correct 200 ms 37716 KB Output is correct
43 Correct 198 ms 40248 KB Output is correct
44 Correct 231 ms 40176 KB Output is correct
45 Correct 200 ms 41020 KB Output is correct
46 Correct 217 ms 40032 KB Output is correct
47 Correct 299 ms 40896 KB Output is correct
48 Correct 232 ms 40756 KB Output is correct
49 Correct 229 ms 38024 KB Output is correct
50 Correct 217 ms 48232 KB Output is correct
51 Correct 191 ms 41604 KB Output is correct
52 Correct 171 ms 40256 KB Output is correct
53 Incorrect 4 ms 9684 KB Output isn't correct
54 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Incorrect 5 ms 9740 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Incorrect 5 ms 9740 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 93 ms 37740 KB Output is correct
3 Correct 92 ms 37756 KB Output is correct
4 Correct 105 ms 38404 KB Output is correct
5 Correct 106 ms 38468 KB Output is correct
6 Correct 104 ms 38356 KB Output is correct
7 Correct 112 ms 38408 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 197 ms 50396 KB Output is correct
11 Correct 97 ms 37684 KB Output is correct
12 Correct 98 ms 37772 KB Output is correct
13 Correct 93 ms 37684 KB Output is correct
14 Correct 192 ms 49936 KB Output is correct
15 Correct 239 ms 51600 KB Output is correct
16 Correct 209 ms 50920 KB Output is correct
17 Correct 197 ms 50628 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 257 ms 49688 KB Output is correct
20 Correct 247 ms 49012 KB Output is correct
21 Correct 226 ms 45628 KB Output is correct
22 Correct 217 ms 41808 KB Output is correct
23 Correct 205 ms 42796 KB Output is correct
24 Correct 184 ms 40244 KB Output is correct
25 Correct 194 ms 39492 KB Output is correct
26 Correct 168 ms 40728 KB Output is correct
27 Correct 153 ms 41160 KB Output is correct
28 Correct 218 ms 49036 KB Output is correct
29 Correct 222 ms 46484 KB Output is correct
30 Correct 187 ms 40624 KB Output is correct
31 Correct 169 ms 41196 KB Output is correct
32 Correct 224 ms 38300 KB Output is correct
33 Correct 222 ms 54712 KB Output is correct
34 Correct 181 ms 42676 KB Output is correct
35 Correct 183 ms 40972 KB Output is correct
36 Correct 154 ms 38828 KB Output is correct
37 Correct 168 ms 38836 KB Output is correct
38 Correct 186 ms 45956 KB Output is correct
39 Correct 174 ms 38076 KB Output is correct
40 Correct 190 ms 44512 KB Output is correct
41 Correct 205 ms 37664 KB Output is correct
42 Correct 200 ms 37716 KB Output is correct
43 Correct 198 ms 40248 KB Output is correct
44 Correct 231 ms 40176 KB Output is correct
45 Correct 200 ms 41020 KB Output is correct
46 Correct 217 ms 40032 KB Output is correct
47 Correct 299 ms 40896 KB Output is correct
48 Correct 232 ms 40756 KB Output is correct
49 Correct 229 ms 38024 KB Output is correct
50 Correct 217 ms 48232 KB Output is correct
51 Correct 191 ms 41604 KB Output is correct
52 Correct 171 ms 40256 KB Output is correct
53 Incorrect 4 ms 9684 KB Output isn't correct
54 Halted 0 ms 0 KB -