Submission #415983

#TimeUsernameProblemLanguageResultExecution timeMemory
415983nicolaalexandraVillage (BOI20_village)C++14
12 / 100
11 ms10880 KiB
#include <bits/stdc++.h> #define DIM 100010 #define INF 2000000000 using namespace std; vector <int> L[DIM],w[DIM]; int v[DIM],fth[DIM],sp[DIM],f[DIM],viz[DIM],d[DIM]; int n,i,x,y,sol_min,sol_max,k,idx,poz; void dfs (int nod, int tata){ fth[nod] = tata; sp[nod] = 1; for (auto vecin : L[nod]) if (vecin != tata){ dfs (vecin,nod); sp[nod] += sp[vecin]; } if (nod != 1 && v[nod] == nod){ sol_min += 2; swap (v[nod],v[fth[nod]]); } sol_max += 2 * min (sp[nod],n-sp[nod]); } int get_centroid (int nod, int tata){ int maxim = 0, heavy_son = 0; for (auto vecin : L[nod]){ if (vecin == tata) continue; if (sp[vecin] > maxim) maxim = sp[vecin], heavy_son = vecin; } if (maxim <= n/2 && n - sp[nod] <= n/2) return nod; else return get_centroid(heavy_son,nod); } void dfs2 (int nod, int idx){ w[idx].push_back(nod); viz[nod] = 1; f[nod] = idx; for (auto vecin : L[nod]) if (!viz[vecin]) dfs2 (vecin,idx); } inline int cmp (int a, int b){ return w[a].size() >= w[b].size(); } int main (){ //ifstream cin ("date.in"); //ofstream cout ("date.out"); cin>>n; for (i=1;i<n;i++){ cin>>x>>y; L[x].push_back(y); L[y].push_back(x); } for (i=1;i<=n;i++) v[i] = i; dfs (1,0); for (i=1;i<=n;i++) if (v[i] == i){ sol_min += 2; if (i != 1) swap (v[i],v[fth[i]]); else swap (v[i],v[L[1][0]]); } cout<<sol_min<<" "<<sol_max<<"\n"; for (i=1;i<=n;i++) cout<<v[i]<<" "; cout<<"\n"; memset (v,0,sizeof v); int centroid = get_centroid(1,0); viz[centroid] = 1; w[centroid].push_back(centroid); d[++k] = centroid; int mini = n, poz; for (auto vecin : L[centroid]){ dfs2 (vecin,vecin); d[++k] = vecin; //cout<<w[vecin].size()<<"\n"; } sort (d+1,d+k+1,cmp); for (int poz=2;poz<=k;poz++){ int i = w[d[poz]].size()-1; int j = w[d[poz-1]].size()-1; while (i >= 0){ v[w[d[poz]][i]] = w[d[poz-1]][j]; i--, j --; } } idx = 2, poz = w[d[2]].size() - 1; for (i=1;i<=k;i++){ for (int j=0;j<w[d[i]].size();j++){ v[w[d[i]][j]] = w[d[idx]][poz]; poz--; if (poz < 0){ idx++; if (idx > k) idx = 1; poz = w[d[idx]].size()-1; } } } /*for (i=0;i<w[d[1]].size();i++){ int x = w[d[1]][i]; while (f[idx] == d[1] || ) idx++; v[x] = idx++; } */ for (i=1;i<=n;i++) cout<<v[i]<<" "; /*for (i=1;i<=n;i++) if (v[i] == 0) cout<<"a";*/ return 0; }

Compilation message (stderr)

Village.cpp: In function 'int main()':
Village.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |         for (int j=0;j<w[d[i]].size();j++){
      |                      ~^~~~~~~~~~~~~~~
Village.cpp:99:9: warning: unused variable 'mini' [-Wunused-variable]
   99 |     int mini = n, poz;
      |         ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...