이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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<int> &poids, 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, poids, roads, prof);
}
}
int main()
{
int N, M;
cin >> N >> M;
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;
roads[a - 1].push_back({b - 1, c});
roads[b - 1].push_back({a - 1, c});
aretes[i] = {0, b - 1, a - 1};
}
vector<vector<pair<int, int>>> parent(N, vector<pair<int, int>>(20, {0, 123456789}));
vector<int> poids(N, -1);
int K;
cin >> K;
vector<int> NPP(K);
priority_queue<pair<int, int>> Q;
for (int i = 0; i < K; i++)
{
cin >> NPP[i];
Q.push({0, NPP[i] - 1});
}
while (!Q.empty())
{
int dist = -Q.top().first;
int n = Q.top().second;
Q.pop();
if (poids[n] != -1)
continue;
poids[n] = dist;
for (int i = 0; i < roads[n].size(); i++)
{
Q.push({-(dist + roads[n][i].second), roads[n][i].first});
}
}
for (int i = 0; i < M; i++)
{
aretes[i][0] = min(poids[aretes[i][2]], poids[aretes[i][1]]);
}
sort(aretes.begin(), aretes.end());
UnionFind UF(N);
for (int i = 0; i < N; i++)
roads[i].clear();
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, poids, 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);
}
}
int q;
cin >> q;
for (int j = 0; j < q; j++)
{
int a, b;
cin >> a >> 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;
}
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'void par(int, int, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&)':
plan.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++)
| ~~^~~~~~~~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i = 0; i < roads[n].size(); i++)
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |