Submission #796241

# Submission time Handle Problem Language Result Execution time Memory
796241 2023-07-28T08:17:59 Z 이성호(#10070) Security Guard (JOI23_guard) C++17
0 / 100
51 ms 26188 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[3005];
pair<int, int> now[3005];
pair<pii, pii> dp[3005];
pii dp2[3005];
int par[3005];
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, min(dp[v].first, dp[i].second));
            }
            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 == dp[p].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 2 ms 5076 KB Output is correct
2 Runtime error 51 ms 26188 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Runtime error 51 ms 26188 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Runtime error 51 ms 26188 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Runtime error 51 ms 26188 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Incorrect 13 ms 6904 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Incorrect 13 ms 6904 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Runtime error 51 ms 26188 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -