QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#813215 | #7338. Education Nightmare | 464zzyx | AC ✓ | 681ms | 37904kb | C++14 | 1.8kb | 2024-12-13 23:51:49 | 2024-12-13 23:51:50 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define deb(x) cerr<<"deb:"<<__LINE__<<" "<<#x<<"="<<x<<"\n"
//#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
using namespace std;
//char buf[1<<21],*p1=buf,*p2=buf;
inline ll read()
{
ll sum=0,l=1;char c=getchar();
while(!isdigit(c)){if(c=='-')l=-1;c=getchar();}
while(isdigit(c)){sum=(sum<<1)+(sum<<3)+(c^48);c=getchar();}
return sum*l;
}
ll head[200100],cnt,ans,l,ed,fa[200100],vis[200100],ds[200100],s[200100],tot;
vector<ll> v[200100];
struct edge
{
ll v,next;
}a[400100];
void Add(ll u,ll v)
{
a[++cnt].v=v;
a[cnt].next=head[u];
head[u]=cnt;
}
void dfs(ll x,ll f,ll dis)
{
fa[x]=f;
ds[x]=dis;
if(x==ed)l=dis;
ans=max(ans,dis);
for(ll i=head[x];i;i=a[i].next)
{
if(a[i].v==f)continue;
dfs(a[i].v,x,dis+1);
}
}
void dfs2(ll x,ll f)
{
fa[x]=f;
for(ll i=head[x];i;i=a[i].next)
{
if(a[i].v==f||vis[a[i].v])continue;
dfs2(a[i].v,x);
}
}
int main()
{
ll t=read();
while(t--)
{
ll b=read(),c=read(),d=read();
cnt=0;
for(ll i=0;i<=b;i++)head[i]=0,vis[i]=0;
for(ll i=1;i<b;i++)
{
ll e=read(),f=read();
Add(e,f);
Add(f,e);
}
ans=0;
ed=d;
dfs(c,0,0);
ll res=(b-1)*2-ans;
ed=0;
ll now=d;
tot=0;
while(now)
{
vis[now]=1;
s[++tot]=now;
if(now==c)break;
now=fa[now];
}
for(ll i=1;i<=tot;i++)
{
dfs2(s[i],0);
}
dfs(d,0,0);
for(ll i=1;i<=b;i++)
{
v[ds[i]].push_back(i);
}
for(ll i=b;i>=0;i--)
{
if(v[i].size())
{
res=min(res,l+i);
for(auto p:v[i])
{
now=p;
while(!vis[now])
{
vis[now]=1;
l+=2;
now=fa[now];
}
}
v[i].clear();
}
}
cout<<res<<"\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 17944kb
input:
1 4 2 3 1 2 1 3 1 4
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 681ms
memory: 37904kb
input:
14966 15 13 8 7 14 7 10 5 3 1 3 15 3 1 13 9 5 7 2 6 13 10 11 13 8 5 10 5 12 14 4 16 14 9 16 11 14 9 8 15 14 15 13 10 6 11 3 2 9 5 6 7 10 6 6 8 1 5 15 4 10 2 11 12 100 49 58 67 43 55 34 84 42 3 74 84 54 20 6 86 83 88 51 2 99 4 78 91 64 14 59 82 38 91 44 24 12 12 2 39 19 43 46 5 80 41 35 80 97 79 8 47...
output:
9 8 63 12 3 9 131 8 119 10 13 95 7 76 121 4 85 2 111 7 121 132 107 69 7 117 0 96 91 6 108 90 7 9 4 97 70 94 13 3 3 10 10 12 8 5 103 115 90 5 84 130 3 6 1 114 11 137 84 4 1 132 99 8 1 3 4 13 5 88 104 123 82 5 9 127 85 12 86 11 5 4 8 5 9 4 9 64 8 105 68 2 124 83 1 108 2 1 9 78 6 121 9 0 9 13 87 0 4 9 ...
result:
ok 14966 numbers