QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#65389 | #4580. Bicycle Tour | Mostafa_Moharram | WA | 14ms | 11192kb | C++17 | 3.2kb | 2022-11-30 09:17:01 | 2022-11-30 09:17:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
const int N = 2e5 + 5;
struct Edge {
int u, v, w;
Edge(int u, int v, int w): u(u), v(v), w(w) {}
bool operator < (const Edge &other) const {
return w < other.w;
}
};
vector<Edge> e;
struct Dsu {
int par[N], rnk[N];
void init() {
for (int i = 1; i < N; ++i)
par[i] = i, rnk[i] = 1;
}
int findParent(int u){
return par[u] = (par[u] == u ? u : findParent(par[u]));
}
bool merge(int u, int v){
u = findParent(u);
v = findParent(v);
if(u == v)return false;
if(rnk[u] < rnk[v])
swap(u,v);
par[v] = u;
rnk[u] += rnk[v];
return true;
}
};
int par[N];
int findPar(int u){
return par[u] = (par[u] == u ? u : findPar(par[u]));
}
void init(){
for(int i = 1; i < N; ++i)
par[i] = i;
}
vector<int> adj[N];
int P[N], lvl[N];
void dfs(int u, int pr, int l){
P[u] = pr;
lvl[u] = l;
for(auto &v: adj[u])
if(v != pr)
dfs(v, u,l + 1);
}
int main() {
ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr);
init();
int n , m;
cin >> n >> m;
vector<int> h(n + 1);
for(int i = 1; i <= n; ++i)
cin >> h[i];
for(int i = 0; i < m; ++i){
int u, v, w;
cin >> u >> v;
w = abs(h[u] - h[v]);
e.emplace_back(u,v,w);
}
sort(e.begin(), e.end());
Dsu mst;
mst.init();
vector<Edge> notUsed;
for(int i = 0; i < m; ++i){
int u,v,w;
u = e[i].u; v = e[i].v; w = e[i].w;
if(!mst.merge(u,v)){ // merged before, so skip
notUsed.emplace_back(e[i]);
}else{
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
}
dfs(1, 1, 0);
vector<int> ans(n + 1, -1);
for(int i = 0; i < notUsed.size(); ++i){
Edge e = notUsed[i];
if(ans[e.u] != -1 and ans[e.v] != -1)
continue;
if(ans[e.u] == -1 and ans[e.v] == -1){
int u = e.u, v = e.v;
int Limit = min(lvl[u], lvl[v]);
if(lvl[u] < lvl[v])
swap(u,v);
while(lvl[u] < Limit){
ans[u] = e.w;
par[u] = findPar(P[u]);
u = findPar(u);
}
while(u != v){
ans[u] = ans[v] = e.w;
par[u] = findPar(P[u]);
par[v] = findPar(P[v]);
u = findPar(u);
v = findPar(v);
}
if(ans[u] == -1)
ans[u] = e.w;
}else if(ans[e.u] == -1){
int u = e.u, v = findPar(e.v);
while(ans[u] == -1){
ans[u] = e.w;
par[u] = findPar(P[u]);
u = findPar(u);
}
}else {
int u = findPar(e.u), v = e.v;
while(ans[v] == -1){
ans[v] = e.w;
par[v] = findPar(P[v]);
v = findPar(v);
}
}
}
for(int i = 1; i <= n; ++i)
cout << ans[i] << ' ';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 10476kb
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
output:
4 4 5 -1 8 0 0 0
result:
ok single line: '4 4 5 -1 8 0 0 0 '
Test #2:
score: 0
Accepted
time: 3ms
memory: 10392kb
input:
4 5 10 20 30 40 1 2 1 3 1 4 2 3 3 4
output:
20 20 20 30
result:
ok single line: '20 20 20 30 '
Test #3:
score: 0
Accepted
time: 7ms
memory: 10400kb
input:
5 4 72 35 22 49 108 1 2 2 3 3 4 4 5
output:
-1 -1 -1 -1 -1
result:
ok single line: '-1 -1 -1 -1 -1 '
Test #4:
score: 0
Accepted
time: 2ms
memory: 10476kb
input:
2 1 10 20 1 2
output:
-1 -1
result:
ok single line: '-1 -1 '
Test #5:
score: -100
Wrong Answer
time: 14ms
memory: 11192kb
input:
252 31626 825 5234 3578 4723 2145 4362 1861 2413 7203 1939 3210 7153 2155 4559 4403 1466 887 3786 6529 719 4272 3287 5703 6708 2390 4987 4214 770 3487 6230 3498 6255 4963 1093 3065 2961 1663 4857 3224 4284 4228 106 1614 1010 145 1557 4510 1032 4632 155 5570 154 884 1204 2876 6163 5023 4593 7261 3729...
output:
14 15 24 34 10 26 40 30 22 6 11 22 10 8 22 48 14 24 33 9 13 11 46 20 23 14 4 9 11 32 10 32 17 38 18 13 14 0 11 6 4 42 14 22 9 48 22 22 34 7 52 7 14 45 12 32 14 8 15 24 10 11 21 34 66 9 20 21 29 6 30 16 26 9 6 27 8 10 12 17 12 22 9 17 10 34 34 9 33 11 14 14 24 22 42 66 110 24 16 7 10 22 15 14 22 48 2...
result:
wrong answer 1st lines differ - expected: '55 33 46 34 10 33 34 30 22 33 ...45 15 71 10 52 18 16 30 10 34 4', found: '14 15 24 34 10 26 40 30 22 6 1...0 11 48 10 52 10 16 30 10 34 4 '