QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#770809 | #7883. Takeout Delivering | fosov | WA | 4ms | 47756kb | C++14 | 2.4kb | 2024-11-22 00:26:07 | 2024-11-22 00:26:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lll __int128
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define N 300010
#define K 22
vector<int> G[N<<2];
int w[N<<2], f[N<<2], dep[N<<2], mxd[N<<2], ch[N<<2], par[N<<2];
void add_edge(int u, int v) {
G[u].emplace_back(v);
G[v].emplace_back(u);
}
void dfs(int u, int fa, int d) {
f[u] = fa;
dep[u] = d;
mxd[u] = d;
ch[u] = 0;
for (auto& v : G[u]) {
if (v == fa) continue;
dfs(v, u, d+1);
if (mxd[v] > mxd[u]) {
mxd[u] = mxd[v];
ch[u] = v;
}
}
}
void dfs1(int u, int fa, int p) {
par[u] = p;
for (auto& v : G[u]) {
if (v == fa) continue;
if (v == ch[u]) dfs1(v, u, p);
else dfs1(v, u, v);
}
}
int lca(int u, int v) {
while (par[u] != par[v]) {
if (dep[u] > dep[v]) {
u = f[par[u]];
} else {
v = f[par[v]];
}
}
return dep[u] < dep[v] ? u : v;
}
int p[N], rt[N], z, flg[N];
void init(int n) {
for (int i = 1; i <= n; ++ i) p[i] = rt[i] = i;
z = n;
}
int find(int x) {
return p[x] == x ? x : (p[x] = find(p[x]));
}
void join(int u, int v, int l) {
u = find(u), v = find(v);
if (u == v) return;
w[++ z] = l;
add_edge(z, rt[u]), add_edge(z, rt[v]);
p[u] = v;
rt[v] = z;
}
int main() {
#ifdef TEST
freopen("zz.in", "r+", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m; cin >> n >> m;
init(n);
vector<tuple<int, int, int>> edgs;
for (int i = 0; i < m; ++ i) {
int u, v, l; cin >> u >> v >> l;
edgs.emplace_back(l, u, v);
}
int res = INF;
sort(edgs.begin(), edgs.end());
for (int i = 0; i < m; ++ i) {
auto& [l, u, v] = edgs[i];
join(u, v, l);
flg[i] = find(1) == find(n);
}
dfs(z, z, 0);
dfs1(z, z, z);
for (int i = 0; i < m; ++ i) {
if (!flg[i]) continue;
auto& [l, u, v] = edgs[i];
res = min(res, l + max(w[lca(u, 1)], w[lca(v, n)]));
res = min(res, l + max(w[lca(u, n)], w[lca(v, 1)]));
}
cout << res << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 47756kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
6
result:
wrong answer 1st numbers differ - expected: '5', found: '6'