답안 #525595

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525595 2022-02-12T05:29:58 Z inwbear Village (BOI20_village) C++14
50 / 100
173 ms 37060 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>mat,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;
    }
    if(nonmat.size()>0){
        ans1+=nonmat.size()-1;
        if(nonmat.size()%2==0){
            for(int i=0;i<nonmat.size();i+=2){
                a1[nonmat[i]]=nonmat[i+1];
                a1[nonmat[i+1]]=nonmat[i];
            }
            return 1;
        }
        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]);
    for(int i=1;i<=n;i++)if(a1[i]==0)while(1);
    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:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |             for(int i=0;i<nonmat.size();i+=2){
      |                         ~^~~~~~~~~~~~~~
Village.cpp:47:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |             for(int i=1;i<nonmat.size();i+=2){
      |                         ~^~~~~~~~~~~~~~
Village.cpp: In function 'void dfs1(int, int)':
Village.cpp:60:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for(int i=0;i<v[N].size();i++){
      |                 ~^~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Village.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         scanf("%d %d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Partially correct 1 ms 2636 KB Partially correct
5 Partially correct 2 ms 2636 KB Partially correct
6 Partially correct 2 ms 2636 KB Partially correct
7 Correct 2 ms 2636 KB Output is correct
8 Partially correct 2 ms 2636 KB Partially correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Partially correct 1 ms 2636 KB Partially correct
14 Partially correct 1 ms 2636 KB Partially correct
15 Partially correct 2 ms 2636 KB Partially correct
16 Partially correct 2 ms 2636 KB Partially correct
17 Partially correct 1 ms 2636 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 2636 KB Partially correct
2 Partially correct 2 ms 2764 KB Partially correct
3 Partially correct 2 ms 2764 KB Partially correct
4 Partially correct 2 ms 2764 KB Partially correct
5 Partially correct 2 ms 2764 KB Partially correct
6 Partially correct 3 ms 2764 KB Partially correct
7 Correct 2 ms 2892 KB Output is correct
8 Partially correct 2 ms 2892 KB Partially correct
9 Partially correct 2 ms 2764 KB Partially correct
10 Partially correct 2 ms 2892 KB Partially correct
11 Partially correct 2 ms 2892 KB Partially correct
12 Partially correct 2 ms 2764 KB Partially correct
13 Correct 2 ms 2764 KB Output is correct
14 Partially correct 2 ms 2892 KB Partially correct
15 Partially correct 2 ms 2764 KB Partially correct
16 Partially correct 2 ms 2764 KB Partially correct
17 Partially correct 3 ms 2764 KB Partially correct
18 Partially correct 2 ms 2892 KB Partially correct
19 Partially correct 2 ms 2892 KB Partially correct
20 Partially correct 2 ms 2892 KB Partially correct
21 Partially correct 4 ms 2772 KB Partially correct
22 Partially correct 3 ms 2892 KB Partially correct
23 Partially correct 2 ms 2848 KB Partially correct
24 Partially correct 2 ms 2764 KB Partially correct
25 Partially correct 2 ms 2764 KB Partially correct
26 Partially correct 2 ms 2764 KB Partially correct
27 Partially correct 2 ms 2764 KB Partially correct
28 Partially correct 2 ms 2892 KB Partially correct
29 Partially correct 2 ms 2892 KB Partially correct
30 Partially correct 3 ms 2892 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Partially correct 1 ms 2636 KB Partially correct
5 Partially correct 2 ms 2636 KB Partially correct
6 Partially correct 2 ms 2636 KB Partially correct
7 Correct 2 ms 2636 KB Output is correct
8 Partially correct 2 ms 2636 KB Partially correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Partially correct 1 ms 2636 KB Partially correct
14 Partially correct 1 ms 2636 KB Partially correct
15 Partially correct 2 ms 2636 KB Partially correct
16 Partially correct 2 ms 2636 KB Partially correct
17 Partially correct 1 ms 2636 KB Partially correct
18 Partially correct 2 ms 2636 KB Partially correct
19 Partially correct 2 ms 2764 KB Partially correct
20 Partially correct 2 ms 2764 KB Partially correct
21 Partially correct 2 ms 2764 KB Partially correct
22 Partially correct 2 ms 2764 KB Partially correct
23 Partially correct 3 ms 2764 KB Partially correct
24 Correct 2 ms 2892 KB Output is correct
25 Partially correct 2 ms 2892 KB Partially correct
26 Partially correct 2 ms 2764 KB Partially correct
27 Partially correct 2 ms 2892 KB Partially correct
28 Partially correct 2 ms 2892 KB Partially correct
29 Partially correct 2 ms 2764 KB Partially correct
30 Correct 2 ms 2764 KB Output is correct
31 Partially correct 2 ms 2892 KB Partially correct
32 Partially correct 2 ms 2764 KB Partially correct
33 Partially correct 2 ms 2764 KB Partially correct
34 Partially correct 3 ms 2764 KB Partially correct
35 Partially correct 2 ms 2892 KB Partially correct
36 Partially correct 2 ms 2892 KB Partially correct
37 Partially correct 2 ms 2892 KB Partially correct
38 Partially correct 4 ms 2772 KB Partially correct
39 Partially correct 3 ms 2892 KB Partially correct
40 Partially correct 2 ms 2848 KB Partially correct
41 Partially correct 2 ms 2764 KB Partially correct
42 Partially correct 2 ms 2764 KB Partially correct
43 Partially correct 2 ms 2764 KB Partially correct
44 Partially correct 2 ms 2764 KB Partially correct
45 Partially correct 2 ms 2892 KB Partially correct
46 Partially correct 2 ms 2892 KB Partially correct
47 Partially correct 3 ms 2892 KB Partially correct
48 Partially correct 106 ms 21784 KB Partially correct
49 Partially correct 129 ms 23608 KB Partially correct
50 Partially correct 128 ms 23624 KB Partially correct
51 Partially correct 102 ms 19140 KB Partially correct
52 Partially correct 128 ms 23364 KB Partially correct
53 Partially correct 110 ms 21328 KB Partially correct
54 Correct 57 ms 19472 KB Output is correct
55 Correct 150 ms 37060 KB Output is correct
56 Partially correct 173 ms 30044 KB Partially correct
57 Partially correct 162 ms 27708 KB Partially correct
58 Partially correct 145 ms 25872 KB Partially correct
59 Partially correct 153 ms 23704 KB Partially correct
60 Partially correct 90 ms 24272 KB Partially correct
61 Partially correct 93 ms 23956 KB Partially correct
62 Partially correct 105 ms 23968 KB Partially correct
63 Partially correct 101 ms 22732 KB Partially correct
64 Partially correct 123 ms 23908 KB Partially correct
65 Partially correct 106 ms 23984 KB Partially correct
66 Partially correct 92 ms 22716 KB Partially correct
67 Partially correct 70 ms 18420 KB Partially correct
68 Partially correct 88 ms 20800 KB Partially correct
69 Partially correct 92 ms 24080 KB Partially correct
70 Partially correct 118 ms 22760 KB Partially correct
71 Partially correct 61 ms 17616 KB Partially correct
72 Partially correct 79 ms 19516 KB Partially correct
73 Partially correct 100 ms 24028 KB Partially correct
74 Partially correct 89 ms 22252 KB Partially correct
75 Partially correct 126 ms 23456 KB Partially correct
76 Partially correct 114 ms 23456 KB Partially correct
77 Partially correct 98 ms 22816 KB Partially correct
78 Partially correct 66 ms 16668 KB Partially correct
79 Partially correct 85 ms 19056 KB Partially correct
80 Partially correct 120 ms 23480 KB Partially correct
81 Partially correct 109 ms 23000 KB Partially correct
82 Partially correct 115 ms 23668 KB Partially correct