이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define all(x) x.begin(), x.end()
#define N 100010
#define ff first
#define ss second
#define pb push_back
#define ll long long
#define pii pair <int, int>
using namespace std;
int k;
int n, m, q;
int a, b, c;
int v[N];
int ans[N];
int Par[N];
bool vis[N];
int L[N], R[N];
pii p[N], P[N];
vector <pii> E[N];
priority_queue <pii, vector <pii>, greater <pii>> Q;
int ata(int x)
{
if (x == Par[x]) return x;
return Par[x] = ata(Par[x]);
}
void uni(int x, int y) {
x = ata(x);
y = ata(y);
if (x == y) return;
Par[y] = x;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
v[i] = 1e9;
for(int i = 1; i <= m; i++) {
cin >> a >> b >> c;
E[a].pb({b, c});
E[b].pb({a, c});
}
cin >> k;
for(int i = 1; i <= k; i++) {
cin >> a;
Q.push({0, a}), v[a] = 0;
}
while(!Q.empty()) {
pii t = Q.top();
Q.pop();
if(vis[t.ss]) continue;
vis[t.ss] = 1;
for(auto i: E[t.ss])
if (i.ss + t.ff < v[i.ff])
v[i.ff] = i.ss + t.ff, Q.push({v[i.ff], i.ff});
}
for(int i = 1; i <= n; i++) P[i] = {v[i], i};
sort(P+1, P+n+1);
cin >> q;
for(int i = 1; i <= q; i++)
cin >> p[i].ff >> p[i].ss, L[i] = 0, R[i] = 1e9;
P[0] = {-1, -1};
while (1) {
vector <pii> M;
for(int i = 1; i <= n; i++) Par[i] = i;
for(int i = 1; i <= q; i++)
if (L[i] <= R[i])
M.pb({(L[i] + R[i])/2, i});
if (!M.size())
break;
sort (all(M));
memset(vis, 0, sizeof (vis));
int pos = M.size() - 1;
for(int i = n; i >= 1; i--)
{
int nd = P[i].ss;
vis[nd] = 1;
for(auto h: E[nd])
if (vis[h.ff])
uni(nd, h.ff);
if (pos < 0)
continue;
pii last = M[pos];
while(last.ff > P[i-1].ff) {
a = p[last.ss].ff;
b = p[last.ss].ss;
if (ata(a) == ata(b)) ans[last.ss] = last.ff, L[last.ss] = last.ff + 1;
else R[last.ss] = last.ff - 1;
pos --;
if (pos < 0)
break;
last = M[pos];
}
}
}
for(int i = 1; i <= q; i++)
cout << ans[i] << '\n';
}
/*
10 13
1 10 20
4 9 14
5 6 10
9 7 10
7 8 8
7 6 7
4 5 5
10 9 5
10 3 3
3 4 3
10 7 3
2 3 2
1 2 1
2
9 8
2
3 6
4 7
*/
# | 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... |