Submission #751886

#TimeUsernameProblemLanguageResultExecution timeMemory
751886puppyReconstruction Project (JOI22_reconstruction)C++17
100 / 100
1267 ms17268 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>
#define all(v) v.begin(), v.end()
using namespace std;
int N, M;
int stop[100005];

struct Edge
{
    int s, e;
    int w;
    bool operator<(const Edge &e)
    {
        return w < e.w;
    }
};
Edge edge[100005];
vector<pair<int, int>> adj[505];
bool visited[505];
pair<int, int> par[505];
bool add[100005];
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> N >> M;
    for (int i = 1; i <= M; i++) {
        int a, b, w; cin >> a >> b >> w;
        edge[i] = {a, b, w};
    }
    sort(edge + 1, edge + M + 1);
    for (int i = 1; i <= M; i++) {
        fill(visited + 1, visited + N + 1, false);
        int a = edge[i].s, b = edge[i].e;
        visited[a] = true;
        queue<int> q;
        q.push(a);
        while (!q.empty()) {
            int cur = q.front(); q.pop();
            for (pair<int, int> p:adj[cur]) {
                if (!visited[p.second]) {
                    par[p.second] = make_pair(p.first, cur);
                    visited[p.second] = true; q.push(p.second);
                }
            }
        }
        if (visited[b]) {
            int mn = i, now = b;
            while (1) {
                mn = min(mn, par[now].first);
                now = par[now].second;
                if (now == a) break;
            }
            int s = edge[mn].s, e = edge[mn].e;
            adj[s].erase(remove(all(adj[s]), make_pair(mn, e)), adj[s].end());
            adj[e].erase(remove(all(adj[e]), make_pair(mn, s)), adj[e].end());
            stop[mn] = i;
            add[i] = true;
        }
        adj[a].push_back(make_pair(i, b));
        adj[b].push_back(make_pair(i, a));
    }
    vector<pair<int, pair<int, int>>> vc;
    for (int i = 1; i <= M; i++) {
        if (stop[i]) {
            vc.push_back(make_pair((edge[i].w+edge[stop[i]].w)/2+1, make_pair(i, stop[i])));
        }
    }
    sort(vc.begin(), vc.end());
    int pv = 0;
    int pv2 = 0;
    int Q; cin >> Q;
    pair<long long, long long> lin = make_pair(0LL, 0LL);
    for (int i = 1; i <= M; i++) {
        if (!add[i]) {
            lin.first -= 1;
            lin.second += edge[i].w;
        }
    }
    for (int i = 0; i < Q; i++) {
        long long X; cin >> X;
        while (pv < M && edge[pv+1].w <= X) {
            pv++;
            lin.second -= 2 * edge[pv].w;
            lin.first += 2;
        }
        while (pv2 < (int)vc.size() && vc[pv2].first <= X) {
            lin.first -= 2;
            lin.second += edge[vc[pv2].second.first].w + edge[vc[pv2].second.second].w;
            pv2++;
        }
        cout << X * lin.first + lin.second << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...