Submission #864315

#TimeUsernameProblemLanguageResultExecution timeMemory
864315raphaelpSightseeing (NOI14_sightseeing)C++14
15 / 25
3954 ms262144 KiB
#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); } } void linsort(vector<tuple<int, int, int>> &aretes) { vector<vector<tuple<int, int, int>>> Temp(100010, vector<tuple<int, int, int>>(0)); for (int i = 0; i < aretes.size(); i++) { Temp[get<0>(aretes[i])].push_back(aretes[i]); } aretes.clear(); for (int i = 0; i < 100010; i++) { for (int j = 0; j < Temp[i].size(); j++) { aretes.push_back(Temp[i][j]); } } } int main() { int N, M, q; cin >> N >> M >> q; vector<vector<pair<int, int>>> roads(N, vector<pair<int, int>>(0)); vector<tuple<int, int, int>> aretes(M); 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})); linsort(aretes); UnionFind UF(N); for (int i = aretes.size() - 1; UF.nbSets() > 1; i--) { if (UF.merge(get<1>(aretes[i]), get<2>(aretes[i]))) { roads[get<1>(aretes[i])].push_back({get<2>(aretes[i]), get<0>(aretes[i])}); roads[get<2>(aretes[i])].push_back({get<1>(aretes[i]), get<0>(aretes[i])}); } } 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 << '\n'; } }

Compilation message (stderr)

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++)
      |                     ~~^~~~~~~~~~~~~~~~~
sightseeing.cpp: In function 'void linsort(std::vector<std::tuple<int, int, int> >&)':
sightseeing.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int i = 0; i < aretes.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~
sightseeing.cpp:58:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         for (int j = 0; j < Temp[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...