#include <bits/stdc++.h>
using namespace std;
vector<int> p;
int find(int x) { return (p[x] == x) ? x : p[x] = find(p[x]); }
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<pair<int, int>>> buckets(1000010);
for (int i = 0; i < M; i++)
{
int a, b, c;
cin >> a >> b >> c;
a--, b--;
buckets[c].push_back({a, b});
}
p.assign(N, 0);
for (int i = 0; i < N; i++)
p[i] = i;
vector<int> dist(N);
vector<vector<int>> contains(N);
vector<vector<pair<int, int>>> AR(N);
for (int i = 0; i < N; i++)
contains[i].push_back(i);
for (int i = 1000000; i >= 0; i--)
{
for (int j = 0; j < buckets[i].size(); j++)
{
int x = find(buckets[i][j].first), y = find(buckets[i][j].second);
if (x == y)
continue;
if (contains[x].size() > contains[y].size())
swap(x, y);
if (find(0) == x)
swap(x, y);
p[x] = y;
AR[buckets[i][j].first].push_back({buckets[i][j].second, i});
AR[buckets[i][j].second].push_back({buckets[i][j].first, i});
}
}
dfs(0, 0, AR, dist, 1000000000);
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:9: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]
9 | for (int i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
sightseeing.cpp: In function 'int main()':
sightseeing.cpp:38: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]
38 | for (int j = 0; j < buckets[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
9 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
30292 KB |
Output is correct |
2 |
Correct |
50 ms |
29772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2498 ms |
163564 KB |
Output is correct |
2 |
Execution timed out |
3570 ms |
245768 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |