Submission #565607

# Submission time Handle Problem Language Result Execution time Memory
565607 2022-05-21T07:22:11 Z shahriarkhan Village (BOI20_village) C++14
50 / 100
110 ms 31948 KB
#include<bits/stdc++.h>
using namespace std ;

const int MX = 1e5 + 5 , INF = 1e9 + 5 ;

int dp[MX][2] , mn[MX] ;

vector<int> adj[MX] ;

vector<pair<int,int> > pick[MX][2] ;

void dfs(int s , int p)
{
    int sum = 0 , good = 0 , leaf = 1 , rd = 0 , pos = 0 , idx = 0 ;
    dp[s][0] = 0 , dp[s][1] = INF ;
    for(int t : adj[s])
    {
        if(t==p) continue ;
        dfs(t,s) , leaf = 0 ;
        dp[s][0] = min(dp[s][0] + dp[t][1] , INF) , pick[s][0].push_back({t,1}) ;
        if((dp[t][0]+2)<=dp[t][1])
        {
            sum += dp[t][0] + 2 , pick[s][1].push_back({t,0}) , ++pos ;
            good = 1 ;
        }
        else
        {
            if(dp[t][0]<dp[t][1])
            {
                rd = t ;
                idx = pos ;
            }
            sum += dp[t][1] , pick[s][1].push_back({t,1}) , ++pos;
        }
    }
    if(!leaf)
    {
        if(!good)
        {
            if(rd)
            {
                pick[s][1].erase(pick[s][1].begin()+idx) ;
                pick[s][1].push_back({rd,0}) ;
                sum -= dp[rd][1] ;
                sum += dp[rd][0] + 2 ;
            }
            else
            {
                pick[s][1][0].second = 2 ;
                sum += 2 ;
            }
        }
        dp[s][1] = sum ;
    }
}

void build(int s , int c)
{
    if(c==2) c = 1 ;
    for(pair<int,int> p : pick[s][c])
    {
        build(p.first,p.second) ;
        if(p.second!=1) swap(mn[p.first],mn[s]) ;
    }
}

