#include <bits/stdc++.h>
using namespace std;
class UnionFind
{
private:
vector<int> p;
public:
UnionFind(int x)
{
p.assign(x, 0);
for (int i = 0; i < x; i++)
p[i] = i;
}
int find(int x) { return (p[x] == x) ? x : p[x] = find(p[x]); }
bool merge(int a, int b)
{
int x = find(a), y = find(b);
if (x == y)
return 0;
p[x] = y;
return 1;
}
};
void dfs(int x, int p, vector<vector<pair<int, int>>> &AR, vector<int> &dist, int cur)
{
dist[x] = cur;
for (int i = 0; i < AR[x].size(); i++)
{
if (AR[x][i].first == p)
continue;
dfs(AR[x][i].first, x, AR, dist, min(cur, AR[x][i].second));
}
}
int main()
{
int N, M, Q;
cin >> N >> M >> Q;
vector<vector<int>> AR;
vector<vector<pair<int, int>>> buckets(100010);
for (int i = 0; i < M; i++)
{
int a, b, c;
cin >> a >> b >> c;
a--, b--;
buckets[c].push_back({a, b});
}
for (int i = 100000; i >= 0; i--)
{
for (int j = 0; j < buckets[i].size(); j++)
{
AR.push_back({buckets[i][j].first, buckets[i][j].second, i});
}
}
UnionFind UF(N);
vector<vector<pair<int, int>>> AR2(N);
for (int i = 0; i < M; i++)
{
if (UF.merge(AR[i][0], AR[i][1]))
{
AR2[AR[i][0]].push_back({AR[i][1], AR[i][2]});
AR2[AR[i][1]].push_back({AR[i][0], AR[i][2]});
}
}
vector<int> dist(N);
dfs(0, 0, AR2, dist, 1000000);
for (int i = 0; i < Q; i++)
{
int x;
cin >> x;
cout << dist[x - 1] << '\n';
}
}
Compilation message
sightseeing.cpp: In function 'void dfs(int, int, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&, int)':
sightseeing.cpp:28: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]
28 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
sightseeing.cpp: In function 'int main()':
sightseeing.cpp:50: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]
50 | for (int j = 0; j < buckets[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3160 KB |
Output is correct |
2 |
Correct |
2 ms |
2872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
10132 KB |
Output is correct |
2 |
Correct |
39 ms |
8968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2474 ms |
262144 KB |
Output is correct |
2 |
Runtime error |
3 ms |
5212 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |