QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#286469#7961. 一棵树gyh20WA 762ms41240kbC++143.5kb2023-12-17 22:21:182023-12-17 22:21:18

Judging History

This is the latest submission verdict.

  • [2023-12-17 22:21:18]
  • Judged
  • Verdict: WA
  • Time: 762ms
  • Memory: 41240kb
  • [2023-12-17 22:21:18]
  • Submitted

answer

#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
    re int t=0;re char v=getchar();
    while(v<'0')v=getchar();
    while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
    return t;
}
int t,n,m,head[1000002],mn,pos,siz[1000002],Z,bl[1000002],num[1000002],dis[1000002],cnt,P,S[500002],deg[500002];
char v[1000002];
long long ans;
struct edge{
    int to,next;
}e[1000002];
struct node{
    int x,y;
    bool operator <(const node A)const{return x>A.x;}
}p[500002];
inline void add(re int x,re int y){e[++cnt]=(edge){y,head[x]},head[x]=cnt;}
inline void dfs(re int x,re int y){
    siz[x]=1;re int mx=0;
    for(re int i=head[x];i;i=e[i].next)
        if(!v[e[i].to]&&(e[i].to^y))
            dfs(e[i].to,x),siz[x]+=siz[e[i].to],mx=max(mx,siz[e[i].to]);
    mx=max(mx,Z-siz[x]);
    if(mx<mn)mn=mx,pos=x;
}
inline void dfs1(re int x,re int y,re int z){
    bl[x]=z,dis[x]=dis[y]+1;
    for(re int i=head[x];i;i=e[i].next)
        if(e[i].to^y)
            dfs1(e[i].to,x,z);
}
inline void chk(re int x){
    memset(num,0,sizeof(num));
    re long long s=0;
    bl[x]=x,dis[x]=0;
    for(re int i=head[x];i;i=e[i].next)dfs1(e[i].to,x,e[i].to);
    for(re int i=1;i<=n;++i)p[i]=(node){dis[i],i};
    sort(p+1,p+n+1);
    P=0;int o=1;
    for(re int i=1;i<=m;++i){
        if(num[bl[p[o].y]]==(m>>1)&&P==0)P=bl[p[o].y];
        while(o<=n&&num[bl[p[o].y]]==(m>>1))++o;
        if(o>n){
            s=1e18;
            break;
        }
        s+=(n-1-p[o].x-p[o].x);
        ++num[bl[p[o].y]];++o;
    }
    ans=min(ans,s);
}
inline void dfs2(re int x,re int y){
    for(re int i=head[x];i;i=e[i].next)
        if(e[i].to^y){
            dfs2(e[i].to,x);
            S[x]+=S[e[i].to];
        }
    if(P==0&&S[x]>=(m>>1)+1)P=x;
}
inline void chk1(re int x){
    memset(num,0,sizeof(num));
    re long long s=0;
    bl[x]=x,dis[x]=0;
    for(re int i=head[x];i;i=e[i].next)dfs1(e[i].to,x,e[i].to);
    for(re int i=1;i<=n;++i)p[i]=(node){dis[i],i};
    sort(p+1,p+n+1);
    P=0;int o=1;
    for(re int i=1;i<=m;++i){
    /*    if(num[bl[p[o].y]]==(m>>1)&&P==0){
            memset(S,0,sizeof(S));
            for(re int j=1;j<=o;++j)if(bl[p[o].y]==bl[p[j].y])S[p[j].y]=1;
            dfs2(x,x);	
        }*/
        while(o<=n&&num[bl[p[o].y]]==(m>>1))++o;
        if(o>n){
            s=1e18;
            break;
        }
        s+=(n-1-p[o].x-p[o].x);
        ++num[bl[p[o].y]];++o;
    }
    ans=min(ans,s);
    memset(num,0,sizeof(num));
    o=1;
    for(re int i=1;i<=m;++i){
        ++num[bl[p[o].y]];++o;
    }
    memset(S,0,sizeof(S)),P=0;
    for(re int j=1;j<=m;++j)if(num[bl[p[j].y]]*2>m)S[p[j].y]=1;
    dfs2(x,x);
}
inline void dfz(re int x,re int y){
    Z=y,mn=1e9,dfs(x,x),x=pos,dfs(x,x),v[x]=1;
    chk(x);
    if(P&&!v[P])dfz(P,siz[P]);
}

