Submission #796277

# Submission time Handle Problem Language Result Execution time Memory
796277 2023-07-28T08:47:43 Z 이성호(#10070) Security Guard (JOI23_guard) C++17
58 / 100
300 ms 33404 KB
#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
vector<int> adj[200005];
pair<int, pair<int, int>> edge[400005];
int S[200005];
int N, M, Q;
struct UnionFind
{
    vector<int> par;
    UnionFind(int n){par.resize(n+1); for(int i=1;i<=n;i++) par[i] = i;}
    int fin(int i){return i==par[i]?i:(par[i] = fin(par[i]));}
    void uni(int u, int v){par[fin(u)] = fin(v);}
    bool isuni(int u, int v){return fin(u) == fin(v);}
};
using pii = pair<int, int>;
vector<int> gph[200005];
pair<int, int> now[200005];
pair<pii, pii> dp[200005];
pii dp2[200005];
int par[200005];
const int inf = 1e18;
void dfs(int v, int p)
{
    par[v] = p;
    dp[v] = make_pair(make_pair(S[v], v), make_pair(inf, -1));
    for (int i:gph[v]) {
        if (i != p) {
            dfs(i, v);
            if (dp[v].first > dp[i].first) {
                dp[v] = make_pair(dp[i].first, dp[v].first);
            }
            else {
                dp[v].second = min(dp[v].second, dp[i].first);
            }
        }
    }
}
void dfs2(int v, int p)
{
    if (v != 1) {
        dp2[v] = dp2[p];
        if (dp[v].first.first == dp[p].first.first) dp2[v] = min(dp2[v], dp[p].second);
        else dp2[v] = min(dp2[v], dp[p].first);
    }
    else {
        dp2[1] = make_pair(inf, -1);
    }
    for (int i:gph[v]) {
        if (i != p) {
            dfs2(i, v);
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> N >> M >> Q;
    for (int i = 1; i <= N; i++) cin >> S[i];
    for (int i = 0; i < M; i++) {
        int u, v; cin >> u >> v;
        edge[i] = make_pair(S[u] + S[v], make_pair(u, v));
    }
    int ans = 0;
    sort(edge, edge + M);
    UnionFind uf(N);
    int pv = 0;
    for (int i = 0; i < M; i++) {
        int u = edge[i].second.first, v = edge[i].second.second;
        if (!uf.isuni(u, v)) {
            uf.uni(u, v);
            ans += S[u] + S[v];
            gph[u].push_back(v);
            gph[v].push_back(u);
            now[pv++] = make_pair(u, v);
        }
    }
    int df = *max_element(S+1, S+N+1) - accumulate(S+1, S+N+1, 0LL);
    cout << ans + df << '\n';
    for (int tt = 0; tt < Q; tt++) {
        if (tt < N - 1) {
            dfs(1, 0);
            dfs2(1, 0);
            int cur = inf, id = -1;
            for (int k = 0; k < N - 1; k++) {
                int u = now[k].first, v = now[k].second;
                if (u == par[v]) swap(u, v);
                int dec = dp[u].first.first + dp2[u].first - S[u] - S[v];
                if (cur > dec) {
                    cur = dec;
                    id = k;
                }
            }
            int u = now[id].first, v = now[id].second;
            if (u == par[v]) swap(u, v);
            ans += cur;
            gph[u].erase(remove(gph[u].begin(), gph[u].end(), v), gph[u].end());
            gph[v].erase(remove(gph[v].begin(), gph[v].end(), u), gph[v].end());
            int nu = dp[u].first.second, nv = dp2[u].second;
            now[id] = make_pair(nu, nv);
            gph[nu].push_back(nv);
            gph[nv].push_back(nu);
        }
        cout << ans + df << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 60 ms 26824 KB Output is correct
3 Correct 60 ms 26876 KB Output is correct
4 Correct 69 ms 26912 KB Output is correct
5 Correct 69 ms 26904 KB Output is correct
6 Correct 71 ms 26832 KB Output is correct
7 Correct 74 ms 26872 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 60 ms 26824 KB Output is correct
3 Correct 60 ms 26876 KB Output is correct
4 Correct 69 ms 26912 KB Output is correct
5 Correct 69 ms 26904 KB Output is correct
6 Correct 71 ms 26832 KB Output is correct
7 Correct 74 ms 26872 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 93 ms 26884 KB Output is correct
11 Correct 68 ms 26832 KB Output is correct
12 Correct 70 ms 27068 KB Output is correct
13 Correct 66 ms 26868 KB Output is correct
14 Correct 92 ms 26884 KB Output is correct
15 Correct 93 ms 26860 KB Output is correct
16 Correct 93 ms 26852 KB Output is correct
17 Correct 93 ms 26828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 60 ms 26824 KB Output is correct
3 Correct 60 ms 26876 KB Output is correct
4 Correct 69 ms 26912 KB Output is correct
5 Correct 69 ms 26904 KB Output is correct
6 Correct 71 ms 26832 KB Output is correct
7 Correct 74 ms 26872 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 93 ms 26884 KB Output is correct
11 Correct 68 ms 26832 KB Output is correct
12 Correct 70 ms 27068 KB Output is correct
13 Correct 66 ms 26868 KB Output is correct
14 Correct 92 ms 26884 KB Output is correct
15 Correct 93 ms 26860 KB Output is correct
16 Correct 93 ms 26852 KB Output is correct
17 Correct 93 ms 26828 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 103 ms 27712 KB Output is correct
20 Correct 104 ms 27692 KB Output is correct
21 Correct 103 ms 27816 KB Output is correct
22 Correct 106 ms 28092 KB Output is correct
23 Correct 91 ms 28620 KB Output is correct
24 Correct 89 ms 28648 KB Output is correct
25 Correct 94 ms 28436 KB Output is correct
26 Correct 86 ms 28424 KB Output is correct
27 Correct 86 ms 28456 KB Output is correct
28 Correct 100 ms 27228 KB Output is correct
29 Correct 98 ms 27620 KB Output is correct
30 Correct 96 ms 28244 KB Output is correct
31 Correct 96 ms 28448 KB Output is correct
32 Correct 102 ms 28416 KB Output is correct
33 Correct 98 ms 29200 KB Output is correct
34 Correct 100 ms 27988 KB Output is correct
35 Correct 99 ms 29096 KB Output is correct
36 Correct 91 ms 28956 KB Output is correct
37 Correct 89 ms 28956 KB Output is correct
38 Correct 102 ms 28740 KB Output is correct
39 Correct 89 ms 28260 KB Output is correct
40 Correct 90 ms 28480 KB Output is correct
41 Correct 89 ms 26868 KB Output is correct
42 Correct 85 ms 26896 KB Output is correct
43 Correct 111 ms 28092 KB Output is correct
44 Correct 124 ms 28092 KB Output is correct
45 Correct 108 ms 28132 KB Output is correct
46 Correct 113 ms 28052 KB Output is correct
47 Correct 113 ms 28120 KB Output is correct
48 Correct 139 ms 28128 KB Output is correct
49 Correct 99 ms 28160 KB Output is correct
50 Correct 94 ms 28108 KB Output is correct
51 Correct 103 ms 28108 KB Output is correct
52 Correct 93 ms 28156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 60 ms 26824 KB Output is correct
3 Correct 60 ms 26876 KB Output is correct
4 Correct 69 ms 26912 KB Output is correct
5 Correct 69 ms 26904 KB Output is correct
6 Correct 71 ms 26832 KB Output is correct
7 Correct 74 ms 26872 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 93 ms 26884 KB Output is correct
11 Correct 68 ms 26832 KB Output is correct
12 Correct 70 ms 27068 KB Output is correct
13 Correct 66 ms 26868 KB Output is correct
14 Correct 92 ms 26884 KB Output is correct
15 Correct 93 ms 26860 KB Output is correct
16 Correct 93 ms 26852 KB Output is correct
17 Correct 93 ms 26828 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 103 ms 27712 KB Output is correct
20 Correct 104 ms 27692 KB Output is correct
21 Correct 103 ms 27816 KB Output is correct
22 Correct 106 ms 28092 KB Output is correct
23 Correct 91 ms 28620 KB Output is correct
24 Correct 89 ms 28648 KB Output is correct
25 Correct 94 ms 28436 KB Output is correct
26 Correct 86 ms 28424 KB Output is correct
27 Correct 86 ms 28456 KB Output is correct
28 Correct 100 ms 27228 KB Output is correct
29 Correct 98 ms 27620 KB Output is correct
30 Correct 96 ms 28244 KB Output is correct
31 Correct 96 ms 28448 KB Output is correct
32 Correct 102 ms 28416 KB Output is correct
33 Correct 98 ms 29200 KB Output is correct
34 Correct 100 ms 27988 KB Output is correct
35 Correct 99 ms 29096 KB Output is correct
36 Correct 91 ms 28956 KB Output is correct
37 Correct 89 ms 28956 KB Output is correct
38 Correct 102 ms 28740 KB Output is correct
39 Correct 89 ms 28260 KB Output is correct
40 Correct 90 ms 28480 KB Output is correct
41 Correct 89 ms 26868 KB Output is correct
42 Correct 85 ms 26896 KB Output is correct
43 Correct 111 ms 28092 KB Output is correct
44 Correct 124 ms 28092 KB Output is correct
45 Correct 108 ms 28132 KB Output is correct
46 Correct 113 ms 28052 KB Output is correct
47 Correct 113 ms 28120 KB Output is correct
48 Correct 139 ms 28128 KB Output is correct
49 Correct 99 ms 28160 KB Output is correct
50 Correct 94 ms 28108 KB Output is correct
51 Correct 103 ms 28108 KB Output is correct
52 Correct 93 ms 28156 KB Output is correct
53 Correct 4 ms 9684 KB Output is correct
54 Correct 105 ms 27932 KB Output is correct
55 Correct 126 ms 28044 KB Output is correct
56 Correct 120 ms 29328 KB Output is correct
57 Correct 156 ms 32768 KB Output is correct
58 Correct 142 ms 33404 KB Output is correct
59 Correct 146 ms 33332 KB Output is correct
60 Correct 115 ms 30764 KB Output is correct
61 Correct 146 ms 33132 KB Output is correct
62 Correct 138 ms 33084 KB Output is correct
63 Correct 104 ms 27500 KB Output is correct
64 Correct 133 ms 29732 KB Output is correct
65 Correct 147 ms 32892 KB Output is correct
66 Correct 142 ms 33096 KB Output is correct
67 Correct 152 ms 33160 KB Output is correct
68 Correct 102 ms 29280 KB Output is correct
69 Correct 137 ms 30956 KB Output is correct
70 Correct 104 ms 29756 KB Output is correct
71 Correct 90 ms 28956 KB Output is correct
72 Correct 93 ms 29012 KB Output is correct
73 Correct 100 ms 28888 KB Output is correct
74 Correct 90 ms 28248 KB Output is correct
75 Correct 93 ms 28512 KB Output is correct
76 Correct 86 ms 26912 KB Output is correct
77 Correct 85 ms 26892 KB Output is correct
78 Correct 120 ms 28480 KB Output is correct
79 Correct 163 ms 32736 KB Output is correct
80 Correct 121 ms 29436 KB Output is correct
81 Correct 149 ms 31812 KB Output is correct
82 Correct 159 ms 32828 KB Output is correct
83 Correct 160 ms 32800 KB Output is correct
84 Correct 155 ms 32824 KB Output is correct
85 Correct 95 ms 28104 KB Output is correct
86 Correct 101 ms 28108 KB Output is correct
87 Correct 106 ms 28160 KB Output is correct
88 Correct 163 ms 32800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9736 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9696 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 16 ms 11860 KB Output is correct
9 Correct 17 ms 11972 KB Output is correct
10 Correct 17 ms 11732 KB Output is correct
11 Correct 17 ms 11892 KB Output is correct
12 Correct 17 ms 11868 KB Output is correct
13 Correct 19 ms 11952 KB Output is correct
14 Correct 17 ms 11860 KB Output is correct
15 Correct 17 ms 11904 KB Output is correct
16 Correct 17 ms 11860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9736 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9696 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 16 ms 11860 KB Output is correct
9 Correct 17 ms 11972 KB Output is correct
10 Correct 17 ms 11732 KB Output is correct
11 Correct 17 ms 11892 KB Output is correct
12 Correct 17 ms 11868 KB Output is correct
13 Correct 19 ms 11952 KB Output is correct
14 Correct 17 ms 11860 KB Output is correct
15 Correct 17 ms 11904 KB Output is correct
16 Correct 17 ms 11860 KB Output is correct
17 Correct 264 ms 13084 KB Output is correct
18 Correct 289 ms 13100 KB Output is correct
19 Correct 279 ms 14280 KB Output is correct
20 Correct 300 ms 16416 KB Output is correct
21 Correct 201 ms 17484 KB Output is correct
22 Correct 196 ms 16600 KB Output is correct
23 Correct 230 ms 21756 KB Output is correct
24 Correct 248 ms 17996 KB Output is correct
25 Incorrect 40 ms 12572 KB Output isn't correct
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 60 ms 26824 KB Output is correct
3 Correct 60 ms 26876 KB Output is correct
4 Correct 69 ms 26912 KB Output is correct
5 Correct 69 ms 26904 KB Output is correct
6 Correct 71 ms 26832 KB Output is correct
7 Correct 74 ms 26872 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 93 ms 26884 KB Output is correct
11 Correct 68 ms 26832 KB Output is correct
12 Correct 70 ms 27068 KB Output is correct
13 Correct 66 ms 26868 KB Output is correct
14 Correct 92 ms 26884 KB Output is correct
15 Correct 93 ms 26860 KB Output is correct
16 Correct 93 ms 26852 KB Output is correct
17 Correct 93 ms 26828 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 103 ms 27712 KB Output is correct
20 Correct 104 ms 27692 KB Output is correct
21 Correct 103 ms 27816 KB Output is correct
22 Correct 106 ms 28092 KB Output is correct
23 Correct 91 ms 28620 KB Output is correct
24 Correct 89 ms 28648 KB Output is correct
25 Correct 94 ms 28436 KB Output is correct
26 Correct 86 ms 28424 KB Output is correct
27 Correct 86 ms 28456 KB Output is correct
28 Correct 100 ms 27228 KB Output is correct
29 Correct 98 ms 27620 KB Output is correct
30 Correct 96 ms 28244 KB Output is correct
31 Correct 96 ms 28448 KB Output is correct
32 Correct 102 ms 28416 KB Output is correct
33 Correct 98 ms 29200 KB Output is correct
34 Correct 100 ms 27988 KB Output is correct
35 Correct 99 ms 29096 KB Output is correct
36 Correct 91 ms 28956 KB Output is correct
37 Correct 89 ms 28956 KB Output is correct
38 Correct 102 ms 28740 KB Output is correct
39 Correct 89 ms 28260 KB Output is correct
40 Correct 90 ms 28480 KB Output is correct
41 Correct 89 ms 26868 KB Output is correct
42 Correct 85 ms 26896 KB Output is correct
43 Correct 111 ms 28092 KB Output is correct
44 Correct 124 ms 28092 KB Output is correct
45 Correct 108 ms 28132 KB Output is correct
46 Correct 113 ms 28052 KB Output is correct
47 Correct 113 ms 28120 KB Output is correct
48 Correct 139 ms 28128 KB Output is correct
49 Correct 99 ms 28160 KB Output is correct
50 Correct 94 ms 28108 KB Output is correct
51 Correct 103 ms 28108 KB Output is correct
52 Correct 93 ms 28156 KB Output is correct
53 Correct 4 ms 9684 KB Output is correct
54 Correct 105 ms 27932 KB Output is correct
55 Correct 126 ms 28044 KB Output is correct
56 Correct 120 ms 29328 KB Output is correct
57 Correct 156 ms 32768 KB Output is correct
58 Correct 142 ms 33404 KB Output is correct
59 Correct 146 ms 33332 KB Output is correct
60 Correct 115 ms 30764 KB Output is correct
61 Correct 146 ms 33132 KB Output is correct
62 Correct 138 ms 33084 KB Output is correct
63 Correct 104 ms 27500 KB Output is correct
64 Correct 133 ms 29732 KB Output is correct
65 Correct 147 ms 32892 KB Output is correct
66 Correct 142 ms 33096 KB Output is correct
67 Correct 152 ms 33160 KB Output is correct
68 Correct 102 ms 29280 KB Output is correct
69 Correct 137 ms 30956 KB Output is correct
70 Correct 104 ms 29756 KB Output is correct
71 Correct 90 ms 28956 KB Output is correct
72 Correct 93 ms 29012 KB Output is correct
73 Correct 100 ms 28888 KB Output is correct
74 Correct 90 ms 28248 KB Output is correct
75 Correct 93 ms 28512 KB Output is correct
76 Correct 86 ms 26912 KB Output is correct
77 Correct 85 ms 26892 KB Output is correct
78 Correct 120 ms 28480 KB Output is correct
79 Correct 163 ms 32736 KB Output is correct
80 Correct 121 ms 29436 KB Output is correct
81 Correct 149 ms 31812 KB Output is correct
82 Correct 159 ms 32828 KB Output is correct
83 Correct 160 ms 32800 KB Output is correct
84 Correct 155 ms 32824 KB Output is correct
85 Correct 95 ms 28104 KB Output is correct
86 Correct 101 ms 28108 KB Output is correct
87 Correct 106 ms 28160 KB Output is correct
88 Correct 163 ms 32800 KB Output is correct
89 Correct 4 ms 9736 KB Output is correct
90 Correct 4 ms 9684 KB Output is correct
91 Correct 4 ms 9684 KB Output is correct
92 Correct 4 ms 9684 KB Output is correct
93 Correct 4 ms 9684 KB Output is correct
94 Correct 4 ms 9696 KB Output is correct
95 Correct 4 ms 9684 KB Output is correct
96 Correct 16 ms 11860 KB Output is correct
97 Correct 17 ms 11972 KB Output is correct
98 Correct 17 ms 11732 KB Output is correct
99 Correct 17 ms 11892 KB Output is correct
100 Correct 17 ms 11868 KB Output is correct
101 Correct 19 ms 11952 KB Output is correct
102 Correct 17 ms 11860 KB Output is correct
103 Correct 17 ms 11904 KB Output is correct
104 Correct 17 ms 11860 KB Output is correct
105 Correct 264 ms 13084 KB Output is correct
106 Correct 289 ms 13100 KB Output is correct
107 Correct 279 ms 14280 KB Output is correct
108 Correct 300 ms 16416 KB Output is correct
109 Correct 201 ms 17484 KB Output is correct
110 Correct 196 ms 16600 KB Output is correct
111 Correct 230 ms 21756 KB Output is correct
112 Correct 248 ms 17996 KB Output is correct
113 Incorrect 40 ms 12572 KB Output isn't correct
114 Halted 0 ms 0 KB -