QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#153340 | #4901. Speike & Tom | zhouhuanyi | 40 | 132ms | 10112kb | C++14 | 2.0kb | 2023-08-29 22:23:44 | 2023-08-29 22:23:45 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#define N 100000
using namespace std;
const int inf=(int)(1e9);
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
int n,m,ans,fa[N+1],depth[N+1],dis[N+1],DP[N+1];
vector<int>E[N+1];
vector<int>ES[N+1];
bool used[N+1],vis[N+1],vst[N+1];
void add(int x,int y)
{
E[x].push_back(y),E[y].push_back(x);
return;
}
void add2(int x,int y)
{
ES[x].push_back(y),ES[y].push_back(x);
return;
}
void dfs(int x)
{
used[x]=1;
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i]])
fa[E[x][i]]=x,dfs(E[x][i]);
return;
}
void dfs2(int x)
{
int rt=0,minn=inf,minn2=inf;
used[x]=1,vst[x]=vis[x];
for (int i=0;i<ES[x].size();++i)
if (fa[fa[x]]==ES[x][i])
dis[x]=min(dis[x],dis[ES[x][i]]+1);
if (vis[x]) DP[x]=min(DP[x],depth[x]+dis[x]+1);
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i]])
{
fa[E[x][i]]=x,depth[E[x][i]]=depth[x]+1,dis[E[x][i]]=dis[x]+1,dfs2(E[x][i]),vst[x]|=vst[E[x][i]];
if (vst[E[x][i]])
{
if (depth[x]+dis[E[x][i]]<minn) minn2=minn,minn=depth[x]+dis[E[x][i]],rt=i;
else if (depth[x]+dis[E[x][i]]<minn2) minn2=depth[x]+dis[E[x][i]];
}
}
for (int i=0;i<E[x].size();++i)
if (fa[E[x][i]]==x)
{
if (rt==i) DP[E[x][i]]=min(DP[E[x][i]],minn2);
else DP[E[x][i]]=min(DP[E[x][i]],minn);
}
return;
}
void dfs3(int x)
{
for (int i=0;i<E[x].size();++i)
if (fa[E[x][i]]==x)
DP[E[x][i]]=min(DP[E[x][i]],DP[x]),dfs3(E[x][i]);
return;
}
int main()
{
int x,y;
n=read(),m=read();
for (int i=1;i<=n-1;++i) x=read(),y=read(),add(x,y);
dfs(1);
for (int i=1;i<=m;++i)
{
x=read(),y=read(),add2(x,y);
if (fa[x]!=y&&fa[y]!=x&&fa[x]!=fa[y]&&fa[fa[x]]!=y&&x!=fa[fa[y]]) vis[x]=vis[y]=1;
}
for (int i=1;i<=n;++i)
{
for (int j=1;j<=n;++j) used[j]=fa[j]=0,DP[j]=inf;
depth[i]=1,dis[i]=0,dfs2(i),dfs3(i);
for (int j=1;j<=n;++j)
if (i!=j&&depth[j]>=DP[j])
ans++;
}
printf("%d\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 3ms
memory: 9940kb
input:
20 3 1 2 1 3 3 4 4 5 1 6 6 7 1 8 5 9 8 10 5 11 7 12 11 13 12 14 11 15 4 16 7 17 2 18 1 19 3 20 8 20 12 4 10 1
output:
307
result:
ok 1 number(s): "307"
Test #2:
score: 0
Accepted
time: 1ms
memory: 8512kb
input:
20 4 1 2 2 3 1 4 3 5 4 6 3 7 4 8 3 9 2 10 8 11 8 12 7 13 8 14 14 15 4 16 1 17 4 18 6 19 19 20 9 13 14 2 14 13 15 6
output:
343
result:
ok 1 number(s): "343"
Test #3:
score: 0
Accepted
time: 0ms
memory: 10056kb
input:
19 4 1 2 1 3 1 4 4 5 4 6 3 7 7 8 6 9 4 10 2 11 6 12 7 13 1 14 13 15 6 16 12 17 3 18 3 19 8 12 4 6 18 5 11 9
output:
314
result:
ok 1 number(s): "314"
Test #4:
score: 0
Accepted
time: 2ms
memory: 9164kb
input:
20 3 1 2 2 3 3 4 4 5 1 6 2 7 1 8 3 9 9 10 5 11 8 12 10 13 12 14 8 15 9 16 11 17 5 18 17 19 19 20 9 19 10 15 18 15
output:
358
result:
ok 1 number(s): "358"
Test #5:
score: 0
Accepted
time: 1ms
memory: 9932kb
input:
20 6 1 2 1 3 3 4 4 5 1 6 6 7 5 8 4 9 7 10 6 11 10 12 6 13 10 14 1 15 15 16 8 17 17 18 15 19 18 20 17 2 16 3 10 1 8 7 13 5 19 7
output:
361
result:
ok 1 number(s): "361"
Test #6:
score: 0
Accepted
time: 2ms
memory: 9972kb
input:
20 8 1 2 2 3 1 4 1 5 1 6 4 7 2 8 4 9 2 10 1 11 11 12 2 13 10 14 1 15 6 16 16 17 11 18 10 19 3 20 2 6 6 7 1 18 18 13 1 20 3 12 8 4 7 1
output:
324
result:
ok 1 number(s): "324"
Subtask #2:
score: 15
Accepted
Dependency #1:
100%
Accepted
Test #7:
score: 15
Accepted
time: 3ms
memory: 9492kb
input:
300 26 1 2 1 3 2 4 4 5 3 6 6 7 5 8 2 9 1 10 10 11 6 12 8 13 6 14 10 15 6 16 4 17 9 18 10 19 5 20 18 21 6 22 10 23 18 24 1 25 19 26 17 27 8 28 10 29 25 30 16 31 27 32 13 33 4 34 5 35 12 36 9 37 15 38 32 39 29 40 11 41 5 42 28 43 1 44 25 45 27 46 3 47 34 48 27 49 9 50 39 51 20 52 48 53 10 54 35 55 23 ...
output:
86687
result:
ok 1 number(s): "86687"
Test #8:
score: 0
Accepted
time: 4ms
memory: 9592kb
input:
300 40 1 2 1 3 3 4 4 5 5 6 3 7 6 8 5 9 2 10 1 11 10 12 11 13 7 14 10 15 6 16 7 17 5 18 12 19 15 20 18 21 15 22 8 23 12 24 23 25 18 26 10 27 4 28 4 29 16 30 20 31 15 32 9 33 6 34 33 35 22 36 27 37 12 38 21 39 37 40 29 41 41 42 32 43 4 44 1 45 12 46 34 47 24 48 20 49 33 50 25 51 47 52 6 53 25 54 5 55 ...
output:
88364
result:
ok 1 number(s): "88364"
Test #9:
score: 0
Accepted
time: 0ms
memory: 8592kb
input:
299 100 1 2 1 3 1 4 3 5 3 6 4 7 1 8 6 9 5 10 8 11 1 12 2 13 11 14 3 15 8 16 14 17 11 18 2 19 3 20 10 21 3 22 17 23 23 24 16 25 7 26 5 27 12 28 8 29 7 30 29 31 23 32 1 33 1 34 29 35 7 36 5 37 6 38 19 39 22 40 21 41 5 42 29 43 19 44 38 45 23 46 21 47 29 48 27 49 28 50 47 51 25 52 28 53 9 54 45 55 52 5...
output:
88551
result:
ok 1 number(s): "88551"
Test #10:
score: 0
Accepted
time: 4ms
memory: 9672kb
input:
300 10 1 2 2 3 3 4 1 5 2 6 5 7 5 8 7 9 2 10 7 11 1 12 4 13 2 14 5 15 8 16 7 17 11 18 13 19 14 20 2 21 8 22 18 23 23 24 16 25 22 26 13 27 9 28 22 29 3 30 19 31 28 32 6 33 12 34 9 35 27 36 17 37 36 38 17 39 9 40 37 41 32 42 11 43 37 44 42 45 34 46 26 47 39 48 16 49 42 50 24 51 39 52 38 53 32 54 24 55 ...
output:
82854
result:
ok 1 number(s): "82854"
Test #11:
score: 0
Accepted
time: 4ms
memory: 9764kb
input:
300 67 1 2 2 3 3 4 1 5 5 6 4 7 6 8 4 9 5 10 4 11 10 12 2 13 8 14 9 15 3 16 4 17 5 18 12 19 1 20 17 21 6 22 4 23 18 24 18 25 14 26 20 27 7 28 26 29 2 30 14 31 20 32 6 33 30 34 29 35 19 36 33 37 12 38 7 39 35 40 9 41 3 42 42 43 33 44 27 45 25 46 7 47 5 48 24 49 47 50 23 51 19 52 41 53 19 54 1 55 27 56...
output:
88372
result:
ok 1 number(s): "88372"
Test #12:
score: 0
Accepted
time: 4ms
memory: 9400kb
input:
280 280 1 2 2 3 3 4 3 5 4 6 2 7 6 8 5 9 4 10 5 11 5 12 12 13 13 14 5 15 13 16 16 17 7 18 10 19 6 20 10 21 4 22 8 23 10 24 15 25 14 26 20 27 17 28 19 29 28 30 27 31 26 32 19 33 6 34 15 35 14 36 33 37 1 38 23 39 33 40 4 41 8 42 10 43 7 44 8 45 4 46 37 47 25 48 38 49 30 50 49 51 31 52 23 53 9 54 46 55 ...
output:
78036
result:
ok 1 number(s): "78036"
Subtask #3:
score: 15
Accepted
Dependency #2:
100%
Accepted
Test #13:
score: 15
Accepted
time: 3ms
memory: 8516kb
input:
300 26 1 2 1 3 2 4 4 5 3 6 6 7 5 8 2 9 1 10 10 11 6 12 8 13 6 14 10 15 6 16 4 17 9 18 10 19 5 20 18 21 6 22 10 23 18 24 1 25 19 26 17 27 8 28 10 29 25 30 16 31 27 32 13 33 4 34 5 35 12 36 9 37 15 38 32 39 29 40 11 41 5 42 28 43 1 44 25 45 27 46 3 47 34 48 27 49 9 50 39 51 20 52 48 53 10 54 35 55 23 ...
output:
86687
result:
ok 1 number(s): "86687"
Test #14:
score: 0
Accepted
time: 0ms
memory: 8540kb
input:
8 2 5 1 6 2 1 3 8 4 3 7 2 1 1 4 2 3 8 5
output:
34
result:
ok 1 number(s): "34"
Test #15:
score: 0
Accepted
time: 19ms
memory: 10096kb
input:
1000 80 1 2 1 3 3 4 1 5 1 6 4 7 3 8 7 9 5 10 9 11 6 12 6 13 3 14 5 15 7 16 4 17 13 18 12 19 13 20 13 21 8 22 10 23 3 24 18 25 7 26 2 27 15 28 14 29 1 30 8 31 22 32 28 33 3 34 34 35 4 36 25 37 3 38 25 39 29 40 29 41 22 42 10 43 27 44 12 45 27 46 27 47 39 48 42 49 13 50 30 51 46 52 34 53 16 54 49 55 1...
output:
988373
result:
ok 1 number(s): "988373"
Test #16:
score: 0
Accepted
time: 104ms
memory: 8992kb
input:
2000 40 1 2 2 3 1 4 4 5 5 6 1 7 7 8 3 9 7 10 3 11 7 12 12 13 3 14 2 15 15 16 16 17 4 18 3 19 18 20 17 21 1 22 17 23 7 24 16 25 6 26 14 27 12 28 13 29 15 30 13 31 30 32 30 33 33 34 28 35 22 36 9 37 7 38 10 39 12 40 5 41 26 42 10 43 27 44 1 45 18 46 34 47 26 48 47 49 24 50 34 51 37 52 28 53 15 54 32 5...
output:
3822427
result:
ok 1 number(s): "3822427"
Test #17:
score: 0
Accepted
time: 101ms
memory: 9584kb
input:
2000 10 1 2 1 3 1 4 3 5 1 6 6 7 4 8 2 9 1 10 6 11 3 12 9 13 13 14 14 15 12 16 14 17 15 18 15 19 16 20 13 21 4 22 9 23 2 24 4 25 1 26 13 27 7 28 20 29 4 30 9 31 14 32 14 33 8 34 5 35 18 36 31 37 11 38 37 39 17 40 20 41 40 42 29 43 24 44 21 45 15 46 38 47 1 48 47 49 35 50 32 51 26 52 9 53 14 54 25 55 ...
output:
3501231
result:
ok 1 number(s): "3501231"
Test #18:
score: 0
Accepted
time: 106ms
memory: 9140kb
input:
2000 200 1 2 2 3 1 4 3 5 2 6 4 7 2 8 1 9 4 10 9 11 9 12 8 13 10 14 9 15 6 16 5 17 5 18 4 19 3 20 11 21 14 22 13 23 20 24 15 25 24 26 19 27 16 28 23 29 28 30 30 31 16 32 5 33 20 34 1 35 9 36 5 37 30 38 15 39 6 40 40 41 31 42 25 43 28 44 42 45 2 46 7 47 34 48 2 49 35 50 42 51 28 52 38 53 35 54 51 55 2...
output:
3970625
result:
ok 1 number(s): "3970625"
Test #19:
score: 0
Accepted
time: 132ms
memory: 10112kb
input:
1998 1996 1 2 2 3 3 4 1 5 1 6 6 7 7 8 8 9 2 10 4 11 8 12 12 13 7 14 4 15 13 16 16 17 3 18 14 19 10 20 7 21 9 22 16 23 13 24 21 25 18 26 18 27 11 28 27 29 16 30 12 31 13 32 12 33 29 34 27 35 1 36 29 37 3 38 14 39 26 40 11 41 21 42 27 43 35 44 17 45 20 46 43 47 23 48 39 49 30 50 27 51 46 52 11 53 29 5...
output:
3989549
result:
ok 1 number(s): "3989549"
Test #20:
score: 0
Accepted
time: 110ms
memory: 10056kb
input:
1999 3 1 2 1 3 3 4 4 5 4 6 5 7 4 8 2 9 9 10 9 11 8 12 5 13 9 14 8 15 14 16 12 17 5 18 3 19 15 20 12 21 15 22 17 23 18 24 11 25 21 26 17 27 1 28 10 29 25 30 7 31 21 32 31 33 5 34 7 35 26 36 29 37 33 38 11 39 10 40 25 41 39 42 8 43 6 44 24 45 5 46 45 47 21 48 30 49 6 50 35 51 27 52 23 53 41 54 36 55 1...
output:
2959137
result:
ok 1 number(s): "2959137"
Subtask #4:
score: 0
Time Limit Exceeded
Dependency #3:
100%
Accepted
Test #21:
score: 20
Accepted
time: 2ms
memory: 8808kb
input:
8 2 5 1 6 2 1 3 8 4 3 7 2 1 1 4 2 3 8 5
output:
34
result:
ok 1 number(s): "34"
Test #22:
score: -20
Time Limit Exceeded
input:
40000 1000 1 2 1 3 3 4 3 5 3 6 1 7 3 8 6 9 9 10 2 11 3 12 7 13 12 14 8 15 13 16 5 17 11 18 15 19 1 20 12 21 11 22 15 23 10 24 12 25 12 26 3 27 3 28 28 29 24 30 29 31 23 32 1 33 21 34 14 35 23 36 3 37 34 38 12 39 24 40 25 41 40 42 27 43 9 44 31 45 35 46 31 47 18 48 21 49 44 50 48 51 2 52 44 53 18 54 ...
output:
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #29:
score: 0
Time Limit Exceeded
input:
98765 1 2 1 3 1 4 2 5 2 6 5 7 4 8 6 9 7 10 7 11 6 12 1 13 11 14 13 15 7 16 6 17 14 18 4 19 13 20 14 21 11 22 21 23 1 24 13 25 7 26 16 27 8 28 21 29 20 30 10 31 12 32 10 33 7 34 31 35 29 36 29 37 30 38 34 39 38 40 14 41 40 42 26 43 33 44 1 45 44 46 25 47 14 48 2 49 30 50 26 51 46 52 34 53 32 54 31 55...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
0%