QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390076 | #5102. Dungeon Crawler | 0_GB_RAM# | WA | 1ms | 8084kb | C++23 | 3.9kb | 2024-04-15 02:37:26 | 2024-04-15 02:37:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) begin(x),end(x)
#define sz(x) (int)((x).size())
using pii=pair<int,int>;
using vi=vector<int>;
#define fi first
#define se second
#define pb push_back
const int N=2010,L=12;
vector<pii> g[N];
struct Tree
{
int par[N][L];
int dep1[N],depw[N];
int lon1[N],lon2[N],lon3[N];
int best[N];
void dfs(int v,int p,int d1,int dw)
{
dep1[v]=d1;
depw[v]=dw;
par[v][0]=p;
for(int i=1;i<L;i++)
par[v][i]=par[par[v][i-1]][i-1];
lon1[v]=depw[v];
for(auto[to,w]:g[v])
if(to!=p) {
dfs(to, v, d1 + 1, dw + w);
lon3[v] = max(lon3[v], lon1[to]);
if(lon3[v]>lon2[v]) swap(lon3[v],lon2[v]);
if(lon2[v]>lon1[v]) swap(lon2[v],lon1[v]);
}
for(auto[to,w]:g[v])
if(to!=p) {
if(lon1[to]==lon1[v])
best[to]=lon2[v]-2*depw[v];
else
best[to]=lon1[v]-2*depw[v];
best[to]=max(best[to],best[v]);
}
}
int lca(int v,int u)
{
if(dep1[v]>dep1[u])
swap(v,u);
for(int i=L-1;i>=0;i--)
if(dep1[par[u][i]]>=dep1[v])
u=par[u][i];
if(u==v)
return u;
for(int i=L-1;i>=0;i--)
if(par[u][i]!=par[v][i])
u=par[u][i],
v=par[v][i];
return par[u][0];
}
} trees[N];
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n,q;
cin>>n>>q;
int sum=0;
for(int i=1;i<=n-1;i++)
{
int u,v,w;
cin>>u>>v>>w;
g[u].pb({v,w});
g[v].pb({u,w});
sum+=w;
}
for(int i=1;i<=n;i++) {
trees[i].dfs(i, i, 0, 0);
// cout<<i<<": ";
// for(int j=1;j<=n;j++)
// cout<<trees[i].longest[j]<<" ";
// cout<<i<<": ";
// for(int j=1;j<=n;j++)
// cout<<trees[i].best[j]<<" ";
// cout<<"\n";
}
while(q--)
{
int s,k,t;
cin>>s>>k>>t;
auto&tree=trees[t];
int l=tree.lca(s,k);
if(l==t)
cout<<"impossible\n";
else {
int ans = sum * 2 - tree.depw[s];
// cout<<l<<" "<<tree.best[l]<<endl;
int best=tree.best[l];
if(s!=l) best=max(best,tree.lon1[s]-2*tree.depw[s]);
if(k!=l) best=max(best,tree.lon1[k]-2*tree.depw[k]);
while(s!=l&&tree.par[s][0])
{
int p=tree.par[s][0];
best=max(best,(tree.lon1[s]==tree.lon1[p]?tree.lon2[p]:tree.lon1[p])-2*tree.depw[p]);
s=p;
}
swap(s,k);
while(s!=l&&tree.par[s][0])
{
int p=tree.par[s][0];
best=max(best,(tree.lon1[s]==tree.lon1[p]?tree.lon2[p]:tree.lon1[p])-2*tree.depw[p]);
s=p;
}
if(s==l)
if(k==l)
best=max(best,tree.lon1[l]-2*tree.depw[l]);
else
best=max(best,tree.best[k]);
else
if(k==l)
best=max(best,tree.best[s]);
else
{
int a=tree.lon1[l],b=tree.lon2[l],c=tree.lon3[l];
int x=tree.lon1[k],y=tree.lon1[s];
if(a==x) a=0; else if(b==x) b=0; else if(c==x) c=0;
if(a==y) a=0; else if(b==y) b=0; else if(c==y) c=0;
best=max({best,a-2*tree.depw[l],b-2*tree.depw[l],c-2*tree.depw[l]});
}
cout << ans - best << "\n";
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 8084kb
input:
22 5 1 2 7 2 3 5 3 4 8 3 6 6 3 7 3 2 5 6 5 8 2 8 9 11 2 10 16 1 11 12 11 12 4 11 13 9 1 14 25 14 15 4 15 16 5 15 17 6 15 18 1 15 19 8 14 20 7 20 21 9 20 22 17 1 19 9 1 9 19 1 8 9 1 9 8 2 22 11
output:
293 293 263 impossible 286
result:
wrong answer 1st lines differ - expected: '316', found: '293'