Submission #525597

# Submission time Handle Problem Language Result Execution time Memory
525597 2022-02-12T05:44:36 Z inwbear Village (BOI20_village) C++14
100 / 100
175 ms 34104 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define MEM(x,a) memset((x),a,sizeof((x)))
#define F first
#define S second
#define imx INT_MAX
const long long MOD = (long long)(1e9+7);
const long long MMX = (long long)(1e18);
typedef long long LL;
typedef pair<int,int> pii;
int n,a,b,h[100005],par[100005][18],wi[100005][18],ans1,Nnum[100005],rr,k,hf,dp[100005],cc,a1[100005],a2[100005];
LL ans2,ans3;
vector<int>v[100005],o;
int dfs(int N,int pr){
    bool p=true;
    vector<int>nonmat;
    dp[N]=1;
    Nnum[rr]=N;
    rr++;
    h[N]=h[pr]+1;
    wi[N][0]=1;
    par[N][0]=pr;
    for(int i=0;i<v[N].size();i++){
        if(v[N][i]!=pr){
            if(dfs(v[N][i],N)==1)nonmat.pb(v[N][i]);
            dp[N]+=dp[v[N][i]];
            if(dp[v[N][i]]>hf)p=false;
        }
    }
    if(p){
        if(n-dp[N]<=hf)cc=N;
    }
    //printf("%d %d\n",N,nonmat.size());
    if(nonmat.size()>0){
        ans1+=nonmat.size()-1;
        if(nonmat.size()%2==0){
            a1[nonmat[0]]=N;
            a1[nonmat[1]]=nonmat[0];
            a1[N]=nonmat[1];
            for(int i=2;i<nonmat.size();i+=2){
                a1[nonmat[i]]=nonmat[i+1];
                a1[nonmat[i+1]]=nonmat[i];
            }
            return 0;
        }
        else{
            a1[nonmat[0]]=N;
            a1[N]=nonmat[0];
            for(int i=1;i<nonmat.size();i+=2){
                a1[nonmat[i]]=nonmat[i+1];
                a1[nonmat[i+1]]=nonmat[i];
            }
            return 0;
        }
    }
    else{
        return 1;
    }
}
void dfs1(int N,int pr){
    o.pb(N);
    for(int i=0;i<v[N].size();i++){
        if(v[N][i]!=pr){
            dfs1(v[N][i],N);
        }
    }
    return;
}
void gen_lca(){
    for(int i=1;i<18;i++){
        for(int j=1;j<=n;j++){
            if(par[j][i-1]!=-1){
                wi[j][i]=wi[j][i-1]+wi[par[j][i-1]][i-1];
                par[j][i]=par[par[j][i-1]][i-1];
            }
        }
    }
    return;
}
int lca(int A,int B){
    if(h[A]<h[B])swap(A,B);
    int diff=h[A]-h[B],re=0;
    for(int i=0;i<18;i++){
        if((diff>>i)&1){
            re+=wi[A][i];
            A=par[A][i];
        }
    }
    if(A==B)return re;
    for(int i=17;i>=0;i--){
        if(par[A][i]!=par[B][i]){
            re+=wi[A][i];
            re+=wi[B][i];
            A=par[A][i];
            B=par[B][i];
        }
    }
    re+=wi[A][0]+wi[B][0];
    return re;
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d",&n);
    hf=n/2;
    ans1=n;
    for(int i=1;i<n;i++){
        scanf("%d %d",&a,&b);
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i=0;i<18;i++){
        for(int j=1;j<=n;j++){
            par[j][i]=-1;
        }
    }
    k=dfs(1,0);
    ans1+=k;
    if(k==1){
        a1[1]=a1[v[1][0]];
        a1[v[1][0]]=1;
    }
    gen_lca();
    for(int i=1;i<=n;i++){
        ans2+=2*lca(i,cc);
    }
    dfs1(cc,0);
    for(int i=0;i<n;i++){
        //printf("%d ",o[i]);
        a2[o[i]]=o[(i+hf)%n];
    }
    printf("%d %lld\n",ans1,ans2);
    for(int i=1;i<=n;i++)printf("%d ",a1[i]);
    printf("\n");
    for(int i=1;i<=n;i++)printf("%d ",a2[i]);
    printf("\n");
}

Compilation message

