Submission #830167

# Submission time Handle Problem Language Result Execution time Memory
830167 2023-08-18T22:08:38 Z raphaelp Sightseeing (NOI14_sightseeing) C++14
15 / 25
3293 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
class UnionFind
{
private:
    vector<int> p, rank;
    int numSets;

public:
    UnionFind(int m)
    {
        p.assign(m, 0);
        for (int i = 0; i < m; ++i)
            p[i] = i;
        rank.assign(m, 0);
        numSets = m;
    }
    int find(int x) { return (p[x] == x) ? x : p[x] = find(p[x]); }
    bool sameSet(int a, int b) { return find(a) == find(b); }
    int nbSets() { return numSets; }
    bool merge(int a, int b)
    {
        if (sameSet(a, b))
            return false;
        int x = find(a);
        int y = find(b);
        if (rank[x] > rank[y])
            swap(x, y);
        p[x] = y;
        if (rank[x] == rank[y])
            rank[y]++;
        numSets--;
        return true;
    }
};
void par(int n, int p, vector<vector<pair<int, int>>> &parent, vector<vector<pair<int, int>>> &roads, vector<int> &prof)
{
    parent[n][0].first = p;
    prof[n] = prof[p] + 1;
    for (int i = 0; i < roads[n].size(); i++)
    {
        if (roads[n][i].first == p)
            continue;
        parent[roads[n][i].first][0].second = roads[n][i].second;
        par(roads[n][i].first, n, parent, roads, prof);
    }
}
int main()
{
    int N, M, q;
    cin >> N >> M >> q;
    vector<vector<pair<int, int>>> roads(N, vector<pair<int, int>>(0));
    vector<vector<int>> aretes(M, vector<int>(0));
    for (int i = 0; i < M; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        aretes[i] = {c, b - 1, a - 1};
    }
    vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(20, {0, 123456789}));
    sort(aretes.begin(), aretes.end());
    UnionFind UF(N);
    for (int i = aretes.size() - 1; UF.nbSets() > 1; i--)
    {
        if (UF.merge(aretes[i][1], aretes[i][2]))
        {
            roads[aretes[i][1]].push_back({aretes[i][2], aretes[i][0]});
            roads[aretes[i][2]].push_back({aretes[i][1], aretes[i][0]});
        }
    }
    vector<int> prof(N, 0);
    par(0, 0, parent, roads, prof);
    for (int i = 1; i < 20; i++)
    {
        for (int j = 0; j < N; j++)
        {
            parent[j][i].first = parent[parent[j][i - 1].first][i - 1].first;
            parent[j][i].second = min(parent[parent[j][i - 1].first][i - 1].second, parent[j][i - 1].second);
        }
    }
    for (int j = 0; j < q; j++)
    {
        int a = 1, b;
        cin >> b;
        a--;
        b--;
        if (prof[a] < prof[b])
            swap(a, b);
        int minn = 123456789;
        for (int i = 19; i >= 0; i--)
        {
            if (prof[a] - (1 << i) >= prof[b])
            {
                minn = min(minn, parent[a][i].second);
                a = parent[a][i].first;
            }
        }
        if (a != b)
        {
            for (int i = 19; i >= 0; i--)
            {
                if (parent[a][i].first != parent[b][i].first)
                {
                    minn = min(minn, parent[a][i].second);
                    minn = min(minn, parent[b][i].second);
                    a = parent[a][i].first;
                    b = parent[b][i].first;
                }
            }
            minn = min(minn, parent[a][0].second);
            minn = min(minn, parent[b][0].second);
        }

        cout << minn << endl;
    }
}

Compilation message

sightseeing.cpp: In function 'void par(int, int, std::vector<std::vector<std::pair<int, int> > >&, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&)':
sightseeing.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 0; i < roads[n].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 13512 KB Output is correct
2 Correct 51 ms 12236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 3293 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -