QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#303160 | #5439. Meet in the Middle | yllcm | WA | 56ms | 41632kb | C++14 | 4.7kb | 2024-01-11 19:50:14 | 2024-01-11 19:50:14 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define db double
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define FR first
#define SE second
#define int long long
using namespace std;
inline int read() {
int x = 0; bool op = false;
char c = getchar();
while(!isdigit(c))op |= (c == '-'), c = getchar();
while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return op ? -x : x;
}
const int N = 2e5 + 10;
int n, Q;
struct Edge {int v, w, id;};
namespace Tree2 {
vector<Edge> G[N];
void add(int u, int v, int w) {G[u].pb({v, w}); G[v].pb({u, w});}
int cnt;
int dfn[N], dep[N], st[N][25];
int cmin(int x, int y) {return dep[x] < dep[y] ? x : y;}
void dfs(int u, int fa) {
st[dfn[u] = ++cnt][0] = fa;
for(auto t : G[u]) {
int v = t.v, w = t.w;
if(v == fa)continue;
dep[v] = dep[u] + w;
dfs(v, u);
}
return ;
}
void init() {
dfs(1, 0);
for(int j = 1; (1 << j) <= n; j++) {
for(int i = 1; i + (1 << j) - 1 <= n; j++) {
st[i][j] = cmin(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
return ;
}
int lca(int u, int v) {
if(u == v)return u;
int l = dfn[u], r = dfn[v];
if(l > r)swap(l, r);
int t = __lg(r - (l++));
return cmin(st[l][t], st[r - (1 << t) + 1][t]);
}
int dist(int u, int v) {return dep[u] + dep[v] - dep[lca(u, v)] * 2;}
};
namespace Tree1 {
int cM, cnte, cntv;
struct Diam {
int x, vx, y, vy;
int val() {return (x == y ? vx : Tree2::dist(x, y) + vx + vy);}
};
Diam operator + (Diam x, Diam y) {
if(!x.x || !y.y)return (x.x ? x : y);
Diam z = (x.val() > y.val() ? x : y), tmp;
if((tmp = (Diam){x.x, x.vx, y.x, y.vx}).val() > z.val())z = tmp;
if((tmp = (Diam){x.x, x.vx, y.y, y.vy}).val() > z.val())z = tmp;
if((tmp = (Diam){x.y, x.vy, y.x, y.vx}).val() > z.val())z = tmp;
if((tmp = (Diam){x.y, x.vy, y.y, y.vy}).val() > z.val())z = tmp;
return z;
}
Diam diam[N][2];
vector<int> d[N];
vector<int> side[N], id[N];
vector<Edge> G[N];
vector<Edge> H[N];
void add(int u, int v, int w) {G[u].pb({v, w}); G[v].pb({u, w});}
void add_h(int u, int v, int w) {H[u].pb({v, w, ++cnte}); H[v].pb({u, w, cnte});}
void dfs(int u, int fa) {
int lv = 0;
for(auto t : G[u]) {
int v = t.v, w = t.w;
if(v == fa)continue;
if(!lv)add_h(u, v, w), lv = u;
else add_h(++cntv, lv, 0), add_h(cntv, v, w), lv = cntv;
dfs(v, u);
}
return ;
}
int X, Y, S;
int sz[N], vis[N];
void findroot(int u, int fa) {
sz[u] = 1;
int mx = 0;
for(auto t : H[u]) {
int v = t.v, id = t.id;
if(v == fa || vis[id])continue;
findroot(v, u); sz[u] += sz[v];
mx = max(mx, sz[v]);
}
if(max(S - sz[u], mx) * 2 <= S)X = u;
return ;
}
void calc(int u, int fa, int o, int dist) {
if(u <= n) {
side[u].pb(o), id[u].pb(cM), d[u].pb(dist);
diam[cM][o] = diam[cM][o] + (Diam){u, dist, u, dist};
}
for(auto t : H[u]) {
int v = t.v, w = t.w, id = t.id;
if(v == fa || vis[id])continue;
calc(v, u, o, dist + w);
}
return ;
}
void solve(int u) {
// cout << u << ' ' << S << endl;
if(S == 1)return ;
X = Y = 0; findroot(u, 0);
int mx = 0; Edge e = {0, 0, 0};
for(auto t : H[X]) {
int v = t.v, id = t.id;
if(vis[id])continue;
int nsz = (sz[v] > sz[u] ? S - sz[u] : sz[v]);
if(nsz > mx)mx = nsz, Y = v, e = t;
}
if(sz[X] < sz[Y])swap(X, Y);
// cout << "X:" << X << ' ' << Y << endl;
vis[e.id] = true;
cM++; diam[cM][0] = {0, 0, 0, 0}; diam[cM][1] = {0, 0, 0, 0};
calc(X, 0, 0, 0); calc(Y, 0, 1, e.w);
int lS = S - sz[Y], rS = sz[Y], lu = X, ru = Y;
S = lS; solve(lu); S = rS; solve(ru);
return ;
}
void init() {
cntv = n; dfs(1, 0);
S = cntv; solve(1);
return ;
}
int query(int x, int y) {
int lim = d[x].size(), ans = 0;
for(int i = 0; i < lim; i++) {
// cout << x << ' ' << i << ' ' << side[x][i] << ' ' << d[x][i] << endl;
int o = side[x][i];
Diam nd = diam[id[x][i]][o ^ 1];
ans = max(ans, Tree2::dist(y, nd.x) + nd.vx + d[x][i]);
ans = max(ans, Tree2::dist(y, nd.y) + nd.vy + d[x][i]);
}
return ans;
}
};
signed main() {
n = read(); Q = read();
for(int i = 1; i < n; i++) {
int u = read(), v = read(), w = read();
Tree1::add(u, v, w);
}
for(int i = 1; i < n; i++) {
int u = read(), v = read(), w = read();
Tree2::add(u, v, w);
}
Tree2::init();
Tree1::init();
while(Q--) {
int x = read(), y = read();
printf("%lld\n", Tree1::query(x, y));
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 31796kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
6 4 5 3
result:
ok 4 number(s): "6 4 5 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 31736kb
input:
2 1 1 2 1 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 7ms
memory: 31792kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 56ms
memory: 41632kb
input:
10000 50000 8101 5996 108427744 5996 7605 870838849 5996 5599 603303696 8101 3006 339377417 8101 6463 442516687 6463 5560 109174079 5560 4063 127596224 3006 1682 947915262 5996 1986 130416311 6463 5455 279771516 6463 2634 516688796 4063 3034 217886863 7605 5092 742375061 5599 2266 193804402 5092 140...
output:
654568671472 650680967225 538415115753 643796679766 496687553645 652573202588 648268145886 597745679005 668222749519 828016625515 681100836614 758905220965 759549027275 778464605350 560230171788 533474111387 625059931348 696280533670 555891589450 750952612209 667820455954 690903166011 715458854222 6...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '654568671472'