QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546812#7961. 一棵树wangjingheng#WA 135ms56036kbC++20990b2024-09-04 14:02:042024-09-04 14:02:05

Judging History

This is the latest submission verdict.

  • [2024-09-04 14:02:05]
  • Judged
  • Verdict: WA
  • Time: 135ms
  • Memory: 56036kb
  • [2024-09-04 14:02:04]
  • Submitted

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10;
int f[N],s[N],d[N];
vector<int> g[N];
int n,k;
void dfs(int x,int fa){
    s[x]=1;
    for(auto it:g[x])if(it!=fa){
        dfs(it,x);
        f[x]=max(f[x],s[it]);
        s[x]+=s[it];
    }
    f[x]=max(f[x],n-s[x]);
}
void dfss(int x,int fa){
    d[x]=d[fa]+1;
    for(auto it:g[x])if(it!=fa){
        dfss(it,x);
    }
}
void solve(){
    cin>>n>>k;
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int ans=k*(n-1);
    dfs(1,0);
    d[0]=-1;
    int minx=1e9,root=0;
    for(int i=1;i<=n;i++){
        if(minx>f[i]) minx=f[i],root=i;
    }
    dfss(root,0);
    sort(d+1,d+n+1);
    for(int i=n;i>=n-k+1;i--){
        ans-=2*d[i];
    }
    cout<<ans<<"\n";
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 132ms
memory: 45748kb

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: -100
Wrong Answer
time: 135ms
memory: 56036kb

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:

195357165502

result:

wrong answer 1st lines differ - expected: '195357165512', found: '195357165502'