이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
#define MP make_pair
typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e5 + 3e2;
const int M = 222;
vector<pair<int, int>> g[N];
int dist[N];
bool processed[N];
int parent[N][750];
int p[N];
int find_set(int x){
if (p[x] == x) return x;
return p[x] = find_set(p[x]);
}
void union_sets(int x, int y){
x = find_set(x);
y = find_set(y);
if (x == y) return;
if (rng() & 1) swap(x, y);
p[x] = y;
}
int qs[N], qt[N];
vector<int> queries[N];
int ans[N];
void solve(int TC) {
int n, m;
cin >> n >> m;
int SQ = sqrt(m) + 3;
for (int i = 1; i <= m; i++){
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
for (int i = 1; i <= n; i++){
dist[i] = INF;
p[i] = i;
for (int j = 0; j < 750; j++) parent[i][j] = i;
}
priority_queue<pair<int, int>> pq;
int k;
cin >> k;
while (k--){
int x;
cin >> x;
dist[x] = 0;
pq.push(MP(0, x));
}
while (!pq.empty()){
int x = pq.top().second; pq.pop();
if (processed[x]) continue;
processed[x] = true;
for (auto edge : g[x]){
int y = edge.first, w = edge.second;
if (dist[y] > dist[x] + w){
dist[y] = dist[x] + w;
pq.push(MP(-dist[y], y));
}
}
}
vector<pair<int, pair<int, int>>> edges;
for (int i = 1; i <= n; i++){
for (auto edge : g[i]){
int j = edge.first;
if (i < j){
edges.emplace_back(min(dist[i], dist[j]), MP(i, j));
}
}
}
sort(all(edges));
for (int i = m - 1; i >= 0; i--){
int x = edges[i].second.first, y = edges[i].second.second;
union_sets(x, y);
if (i % SQ == 0){
for (int j = 1; j <= n; j++){
parent[j][i / SQ] = find_set(j);
}
}
}
int q;
cin >> q;
for (int i = 1; i <= q; i++){
cin >> qs[i] >> qt[i];
int block = SQ;
while (parent[qs[i]][block] != parent[qt[i]][block]) block--;
queries[block].push_back(i);
ans[i] = 0;
}
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = m - 1; i >= 0; i--){
int x = edges[i].second.first, y = edges[i].second.second, w = edges[i].first;
union_sets(x, y);
for (int j : queries[i / SQ]){
if (find_set(qs[j]) == find_set(qt[j])) ans[j] = max(ans[j], w);
}
}
for (int i = 1; i <= q; i++){
cout << ans[i] << endl;
}
}
int main() {
startTime = clock();
cin.tie(nullptr); cout.tie(nullptr);
ios_base::sync_with_stdio(false);
bool llololcal = false;
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
llololcal = true;
#endif
int TC = 1;
//cin >> TC;
for (int test = 1; test <= TC; test++) {
//debug(test);
//cout << "Case #" << test << ": ";
solve(test);
}
if (llololcal) cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
return 0;
}
# | 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... |