QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#201306#7338. Education NightmarekjhhjkiWA 0ms5716kbC++141.6kb2023-10-05 13:38:022023-10-05 13:38:02

Judging History

你现在查看的是最新测评结果

  • [2023-10-05 13:38:02]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:5716kb
  • [2023-10-05 13:38:02]
  • 提交

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,n,m,s,u,v,dep[MAXN],sz[MAXN];
bool hasS[MAXN];
void dfs(int u, int pre, int flag)
{
    sz[u] = 1;
    if(u == s) flag = 1;
    hasS[u] = flag;
    ForE(i,u)
    {
        int v = e[i].to;
        if(v == pre) continue;
        dep[v] = dep[u] + 1;
        dfs(v,u,flag);
        if(hasS[v]) hasS[u] = 1;
        sz[u] += sz[v];
    }
}
void dfs2(int u, int pre)
{
    hasS[u] = 1;
    ForE(i,u)
    {
        int v = e[i].to;
        if(v == pre) continue;
        dfs2(v,u);
    }
}
void solve()
{
    cin >> n >> s >> m; mx = ans = 0;
    tot = 0; fill(head,head+n+1,0); 
    fill(hasS,hasS+n+1,0); fill(sz,sz+n+1,0); fill(dep,dep+n+1,0);
    For(i,2,n) cin >> u >> v, add_edge(u,v), add_edge(v,u);
    dfs(m,0,0);
    if(s != m)
    {
        ForE(i,m)
            if(hasS[e[i].to])
            {
                dfs2(e[i].to,m);
                ans += sz[e[i].to]<<1;
            }
        ans -= dep[s]+2;
    }
    For(i,1,n) 
        if(!hasS[i])
            mx = max(mx,dep[i]);
    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: 0ms
memory: 5716kb

input:

1
4 2 3
1 2
1 3
1 4

output:

2

result:

wrong answer 1st numbers differ - expected: '4', found: '2'