제출 #1336748

#제출 시각아이디문제언어결과실행 시간메모리
1336748iamhereforfunEvacuation plan (IZhO18_plan)C++20
100 / 100
459 ms24052 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 1e5 + 5;
const int M = 5e5 + 5;
const int K = 1e3 + 5;
const int LG = 20;
const int INF = 1e9 + 5;
const int B = 1000;
const int MOD = 1e9 + 7;

struct Dsu
{
    vector<int> parent;
    vector<int> size;
    Dsu(int n)
    {
        parent.assign(n + 1, 0);
        size.assign(n + 1, 1);
        for (int x = 0; x < n + 1; x++)
        {
            parent[x] = x;
        }
    }
    int getParent(int x)
    {
        return parent[x] == x ? x : parent[x] = getParent(parent[x]);
    }
    void update(int a, int b)
    {
        a = getParent(a);
        b = getParent(b);
        if (a != b)
        {
            if (size[a] < size[b])
            {
                swap(a, b);
            }
            parent[b] = a;
            size[a] += size[b];
        }
    }
    bool chkConnect(int a, int b)
    {
        return getParent(a) == getParent(b);
    }
};

int n, m, q, k, l[M], r[M], dis[N], ans[M];
vector<pair<int, int>> adj[N], city;
pair<int, int> query[M];
bool vis[N];

inline void solve()
{
    cin >> n >> m;
    for (int x = 0; x < m; x++)
    {
        int a, b, w;
        cin >> a >> b >> w;
        adj[a].push_back({b, w});
        adj[b].push_back({a, w});
    }
    for (int x = 1; x <= n; x++)
    {
        dis[x] = INF;
    }
    cin >> k;
    priority_queue<pair<int, int>> pq;
    for (int x = 0; x < k; x++)
    {
        int i;
        cin >> i;
        dis[i] = 0;
        pq.push({0, i});
    }
    while (!pq.empty())
    {
        auto [w, c] = pq.top();
        pq.pop();
        if (vis[c])
            continue;
        vis[c] = 1;
        for (auto &[i, w] : adj[c])
        {
            if (dis[i] > dis[c] + w)
            {
                dis[i] = dis[c] + w;
                pq.push({-dis[i], i});
            }
        }
    }
    for (int x = 1; x <= n; x++)
    {
        city.push_back({dis[x], x});
        // cout << dis[x] << " " << x << "\n";
    }
    // cout << "\n";
    sort(city.rbegin(), city.rend());
    cin >> q;
    for (int x = 0; x < q; x++)
    {
        int a, b;
        cin >> a >> b;
        // cout << a <<  " " << b << "\n";
        query[x] = {a, b};
        l[x] = 0;
        r[x] = INF;
    }
    while (true)
    {
        vector<pair<int, int>> v;
        Dsu dsu(n);
        bool b = 0;
        for (int x = 0; x < q; x++)
        {
            if (l[x] <= r[x])
            {
                v.push_back({(l[x] + r[x]) / 2, x});
                b = 1;
            }
        }
        for (int x = 1; x <= n; x++)
        {
            vis[x] = 0;
        }
        if (!b)
            break;
        sort(v.rbegin(), v.rend());
        int i = 0;
        for (auto &[w, id] : v)
        {
            while (i < n)
            {
                if (city[i].first >= w)
                {
                    int j = city[i].second;
                    vis[j] = 1;
                    for (auto &[c, w] : adj[j])
                    {
                        if (vis[c])
                        {
                            dsu.update(c, j);
                        }
                    }
                    i++;
                }
                else
                {
                    break;
                }
            }
            if (dsu.chkConnect(query[id].first, query[id].second))
            {
                ans[id] = w;
                l[id] = w + 1;
            }
            else
            {
                r[id] = w - 1;
            }
        }
    }
    for (int x = 0; x < q; x++)
    {
        cout << ans[x] << "\n";
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#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...