Village.cpp: In function 'int dfs(int, int)':
Village.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for(int i=0;i<v[N].size();i++){
      |                 ~^~~~~~~~~~~~
Village.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |             for(int i=2;i<nonmat.size();i+=2){
      |                         ~^~~~~~~~~~~~~~
Village.cpp:51:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |             for(int i=1;i<nonmat.size();i+=2){
      |                         ~^~~~~~~~~~~~~~
Village.cpp: In function 'void dfs1(int, int)':
Village.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for(int i=0;i<v[N].size();i++){
      |                 ~^~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Village.cpp:110:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         scanf("%d %d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 3 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2648 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2764 KB Output is correct
3 Correct 2 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 2 ms 2892 KB Output is correct
6 Correct 2 ms 2764 KB Output is correct
7 Correct 2 ms 2892 KB Output is correct
8 Correct 2 ms 2892 KB Output is correct
9 Correct 2 ms 2764 KB Output is correct
10 Correct 2 ms 2892 KB Output is correct
11 Correct 2 ms 2892 KB Output is correct
12 Correct 2 ms 2892 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2892 KB Output is correct
15 Correct 2 ms 2764 KB Output is correct
16 Correct 2 ms 2764 KB Output is correct
17 Correct 2 ms 2892 KB Output is correct
18 Correct 2 ms 2892 KB Output is correct
19 Correct 2 ms 2892 KB Output is correct
20 Correct 2 ms 2892 KB Output is correct
21 Correct 3 ms 2892 KB Output is correct
22 Correct 2 ms 2892 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2764 KB Output is correct
25 Correct 2 ms 2764 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 2 ms 2892 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 2 ms 2892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 3 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2648 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 2 ms 2892 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2892 KB Output is correct
25 Correct 2 ms 2892 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2892 KB Output is correct
28 Correct 2 ms 2892 KB Output is correct
29 Correct 2 ms 2892 KB Output is correct
30 Correct 2 ms 2764 KB Output is correct
31 Correct 2 ms 2892 KB Output is correct
32 Correct 2 ms 2764 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2892 KB Output is correct
35 Correct 2 ms 2892 KB Output is correct
36 Correct 2 ms 2892 KB Output is correct
37 Correct 2 ms 2892 KB Output is correct
38 Correct 3 ms 2892 KB Output is correct
39 Correct 2 ms 2892 KB Output is correct
40 Correct 2 ms 2764 KB Output is correct
41 Correct 2 ms 2764 KB Output is correct
42 Correct 2 ms 2764 KB Output is correct
43 Correct 2 ms 2764 KB Output is correct
44 Correct 2 ms 2764 KB Output is correct
45 Correct 2 ms 2892 KB Output is correct
46 Correct 2 ms 2764 KB Output is correct
47 Correct 2 ms 2892 KB Output is correct
48 Correct 110 ms 21692 KB Output is correct
49 Correct 118 ms 23624 KB Output is correct
50 Correct 169 ms 23560 KB Output is correct
51 Correct 86 ms 19036 KB Output is correct
52 Correct 112 ms 23432 KB Output is correct
53 Correct 102 ms 21308 KB Output is correct
54 Correct 64 ms 17992 KB Output is correct
55 Correct 164 ms 34104 KB Output is correct
56 Correct 175 ms 28604 KB Output is correct
57 Correct 135 ms 26644 KB Output is correct
58 Correct 157 ms 25364 KB Output is correct
59 Correct 125 ms 23684 KB Output is correct
60 Correct 121 ms 24168 KB Output is correct
61 Correct 90 ms 23904 KB Output is correct
62 Correct 91 ms 24008 KB Output is correct
63 Correct 113 ms 22844 KB Output is correct
64 Correct 103 ms 23844 KB Output is correct
65 Correct 112 ms 24016 KB Output is correct
66 Correct 84 ms 22748 KB Output is correct
67 Correct 59 ms 18420 KB Output is correct
68 Correct 87 ms 20832 KB Output is correct
69 Correct 136 ms 24072 KB Output is correct
70 Correct 83 ms 22800 KB Output is correct
71 Correct 77 ms 17536 KB Output is correct
72 Correct 67 ms 19612 KB Output is correct
73 Correct 90 ms 24100 KB Output is correct
74 Correct 102 ms 22216 KB Output is correct
75 Correct 113 ms 23492 KB Output is correct
76 Correct 113 ms 23456 KB Output is correct
77 Correct 101 ms 22844 KB Output is correct
78 Correct 65 ms 16700 KB Output is correct
79 Correct 105 ms 19100 KB Output is correct
80 Correct 114 ms 23500 KB Output is correct
81 Correct 95 ms 22940 KB Output is correct
82 Correct 98 ms 23740 KB Output is correct