답안 #796272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796272 2023-07-28T08:41:38 Z 이동현(#10071) Security Guard (JOI23_guard) C++17
37 / 100
250 ms 34368 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;

const int NS = (int)2e5 + 4;
int n, m, q;
int a[NS], srt[NS], chk[NS];
vector<int> way[NS], way2[NS];

struct Dsu{
    int n;
    vector<int> pr;
    Dsu(int m){
        n = m + 4;
        pr.resize(n);
        iota(pr.begin(), pr.end(), 0);
    }

    int fd(int x){
        return (x == pr[x] ? x : pr[x] = fd(pr[x]));
    }

    bool unite(int x, int y){
        x = fd(x), y = fd(y);
        if(x == y){
            return false;
        }
        pr[y] = x;
        return true;
    }
};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m >> q;
    int ans = 0;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for(int i = 1; i <= m; ++i){
        int x, y;
        cin >> x >> y;
        way2[x].push_back(y);
        way2[y].push_back(x);
        // ans += min(a[x], a[y]);
    }

    iota(srt, srt + NS, 0);
    sort(srt + 1, srt + n + 1, [&](int x, int y){return a[x] < a[y];});

    Dsu imsi(n + 4);
    for(int i = 1; i <= n; ++i){
        sort(way2[srt[i]].begin(), way2[srt[i]].end(), [&](int x, int y){return a[x] < a[y];});
        for(auto&nxt:way2[srt[i]]){
            if(a[srt[i]] >= a[nxt] && imsi.unite(srt[i], nxt)){
                // cout << srt[i] << ' ' << nxt << endl;
                way[srt[i]].push_back(nxt);
                way[nxt].push_back(srt[i]);
            }
        }
    }

    Dsu uni(n + 4);

    int gr = 0;
    for(int i = 1; i <= n; ++i){
        int now = srt[i];
        
        ++gr;
        chk[now] = 1;
        sort(way[now].begin(), way[now].end(), [&](int x, int y){return a[x] < a[y];});
        for(auto&nxt:way[now]){
            if(chk[nxt]){
                int rv = uni.unite(now, nxt);
                gr -= rv;
                if(rv) ans += a[nxt];

                // cout << now << ' ' << nxt << endl;
            }
        }

        // cout << now << ' ' << gr << endl;

        if(i < n) ans += gr * (a[srt[i + 1]] - a[srt[i]]);
    }

    cout << ans << '\n';
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 85 ms 29964 KB Output is correct
3 Correct 87 ms 29988 KB Output is correct
4 Correct 99 ms 30000 KB Output is correct
5 Correct 100 ms 29948 KB Output is correct
6 Correct 98 ms 29960 KB Output is correct
7 Correct 96 ms 29952 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 85 ms 29964 KB Output is correct
3 Correct 87 ms 29988 KB Output is correct
4 Correct 99 ms 30000 KB Output is correct
5 Correct 100 ms 29948 KB Output is correct
6 Correct 98 ms 29960 KB Output is correct
7 Correct 96 ms 29952 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
9 Correct 7 ms 11220 KB Output is correct
10 Correct 168 ms 30024 KB Output is correct
11 Correct 93 ms 30036 KB Output is correct
12 Correct 93 ms 29980 KB Output is correct
13 Correct 108 ms 29948 KB Output is correct
14 Correct 173 ms 29996 KB Output is correct
15 Correct 167 ms 30040 KB Output is correct
16 Correct 182 ms 30020 KB Output is correct
17 Correct 162 ms 30028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 85 ms 29964 KB Output is correct
3 Correct 87 ms 29988 KB Output is correct
4 Correct 99 ms 30000 KB Output is correct
5 Correct 100 ms 29948 KB Output is correct
6 Correct 98 ms 29960 KB Output is correct
7 Correct 96 ms 29952 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
9 Correct 7 ms 11220 KB Output is correct
10 Correct 168 ms 30024 KB Output is correct
11 Correct 93 ms 30036 KB Output is correct
12 Correct 93 ms 29980 KB Output is correct
13 Correct 108 ms 29948 KB Output is correct
14 Correct 173 ms 29996 KB Output is correct
15 Correct 167 ms 30040 KB Output is correct
16 Correct 182 ms 30020 KB Output is correct
17 Correct 162 ms 30028 KB Output is correct
18 Correct 5 ms 11220 KB Output is correct
19 Correct 238 ms 31632 KB Output is correct
20 Correct 209 ms 31568 KB Output is correct
21 Correct 210 ms 31860 KB Output is correct
22 Correct 232 ms 32528 KB Output is correct
23 Correct 250 ms 33580 KB Output is correct
24 Correct 201 ms 33900 KB Output is correct
25 Correct 197 ms 33204 KB Output is correct
26 Correct 183 ms 33108 KB Output is correct
27 Correct 177 ms 33112 KB Output is correct
28 Correct 246 ms 30736 KB Output is correct
29 Correct 246 ms 31324 KB Output is correct
30 Correct 188 ms 32716 KB Output is correct
31 Correct 170 ms 33084 KB Output is correct
32 Correct 214 ms 33280 KB Output is correct
33 Correct 169 ms 34184 KB Output is correct
34 Correct 177 ms 32424 KB Output is correct
35 Correct 202 ms 34368 KB Output is correct
36 Correct 144 ms 34124 KB Output is correct
37 Correct 147 ms 34120 KB Output is correct
38 Correct 157 ms 33576 KB Output is correct
39 Correct 180 ms 32700 KB Output is correct
40 Correct 159 ms 33052 KB Output is correct
41 Correct 169 ms 29972 KB Output is correct
42 Correct 165 ms 29952 KB Output is correct
43 Correct 232 ms 32464 KB Output is correct
44 Correct 202 ms 32520 KB Output is correct
45 Correct 178 ms 32388 KB Output is correct
46 Correct 205 ms 32396 KB Output is correct
47 Correct 193 ms 32504 KB Output is correct
48 Correct 204 ms 32460 KB Output is correct
49 Correct 201 ms 32552 KB Output is correct
50 Correct 177 ms 32436 KB Output is correct
51 Correct 168 ms 32424 KB Output is correct
52 Correct 179 ms 32448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 85 ms 29964 KB Output is correct
3 Correct 87 ms 29988 KB Output is correct
4 Correct 99 ms 30000 KB Output is correct
5 Correct 100 ms 29948 KB Output is correct
6 Correct 98 ms 29960 KB Output is correct
7 Correct 96 ms 29952 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
9 Correct 7 ms 11220 KB Output is correct
10 Correct 168 ms 30024 KB Output is correct
11 Correct 93 ms 30036 KB Output is correct
12 Correct 93 ms 29980 KB Output is correct
13 Correct 108 ms 29948 KB Output is correct
14 Correct 173 ms 29996 KB Output is correct
15 Correct 167 ms 30040 KB Output is correct
16 Correct 182 ms 30020 KB Output is correct
17 Correct 162 ms 30028 KB Output is correct
18 Correct 5 ms 11220 KB Output is correct
19 Correct 238 ms 31632 KB Output is correct
20 Correct 209 ms 31568 KB Output is correct
21 Correct 210 ms 31860 KB Output is correct
22 Correct 232 ms 32528 KB Output is correct
23 Correct 250 ms 33580 KB Output is correct
24 Correct 201 ms 33900 KB Output is correct
25 Correct 197 ms 33204 KB Output is correct
26 Correct 183 ms 33108 KB Output is correct
27 Correct 177 ms 33112 KB Output is correct
28 Correct 246 ms 30736 KB Output is correct
29 Correct 246 ms 31324 KB Output is correct
30 Correct 188 ms 32716 KB Output is correct
31 Correct 170 ms 33084 KB Output is correct
32 Correct 214 ms 33280 KB Output is correct
33 Correct 169 ms 34184 KB Output is correct
34 Correct 177 ms 32424 KB Output is correct
35 Correct 202 ms 34368 KB Output is correct
36 Correct 144 ms 34124 KB Output is correct
37 Correct 147 ms 34120 KB Output is correct
38 Correct 157 ms 33576 KB Output is correct
39 Correct 180 ms 32700 KB Output is correct
40 Correct 159 ms 33052 KB Output is correct
41 Correct 169 ms 29972 KB Output is correct
42 Correct 165 ms 29952 KB Output is correct
43 Correct 232 ms 32464 KB Output is correct
44 Correct 202 ms 32520 KB Output is correct
45 Correct 178 ms 32388 KB Output is correct
46 Correct 205 ms 32396 KB Output is correct
47 Correct 193 ms 32504 KB Output is correct
48 Correct 204 ms 32460 KB Output is correct
49 Correct 201 ms 32552 KB Output is correct
50 Correct 177 ms 32436 KB Output is correct
51 Correct 168 ms 32424 KB Output is correct
52 Correct 179 ms 32448 KB Output is correct
53 Correct 5 ms 11220 KB Output is correct
54 Incorrect 224 ms 31800 KB Output isn't correct
55 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Incorrect 5 ms 11220 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Incorrect 5 ms 11220 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 85 ms 29964 KB Output is correct
3 Correct 87 ms 29988 KB Output is correct
4 Correct 99 ms 30000 KB Output is correct
5 Correct 100 ms 29948 KB Output is correct
6 Correct 98 ms 29960 KB Output is correct
7 Correct 96 ms 29952 KB Output is correct
8 Correct 5 ms 11220 KB Output is correct
9 Correct 7 ms 11220 KB Output is correct
10 Correct 168 ms 30024 KB Output is correct
11 Correct 93 ms 30036 KB Output is correct
12 Correct 93 ms 29980 KB Output is correct
13 Correct 108 ms 29948 KB Output is correct
14 Correct 173 ms 29996 KB Output is correct
15 Correct 167 ms 30040 KB Output is correct
16 Correct 182 ms 30020 KB Output is correct
17 Correct 162 ms 30028 KB Output is correct
18 Correct 5 ms 11220 KB Output is correct
19 Correct 238 ms 31632 KB Output is correct
20 Correct 209 ms 31568 KB Output is correct
21 Correct 210 ms 31860 KB Output is correct
22 Correct 232 ms 32528 KB Output is correct
23 Correct 250 ms 33580 KB Output is correct
24 Correct 201 ms 33900 KB Output is correct
25 Correct 197 ms 33204 KB Output is correct
26 Correct 183 ms 33108 KB Output is correct
27 Correct 177 ms 33112 KB Output is correct
28 Correct 246 ms 30736 KB Output is correct
29 Correct 246 ms 31324 KB Output is correct
30 Correct 188 ms 32716 KB Output is correct
31 Correct 170 ms 33084 KB Output is correct
32 Correct 214 ms 33280 KB Output is correct
33 Correct 169 ms 34184 KB Output is correct
34 Correct 177 ms 32424 KB Output is correct
35 Correct 202 ms 34368 KB Output is correct
36 Correct 144 ms 34124 KB Output is correct
37 Correct 147 ms 34120 KB Output is correct
38 Correct 157 ms 33576 KB Output is correct
39 Correct 180 ms 32700 KB Output is correct
40 Correct 159 ms 33052 KB Output is correct
41 Correct 169 ms 29972 KB Output is correct
42 Correct 165 ms 29952 KB Output is correct
43 Correct 232 ms 32464 KB Output is correct
44 Correct 202 ms 32520 KB Output is correct
45 Correct 178 ms 32388 KB Output is correct
46 Correct 205 ms 32396 KB Output is correct
47 Correct 193 ms 32504 KB Output is correct
48 Correct 204 ms 32460 KB Output is correct
49 Correct 201 ms 32552 KB Output is correct
50 Correct 177 ms 32436 KB Output is correct
51 Correct 168 ms 32424 KB Output is correct
52 Correct 179 ms 32448 KB Output is correct
53 Correct 5 ms 11220 KB Output is correct
54 Incorrect 224 ms 31800 KB Output isn't correct
55 Halted 0 ms 0 KB -