int main()
{
    int n ;
    scanf("%d",&n) ;
    for(int i = 1 ; i < n ; ++i)
    {
        int x , y ;
        scanf("%d%d",&x,&y) ;
        adj[x].push_back(y) ;
        adj[y].push_back(x) ;
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        mn[i] = i ;
    }
    dfs(1,0) ;
    build(1,1) ;
    printf("%d %d\n",dp[1][1],1) ;
    for(int i = 1 ; i <= n ; ++i)
    {
        printf("%d ",mn[i]) ;
    }
    printf("\n") ;
    for(int i = 1 ; i <= n ; ++i)
    {
        printf("%d ",1) ;
    }
    printf("\n") ;
    return 0 ;
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     scanf("%d",&n) ;
      |     ~~~~~^~~~~~~~~
Village.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         scanf("%d%d",&x,&y) ;
      |         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 7252 KB Partially correct
2 Partially correct 5 ms 7252 KB Partially correct
3 Partially correct 4 ms 7352 KB Partially correct
4 Partially correct 4 ms 7360 KB Partially correct
5 Partially correct 4 ms 7252 KB Partially correct
6 Partially correct 4 ms 7252 KB Partially correct
7 Partially correct 4 ms 7352 KB Partially correct
8 Partially correct 5 ms 7380 KB Partially correct
9 Partially correct 4 ms 7252 KB Partially correct
10 Partially correct 4 ms 7252 KB Partially correct
11 Partially correct 4 ms 7252 KB Partially correct
12 Partially correct 4 ms 7252 KB Partially correct
13 Partially correct 4 ms 7252 KB Partially correct
14 Partially correct 4 ms 7252 KB Partially correct
15 Partially correct 4 ms 7356 KB Partially correct
16 Partially correct 5 ms 7440 KB Partially correct
17 Partially correct 4 ms 7252 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 7380 KB Partially correct
2 Partially correct 6 ms 7356 KB Partially correct
3 Partially correct 4 ms 7352 KB Partially correct
4 Partially correct 4 ms 7380 KB Partially correct
5 Partially correct 5 ms 7380 KB Partially correct
6 Partially correct 5 ms 7380 KB Partially correct
7 Partially correct 4 ms 7508 KB Partially correct
8 Partially correct 4 ms 7380 KB Partially correct
9 Partially correct 5 ms 7380 KB Partially correct
10 Partially correct 5 ms 7380 KB Partially correct
11 Partially correct 5 ms 7380 KB Partially correct
12 Partially correct 4 ms 7360 KB Partially correct
13 Partially correct 4 ms 7380 KB Partially correct
14 Partially correct 6 ms 7380 KB Partially correct
15 Partially correct 5 ms 7364 KB Partially correct
16 Partially correct 5 ms 7360 KB Partially correct
17 Partially correct 5 ms 7352 KB Partially correct
18 Partially correct 4 ms 7380 KB Partially correct
19 Partially correct 4 ms 7380 KB Partially correct
20 Partially correct 5 ms 7384 KB Partially correct
21 Partially correct 4 ms 7380 KB Partially correct
22 Partially correct 5 ms 7380 KB Partially correct
23 Partially correct 5 ms 7380 KB Partially correct
24 Partially correct 4 ms 7360 KB Partially correct
25 Partially correct 4 ms 7380 KB Partially correct
26 Partially correct 5 ms 7380 KB Partially correct
27 Partially correct 4 ms 7380 KB Partially correct
28 Partially correct 4 ms 7380 KB Partially correct
29 Partially correct 5 ms 7372 KB Partially correct
30 Partially correct 4 ms 7380 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 7252 KB Partially correct
2 Partially correct 5 ms 7252 KB Partially correct
3 Partially correct 4 ms 7352 KB Partially correct
4 Partially correct 4 ms 7360 KB Partially correct
5 Partially correct 4 ms 7252 KB Partially correct
6 Partially correct 4 ms 7252 KB Partially correct
7 Partially correct 4 ms 7352 KB Partially correct
8 Partially correct 5 ms 7380 KB Partially correct
9 Partially correct 4 ms 7252 KB Partially correct
10 Partially correct 4 ms 7252 KB Partially correct
11 Partially correct 4 ms 7252 KB Partially correct
12 Partially correct 4 ms 7252 KB Partially correct
13 Partially correct 4 ms 7252 KB Partially correct
14 Partially correct 4 ms 7252 KB Partially correct
15 Partially correct 4 ms 7356 KB Partially correct
16 Partially correct 5 ms 7440 KB Partially correct
17 Partially correct 4 ms 7252 KB Partially correct
18 Partially correct 4 ms 7380 KB Partially correct
19 Partially correct 6 ms 7356 KB Partially correct
20 Partially correct 4 ms 7352 KB Partially correct
21 Partially correct 4 ms 7380 KB Partially correct
22 Partially correct 5 ms 7380 KB Partially correct
23 Partially correct 5 ms 7380 KB Partially correct
24 Partially correct 4 ms 7508 KB Partially correct
25 Partially correct 4 ms 7380 KB Partially correct
26 Partially correct 5 ms 7380 KB Partially correct
27 Partially correct 5 ms 7380 KB Partially correct
28 Partially correct 5 ms 7380 KB Partially correct
29 Partially correct 4 ms 7360 KB Partially correct
30 Partially correct 4 ms 7380 KB Partially correct
31 Partially correct 6 ms 7380 KB Partially correct
32 Partially correct 5 ms 7364 KB Partially correct
33 Partially correct 5 ms 7360 KB Partially correct
34 Partially correct 5 ms 7352 KB Partially correct
35 Partially correct 4 ms 7380 KB Partially correct
36 Partially correct 4 ms 7380 KB Partially correct
37 Partially correct 5 ms 7384 KB Partially correct
38 Partially correct 4 ms 7380 KB Partially correct
39 Partially correct 5 ms 7380 KB Partially correct
40 Partially correct 5 ms 7380 KB Partially correct
41 Partially correct 4 ms 7360 KB Partially correct
42 Partially correct 4 ms 7380 KB Partially correct
43 Partially correct 5 ms 7380 KB Partially correct
44 Partially correct 4 ms 7380 KB Partially correct
45 Partially correct 4 ms 7380 KB Partially correct
46 Partially correct 5 ms 7372 KB Partially correct
47 Partially correct 4 ms 7380 KB Partially correct
48 Partially correct 85 ms 16352 KB Partially correct
49 Partially correct 94 ms 17232 KB Partially correct
50 Partially correct 84 ms 17324 KB Partially correct
51 Partially correct 65 ms 15088 KB Partially correct
52 Partially correct 110 ms 17180 KB Partially correct
53 Partially correct 74 ms 16204 KB Partially correct
54 Partially correct 46 ms 19204 KB Partially correct
55 Partially correct 103 ms 31948 KB Partially correct
56 Partially correct 103 ms 23916 KB Partially correct
57 Partially correct 102 ms 21736 KB Partially correct
58 Partially correct 100 ms 19824 KB Partially correct
59 Partially correct 88 ms 17228 KB Partially correct
60 Partially correct 57 ms 16232 KB Partially correct
61 Partially correct 56 ms 16076 KB Partially correct
62 Partially correct 69 ms 15960 KB Partially correct
63 Partially correct 59 ms 15564 KB Partially correct
64 Partially correct 68 ms 16452 KB Partially correct
65 Partially correct 71 ms 16300 KB Partially correct
66 Partially correct 58 ms 15708 KB Partially correct
67 Partially correct 61 ms 13816 KB Partially correct
68 Partially correct 65 ms 14964 KB Partially correct
69 Partially correct 64 ms 16548 KB Partially correct
70 Partially correct 56 ms 15836 KB Partially correct
71 Partially correct 42 ms 13572 KB Partially correct
72 Partially correct 49 ms 14472 KB Partially correct
73 Partially correct 61 ms 16436 KB Partially correct
74 Partially correct 56 ms 15548 KB Partially correct
75 Partially correct 89 ms 16680 KB Partially correct
76 Partially correct 82 ms 16588 KB Partially correct
77 Partially correct 68 ms 16752 KB Partially correct
78 Partially correct 48 ms 13308 KB Partially correct
79 Partially correct 56 ms 14028 KB Partially correct
80 Partially correct 84 ms 16600 KB Partially correct
81 Partially correct 66 ms 16804 KB Partially correct
82 Partially correct 61 ms 15576 KB Partially correct