QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#16517 | #791. Mousetrap | jahayo | 20 | 330ms | 70300kb | C++20 | 2.0kb | 2021-12-20 18:28:14 | 2022-05-04 09:11:59 |
Judging History
answer
#include<cstdio>
#include<vector>
using namespace std;
//令 f_i 表示耗子从这个节点进入子树后又把把耗子赶回来的最小步数
//取次大儿子转移,因为可以把通往 wi 最大的儿子的路封上,
//耗子就会走向次大儿子。
int m,n,from,to,root,oo,sm[1000500],s[1000500],top,
f[1000500],du[1000500],fa[1000500];
vector<int> g[1000500];
bool e[1000500],asd;
void dfs(int num,int last)
{
fa[num]=last;//还顺便处理了一个fa数组
if(du[num]==0){f[num]=0;return ;}//边界
int h1=0,h2=0;//h1:最大值; h2:次大值
for (int i=0;i<g[num].size();i++)
if (g[num][i]!=last){
dfs(g[num][i],num);
if (f[g[num][i]]>=h1)//注意等于号
{h2=h1;h1=f[g[num][i]];}
}f[num]=h2+du[num]-1;
}
//s数组按顺序(老鼠->根)存的是老鼠开始时的结点到根路径上的所有节点
bool check(int k)
{
int sum=0,tmp;
//有这个tmp的缘故是因为在j的循环内k--会WA,同一个结点的子树的k要求必然相同(感性理解,谢谢)
for(int i=1;i<top;i++){
tmp=0;
for(int j=0;j<g[s[i]].size();j++){
int v=g[s[i]][j];//某棵子树
if(v!=s[i+1]&&//往上走的情况不在这里考虑,应该在下一次循环
v!=s[i-1]&&//不能走回头路
sm[i]+f[v]+1-(i!=1)>k)//不堵上就会TLE
tmp++;//堵上
}sum+=tmp;k-=tmp;//总操作次数增加,剩余操作次数减少
if(k<0||//封堵的总次数>k
sum>i)//管理者的手速不够快
return 0;
}return 1;
}
int main()
{
scanf("%d%d%d",&n,&root,&m);
for (int i=1;i<n;i++){
from=i;to=i+1;
scanf("%d%d",&from,&to);
g[from].push_back(to);
g[to].push_back(from);
du[from]++;du[to]++;
}dfs(root,0);
f[root]=0;//边界
for(int i=m;i;i=fa[i])s[++top]=i;
for(int i=top-1;i;i--)sm[i]=sm[i+1]+du[s[i]]-1-(s[i]!=root);
int l=0,r=1e8,mid;
while(l<r){
mid=(l+r)>>1;
if(check(mid))r=mid;
else l=mid+1;//二分,这里要+1
}printf("%d",r);
return 0;
}
详细
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 4ms
memory: 26548kb
input:
10 2 10 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 1ms
memory: 26992kb
input:
10 1 10 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 8ms
memory: 27220kb
input:
10 1 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 12ms
memory: 26576kb
input:
9 1 7 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 7ms
memory: 26864kb
input:
10 1 10 1 2 3 2 4 3 5 3 6 4 7 3 8 7 9 2 10 5
output:
3
result:
ok single line: '3'
Test #6:
score: 0
Accepted
time: 4ms
memory: 27052kb
input:
8 1 8 1 2 3 2 4 3 5 4 6 2 7 6 8 3
output:
2
result:
ok single line: '2'
Test #7:
score: 0
Accepted
time: 3ms
memory: 26640kb
input:
10 3 2 3 1 1 2 2 4 2 5 2 6 7 5 8 5 9 6 10 6
output:
5
result:
ok single line: '5'
Test #8:
score: 0
Accepted
time: 4ms
memory: 26708kb
input:
10 2 3 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 1 10
output:
7
result:
ok single line: '7'
Test #9:
score: 0
Accepted
time: 6ms
memory: 26788kb
input:
7 1 2 2 1 3 2 4 2 5 3 6 3 7 4
output:
3
result:
ok single line: '3'
Test #10:
score: 0
Accepted
time: 8ms
memory: 27220kb
input:
2 1 2 1 2
output:
0
result:
ok single line: '0'
Subtask #2:
score: 0
Wrong Answer
Test #11:
score: 0
Wrong Answer
time: 330ms
memory: 70300kb
input:
1000000 1 2 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 53 27 5...
output:
4
result:
wrong answer 1st lines differ - expected: '36', found: '4'
Subtask #3:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #17:
score: 20
Accepted
time: 3ms
memory: 27528kb
input:
43 2 5 1 2 1 3 3 4 4 5 5 11 11 12 11 13 4 9 6 9 9 14 9 15 9 16 4 10 10 17 10 18 10 19 10 20 8 21 8 22 8 23 8 24 8 25 8 26 8 27 8 28 8 29 8 30 1 8 7 1 7 31 7 32 7 33 7 34 7 35 7 36 7 37 7 38 7 39 7 40 9 41 10 42 10 43
output:
7
result:
ok single line: '7'
Test #18:
score: 0
Accepted
time: 12ms
memory: 27564kb
input:
1000 1 1000 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 53 27 5...
output:
9
result:
ok single line: '9'
Test #19:
score: 0
Accepted
time: 0ms
memory: 27268kb
input:
999 1 20 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 53 27 54 2...
output:
14
result:
ok single line: '14'
Test #20:
score: 0
Accepted
time: 5ms
memory: 26908kb
input:
1000 1 998 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 ...
output:
1
result:
ok single line: '1'
Test #21:
score: 0
Accepted
time: 8ms
memory: 27516kb
input:
901 1 901 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 5...
output:
0
result:
ok single line: '0'
Test #22:
score: 0
Accepted
time: 3ms
memory: 26716kb
input:
1000 1 150 1 2 3 2 4 3 5 2 6 5 7 2 8 6 9 2 10 4 11 3 12 10 13 12 14 11 15 3 16 12 17 12 18 14 19 18 20 10 21 4 22 13 23 13 24 13 25 10 26 13 27 17 28 20 29 14 30 29 31 21 32 29 33 27 34 19 35 7 36 24 37 2 38 6 39 17 40 19 41 40 42 23 43 15 44 17 45 25 46 14 47 24 48 13 49 32 50 28 51 3 52 42 53 52 5...
output:
31
result:
ok single line: '31'
Test #23:
score: -20
Wrong Answer
time: 7ms
memory: 26736kb
input:
998 1 833 1 2 3 2 4 3 5 4 6 4 7 2 8 7 9 7 10 5 11 3 12 3 13 10 14 6 15 10 16 13 17 2 18 13 19 8 20 15 21 10 22 11 23 13 24 13 25 5 26 21 27 4 28 12 29 4 30 9 31 28 32 21 33 32 34 24 35 33 36 14 37 29 38 31 39 5 40 6 41 12 42 24 43 27 44 21 45 12 46 18 47 13 48 39 49 40 50 13 51 50 52 3 53 40 54 32 5...
output:
39
result:
wrong answer 1st lines differ - expected: '40', found: '39'
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%