제출 #1312912

#제출 시각아이디문제언어결과실행 시간메모리
1312912herominhsteveVillage (BOI20_village)C++20
100 / 100
57 ms16704 KiB
#include <bits/stdc++.h> #define el '\n' #define FNAME "village" #define allof(x) x.begin(),x.end() #define allof1(x) x.begin()+1,x.end() #define mset(x,n) memset(x,(n),sizeof(x)) using namespace std; const long long MOD = (long long) 1e9 + 7; template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;} template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;} void setup(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); if (fopen(FNAME".inp","r")){ freopen(FNAME".inp","r",stdin); freopen(FNAME".out","w",stdout); } } const int MAXN = 1e5 + 5; const long long INF = (long long) 1e16 + 15092007; int n; vector<int> graph[MAXN]; void init(){ cin>>n; for (int i=1;i<n;i++){ int u,v; cin>>u>>v; graph[u].push_back(v); graph[v].push_back(u); } } namespace calMin{ long long res = 0; vector<int> des; void dfs(int u=1,int pre=-1){ for (int v : graph[u]){ if (v==pre) continue; dfs(v,u); if (des[v] == v){ swap(des[u],des[v]); res += 2; } } if (pre==-1 and des[u]==u and !graph[u].empty()){ swap(des[u],des[graph[u].front()]); res += 2; } } void getAns(){ des.assign(n+1,0); iota(allof(des),0); dfs(); } }; namespace calMax{ int sz[MAXN]; void dfsSZ(int u=1,int pre=-1){ sz[u] = 1; for (int v : graph[u]){ if (v==pre) continue; dfsSZ(v,u); sz[u] += sz[v]; } } int dfsCentroid(int u=1,int pre=-1){ for (int v : graph[u]){ if (v==pre) continue; if (sz[v] * 2 > n) return dfsCentroid(v,u); } return u; } int centroid; long long res = 0; vector<int> des; int timedfs; int in[MAXN]; vector<int> euler; void dfsEuler(int u,int pre){ euler.push_back(u); in[u] = timedfs++; for (int v : graph[u]){ if (v==pre) continue; dfsEuler(v,u); } } void getAns(){ dfsSZ(); centroid = dfsCentroid(); for (int u=2;u<=n;u++) res += 2ll * min(sz[u],n - sz[u]); dfsEuler(centroid,-1); des.assign(n+1,0); for (int u=1;u<=n;u++) des[u] = euler[(in[u] + n/2) % n]; } }; void sol(){ calMin::getAns(); calMax::getAns(); cout<<calMin::res<<" "<<calMax::res<<el; for (int i=1;i<=n;i++) cout<<calMin::des[i]<<" "; cout<<el; for (int i=1;i<=n;i++) cout<<calMax::des[i]<<" "; } int main(){ setup(); init(); sol(); }

컴파일 시 표준 에러 (stderr) 메시지

Village.cpp: In function 'void setup()':
Village.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |                 freopen(FNAME".inp","r",stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Village.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |                 freopen(FNAME".out","w",stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...