int main(){ans=1e18;
//freopen("a.in","r",stdin);
    n=read(),m=read();
    if(m==1){
        printf("%d\n",n-1);
        return 0;
    }
    for(re int i=1,x,y;i<n;++i)x=read(),y=read(),add(x,y),add(y,x),++deg[x],++deg[y];
    for(re int i=1;i<=n;++i)if(deg[i]>=deg[P])P=i;
    chk1(P);    
	for(re int i=1;i<=n;++i)if(deg[i]>deg[P])P=i;
    chk1(P);
    mn=1e9,Z=n,dfs(1,1),P=pos;//printf("%d\n",P);
//    dfs3(1,1,0);
//	dfz(1,n);
//printf("%lld\n",ans);
//	re long long tmp=ans;
//	for(re int i=1;i<=n;++i)chk(i);
//	printf("%lld\n",ans);
//	assert(tmp==ans);
    ans=1e18;
    for(re int i=1;i<=15&&P;++i)chk1(P);
    printf("%lld",ans);
}

詳細信息

Test #1:

score: 100
Accepted
time: 725ms
memory: 37668kb

input:

499991 31473
1 2
2 3
1 4
2 5
4 6
4 7
6 8
4 9
8 10
6 11
9 12
10 13
10 14
1 15
14 16
3 17
14 18
12 19
13 20
11 21
16 22
16 23
20 24
5 25
16 26
18 27
8 28
15 29
27 30
27 31
26 32
21 33
3 34
27 35
33 36
25 37
22 38
11 39
11 40
17 41
25 42
3 43
3 44
2 45
35 46
25 47
5 48
6 49
41 50
42 51
1 52
39 53
14 54...

output:

15734930984

result:

ok single line: '15734930984'

Test #2:

score: 0
Accepted
time: 762ms
memory: 40060kb

input:

499993 461706
1 2
2 3
3 4
1 5
3 6
3 7
7 8
5 9
5 10
7 11
10 12
9 13
13 14
12 15
13 16
15 17
14 18
17 19
16 20
15 21
16 22
17 23
22 24
20 25
23 26
25 27
27 28
28 29
25 30
29 31
30 32
31 33
33 34
33 35
32 36
36 37
35 38
33 39
34 40
35 41
41 42
40 43
42 44
39 45
40 46
41 47
43 48
44 49
46 50
49 51
50 52...

output:

195357165512

result:

ok single line: '195357165512'

Test #3:

score: 0
Accepted
time: 569ms
memory: 40292kb

input:

499993 170472
1 2
1 3
1 4
4 5
5 6
4 7
6 8
5 9
8 10
6 11
6 12
10 13
13 14
11 15
14 16
15 17
14 18
16 19
16 20
17 21
17 22
20 23
19 24
21 25
23 26
22 27
27 28
24 29
24 30
28 31
31 32
30 33
29 34
29 35
35 36
33 37
34 38
36 39
36 40
36 41
41 42
40 43
41 44
39 45
45 46
42 47
47 48
44 49
45 50
46 51
50 52...

output:

65062419628

result:

ok single line: '65062419628'

Test #4:

score: -100
Wrong Answer
time: 443ms
memory: 41240kb

input:

499994 801
1 2
2 3
1 4
2 5
2 6
1 7
7 8
4 9
6 10
10 11
7 12
7 13
10 14
13 15
15 16
14 17
16 18
18 19
15 20
17 21
18 22
19 23
21 24
23 25
21 26
26 27
27 28
24 29
28 30
29 31
27 32
30 33
28 34
29 35
35 36
31 37
36 38
36 39
38 40
36 41
36 42
38 43
38 44
42 45
41 46
41 47
47 48
46 49
45 50
49 51
49 52
48...

output:

285905411

result:

wrong answer 1st lines differ - expected: '285905307', found: '285905411'