QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#217653 | #5102. Dungeon Crawler | ucup-team1004# | WA | 1ms | 11644kb | C++14 | 2.5kb | 2023-10-17 08:11:57 | 2023-10-17 08:11:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T>
ostream& operator << (ostream &out,const pair<T,T> &x){
return out<<'('<<x.first<<','<<x.second<<')';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
cerr<<x<<' ',debug(y...);
}
const int N=2e3+10;
int n,m;
vector<pair<int,int> >to[N];
struct Path{
int fa[N],top[N],siz[N],son[N],dep[N];
ll dis[N],f[N],g[N];
#define v e.first
#define w e.second
void dfs1(int u){
dep[u]=dep[fa[u]]+1,siz[u]=1;
f[u]=dis[u];
for(auto e:to[u])if(v^fa[u]){
fa[v]=u,dis[v]=dis[u]+w,dfs1(v);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
f[u]=max(f[u],f[v]);
}
}
int dft,dfn[N],pos[N];
void dfs2(int u,int t){
top[u]=t,pos[dfn[u]=++dft]=u;
if(son[u])dfs2(son[u],t);
for(auto e:to[u])if(v^fa[u]&&v^son[u])dfs2(v,v);
}
void dfs3(int u){
ll fi=g[u],se=fi;
for(auto e:to[u])if(v^fa[u]){
if(f[v]>fi)se=fi,fi=f[v];
else se=max(se,f[v]);
}
// if(u==1&&!fa[2])debug(fi,se,to[u],fa[u],f[11]);
for(auto e:to[u])if(v^fa[u]){
g[v]=f[v]==fi?se:fi;
dfs3(v);
}
}
#undef v
#undef w
void init(int rt){
dfs1(rt),dfs2(rt,rt),dfs3(rt);
}
int LCA(int u,int v){
for(;top[u]^top[v];u=fa[top[u]]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
}
return dep[u]<dep[v]?u:v;
}
int jump(int u,int k){
for(;k>dep[u]-dep[top[u]];u=fa[top[u]]){
k-=dep[u]-dep[top[u]]+1;
}
return pos[dfn[u]-k];
}
}T[N];
int main(){
scanf("%d%d",&n,&m);
ll sum=0;
for(int i=1,u,v,w;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
to[u].push_back({v,w}),to[v].push_back({u,w});
sum+=w*2;
}
for(int i=1;i<=n;i++)T[i].init(i);
// debug(sum);
for(int rt,u,v;m--;){
scanf("%d%d%d",&rt,&u,&v);
int t=T[rt].LCA(u,v);
if(v==t){
puts("impossible");
}else if(u==t){
printf("%lld\n",sum-T[rt].f[rt]);
}else{
int x=T[rt].jump(u,T[rt].dep[u]-T[rt].dep[t]-1),y=T[rt].fa[t];
// debug(x,T[rt].g[x],T[rt].f[11]);
printf("%lld\n",min({
sum-T[rt].g[x],
sum-T[u].g[y]+T[u].dis[t]*2-T[rt].dis[t],
sum+T[rt].dis[u]-T[u].f[u]
}));
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 6420kb
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:
316 293 293 impossible 314
result:
ok 5 lines
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 11644kb
input:
100 100 1 2 289384 2 3 930887 2 4 692778 4 5 636916 4 6 747794 4 7 238336 4 8 885387 8 9 760493 8 10 516650 8 11 641422 8 12 202363 8 13 490028 8 14 368691 8 15 520060 8 16 897764 16 17 513927 16 18 180541 16 19 383427 16 20 89173 16 21 455737 16 22 5212 16 23 595369 16 24 702568 16 25 956430 16 26 ...
output:
103526917 103484292 106288816 104379596 104405611 104775512 106037785 105291604 103838430 106026228 105396939 104175650 105894571 104509242 103971939 105376499 105223283 104153426 105082245 105932330 104130613 104800548 106846555 104138329 103769253 106879186 104044745 104385328 106973740 105460460 ...
result:
wrong answer 7th lines differ - expected: '105434682', found: '106037785'