QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#201426 | #7338. Education Nightmare | kjhhjki | WA | 1ms | 5604kb | C++14 | 1.9kb | 2023-10-05 14:16:57 | 2023-10-05 14:16:58 |
Judging History
answer
#include <bits/stdc++.h>
#define MAXN 200005
#define For(I,A,B) for(int I = (A), endi = (B); I <= endi; ++I)
#define foR(I,A,B) for(int I = (A), endi = (B); I >= endi; --I)
#define ForE(I,A) for(int I = head[A]; I; I = e[I].nxt)
using namespace std;
typedef long long _ll;
typedef unsigned int ui;
struct Edge { int to,nxt; } e[MAXN<<1];
int head[MAXN],tot;
void add_edge(int x,int y) { e[++tot] = (Edge){y,head[x]}; head[x] = tot; }
int T,mx,ans,cnt,dis,n,m,s,u,v,dep[MAXN],pre[MAXN];
bool hasS[MAXN],inS[MAXN];
void dfs(int u, int prev)
{
pre[u] = prev;
if(u == s) hasS[u] = 1;
ForE(i,u)
{
int v = e[i].to;
if(v == prev) continue;
dep[v] = dep[u] + 1;
dfs(v,u);
if(hasS[v]) hasS[u] = 1;
}
}
void dfs2(int u, int pre)
{
inS[u] = 1;
ForE(i,u)
{
int v = e[i].to;
if(v == pre) continue;
dfs2(v,u);
}
}
void dfs3(int u, int pre)
{
++cnt;
ForE(i,u)
{
int v = e[i].to;
if(v == pre) continue;
dis = max(dis,dep[v]);
dfs3(v,u);
}
}
void solve()
{
cin >> n >> s >> m; ans = 0;
tot = 0; fill(head,head+n+1,0);
fill(hasS,hasS+n+1,0); fill(dep,dep+n+1,0); fill(inS,inS+n+1,0);
For(i,2,n) cin >> u >> v, add_edge(u,v), add_edge(v,u);
dfs(m,0);
ForE(i,m)
if(hasS[e[i].to])
dfs2(e[i].to,m);
For(i,1,n) if(!inS[i]) ans = max(ans,dep[s]+dep[i]);
u = s;
do
{
dis = cnt = 0;
ForE(i,u)
{
int v = e[i].to;
if(v != pre[u] && !hasS[v]) dfs3(v,u);
}
ans = max(ans,dep[s]+min(dis,cnt<<1));
u = pre[u];
}while(u != m);
cout << ans + mx << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5604kb
input:
1 4 2 3 1 2 1 3 1 4
output:
2
result:
wrong answer 1st numbers differ - expected: '4', found: '2'