QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#706782 | #4580. Bicycle Tour | arnold518# | RE | 0ms | 0kb | C++17 | 2.8kb | 2024-11-03 13:27:04 | 2024-11-03 13:27:05 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long lint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 100005, M = 200005, G=17, inf = 2e9;
int n, m, h[N], dep[N], par[G][N], val[G][N];
pair<int, pii> edg[M];
vector<int> adj[N];
struct UF {
int p[N];
void Init() {
for(int i=1;i<=n;i++) {
p[i] = i;
}
}
int Find (int X) {
if(p[X] == X) return X;
return p[X] = Find(p[X]);
}
bool Union (int A, int B) {
A = Find(A);
B = Find(B);
if (A == B) return false;
p[A] = B;
return true;
}
} uf;
void calc (int C, int P) {
dep[C] = dep[P] + 1;
par[0][C] = P;
for(auto &T : adj[C]) {
if(T == P) continue;
calc(T, C);
}
}
void prop (int C, int P) {
for(auto &T : adj[C]) {
if(T == P) continue;
prop(T, C);
}
for(int i=G;i--;) {
if(val[i][C] < inf) {
val[i-1][C] = min(val[i-1][C], val[i][C]);
val[i-1][par[i-1][C]] = min(val[i-1][par[i-1][C]], val[i][C]);
}
}
}
void Color (int A, int B, int C) {
if(dep[A] < dep[B]) swap(A, B);
for(int i=G;i--;) {
if(dep[A] - dep[B] >= (1<<i)) {
val[i][A] = min(val[i][A], C);
A = par[i][A];
}
}
if (A == B) {
val[0][A] = min(val[0][A], C);
return;
}
for(int i=G;i--;) {
if(par[i][A] != par[i][B]) {
val[i][A] = min(val[i][A], C);
val[i][B] = min(val[i][B], C);
A = par[i][A];
B = par[i][B];
}
}
val[1][A] = min(val[1][A], C);
val[1][B] = min(val[1][B], C);
}
int main()
{
// ios_base::sync_with_stdio(false); cin.tie(NULL);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) {
scanf("%d",&h[i]);
}
for(int i=1;i<=m;i++) {
int A, B;
scanf("%d%d",&A,&B);
edg[i] = {abs(h[A]-h[B]), {A, B}};
}
uf.Init();
sort(edg+1, edg+1+m);
for(int i=1;i<=m;i++) {
int A = edg[i].second.first;
int B = edg[i].second.second;
if (uf.Union(A, B)) {
adj[A].push_back(B);
adj[B].push_back(A);
}
}
calc(1, 0);
for(int i=0;i<G;i++) {
for(int j=0;j<N;j++) {
val[i][j] = inf;
}
}
for(int i=1;i<G;i++) {
for(int j=1;j<=n;j++) {
par[i][j] = par[i-1][par[i-1][j]];
}
}
for(int i=1;i<=m;i++) {
int A = edg[i].second.first;
int B = edg[i].second.second;
int C = edg[i].first;
if(par[0][A] == B || par[0][B] == A) continue;
Color(A, B, C);
}
prop(1, 0);
for(int i=1;i<=n;i++) {
if(val[0][i] == inf) printf("-1 ");
else printf("%d ", val[0][i]);
}
puts("");
}
详细
Test #1:
score: 0
Runtime Error
input:
8 11 5 2 7 0 10 6 6 6 1 2 1 3 2 3 2 4 2 5 2 7 3 5 1 6 6 7 6 8 7 8