QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201833#7338. Education NightmarekjhhjkiWA 812ms18236kbC++142.3kb2023-10-05 16:58:072023-10-05 16:58:08

Judging History

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

  • [2023-10-05 16:58:08]
  • 评测
  • 测评结果:WA
  • 用时:812ms
  • 内存:18236kb
  • [2023-10-05 16:58:07]
  • 提交

answer

#pragma GCC optimize(2)
#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,pos,n,m,s,u,v,sz,dep[MAXN],pre[MAXN],cst[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;
    }
    sz += hasS[u];
}
void dfs2(int u, int pre)
{
    inS[u] = 1;
    if(hasS[u]) cst[u] = 0;
    ForE(i,u)
    {
        int v = e[i].to;
        if(v == pre) continue;
        cst[v] = cst[u] + 1;
        dfs2(v,u);
    }
}

int t,a[MAXN];
void solve()
{
    cin >> n >> s >> m; cnt = sz = mx = ans = 0; int f = 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);
    fill(cst,cst+n+1,0); 
    For(i,2,n) cin >> u >> v, add_edge(u,v), add_edge(v,u);
    dfs(m,0); inS[m] = 1;
    ForE(i,m)
        if(hasS[e[i].to]) 
            dfs2(e[i].to,m);
    For(i,1,n) if(!inS[i]) mx = max(mx,dep[i]), f = 1;
    u = s; ans = mx + dep[s];
    if(s == m)
    {
        For(i,1,n) ans = max(ans,dep[i]);
        cout << ans << '\n';
        return;
    }
    For(i,1,n) if(inS[i] && !hasS[i]) a[++cnt] = i;
    sort(a+1,a+1+cnt,[](int x, int y){ return dep[x] == dep[y]? cst[x] < cst[y]: dep[x] > dep[y]; });
    ans = dep[s] + max(mx,dep[a[1]]); --sz;
    for(pos = 1; pos <= cnt;)
    {
        u = a[pos];
        while(1)
        {
            if(hasS[u]) break;
            hasS[u] = 1; ++sz;
            u = pre[u];
        }
        while(pos <= cnt && hasS[a[pos]]) ++pos;
        ans = min(ans,sz*2-dep[s]+max(mx,dep[a[pos]]));
    }
    cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> T;
    while(T--) solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 7768kb

input:

1
4 2 3
1 2
1 3
1 4

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: -100
Wrong Answer
time: 812ms
memory: 18236kb

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
6
9
131
8
153
10
13
95
7
76
166
4
85
2
118
7
121
132
128
69
7
158
0
96
91
6
108
90
7
9
4
97
70
94
13
3
6
10
10
12
8
5
136
115
90
5
84
130
3
6
5
114
11
148
84
4
1
134
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:

wrong answer 5th numbers differ - expected: '3', found: '6'