Submission #971271

# Submission time Handle Problem Language Result Execution time Memory
971271 2024-04-28T09:27:40 Z huutuan Meetings 2 (JOI21_meetings2) C++14
100 / 100
408 ms 53260 KB
#include<bits/stdc++.h>

using namespace std;

const int N=2e5+10, LG=18;
int n, sz[N], ans[N], dep[N], up[LG][N], tin[N], tout[N], tdfs, par[N];
vector<int> g[N], vv[N];
int mxdep[N];

void dfs(int u, int p){
   tin[u]=++tdfs;
   dep[u]=dep[p]+1;
   par[u]=p;
   for (int k=1; k<LG; ++k) up[k][u]=up[k-1][up[k-1][u]];
   for (int v:g[u]) if (v!=p){
      up[0][v]=u;
      dfs(v, u);
   }
   tout[u]=tdfs;
}

int lca(int u, int v){
   if (dep[u]!=dep[v]){
      if (dep[u]<dep[v]) swap(u, v);
      int d=dep[u]-dep[v];
      for (int k=0; k<LG; ++k) if (d>>k&1) u=up[k][u];
   }
   if (u==v) return u;
   for (int k=LG-1; k>=0; --k) if (up[k][u]!=up[k][v]) u=up[k][u], v=up[k][v];
   return up[0][u];
}

int distance(int u, int v){
   return dep[u]+dep[v]-dep[lca(u, v)]*2;
}

void dfs_sz(int u, int p){
   sz[u]=1;
   for (int v:g[u]) if (v!=p){
      dfs_sz(v, u);
      sz[u]+=sz[v];
   }
}

int find_centroid(int u, int p){
   for (int v:g[u]) if (v!=p && sz[v]*2>n) return find_centroid(v, u);
   return u;
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cin >> n;
   for (int i=1; i<n; ++i){
      int u, v; cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
   }
   for (int i=1; i<=n; i+=2) ans[i]=1;
   dfs_sz(1, 0);
   int cen=find_centroid(1, 0);
   dfs_sz(cen, 0);
   dfs(cen, 0);
   for (int i=1; i<=n; ++i) if (i!=cen) vv[sz[i]].push_back(i);
   set<int> st;
   pair<int, int> p={-1, -1};
   auto insert=[&](int u){
      st.insert(u);
      st.erase(par[u]);
      if ((int)st.size()==1) p={-1, -1};
      else if ((int)st.size()==2) p={*st.begin(), *st.rbegin()};
      else{
         if (p.first==par[u]) p.first=u;
         else if (p.second==par[u]) p.second=u;
         else if (distance(u, p.second)>distance(p.first, p.second)) p.first=u;
         else if (distance(u, p.first)>distance(p.first, p.second)) p.second=u;
      }
   };
   for (int i=n-(n&1); i>=2; i-=2){
      for (auto &j:vv[i/2]) insert(j);
      ans[i]=(p.first==-1)?1:distance(p.first, p.second)+1;
   }
   for (int i=1; i<=n; ++i) if (i!=cen) mxdep[sz[i]]=max(mxdep[sz[i]], dep[i]);
   for (int i=n; i>=2; --i) mxdep[i-1]=max(mxdep[i-1], mxdep[i]);
   for (int i=n-(n&1); i>=2; i-=2) ans[i]=max(ans[i], mxdep[i/2]);
   for (int i=1; i<=n; ++i) cout << ans[i] << '\n';
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 22872 KB Output is correct
2 Correct 4 ms 22876 KB Output is correct
3 Correct 6 ms 23080 KB Output is correct
4 Correct 4 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 4 ms 22876 KB Output is correct
7 Correct 6 ms 23064 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 4 ms 22876 KB Output is correct
12 Correct 4 ms 23056 KB Output is correct
13 Correct 4 ms 22872 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 4 ms 22876 KB Output is correct
16 Correct 4 ms 23064 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
18 Correct 4 ms 22872 KB Output is correct
19 Correct 4 ms 22876 KB Output is correct
20 Correct 4 ms 23012 KB Output is correct
21 Correct 4 ms 22876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 22872 KB Output is correct
2 Correct 4 ms 22876 KB Output is correct
3 Correct 6 ms 23080 KB Output is correct
4 Correct 4 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 4 ms 22876 KB Output is correct
7 Correct 6 ms 23064 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 4 ms 22876 KB Output is correct
12 Correct 4 ms 23056 KB Output is correct
13 Correct 4 ms 22872 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 4 ms 22876 KB Output is correct
16 Correct 4 ms 23064 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
18 Correct 4 ms 22872 KB Output is correct
19 Correct 4 ms 22876 KB Output is correct
20 Correct 4 ms 23012 KB Output is correct
21 Correct 4 ms 22876 KB Output is correct
22 Correct 7 ms 23388 KB Output is correct
23 Correct 7 ms 23388 KB Output is correct
24 Correct 7 ms 23388 KB Output is correct
25 Correct 7 ms 23388 KB Output is correct
26 Correct 8 ms 23368 KB Output is correct
27 Correct 8 ms 23364 KB Output is correct
28 Correct 7 ms 23384 KB Output is correct
29 Correct 7 ms 23388 KB Output is correct
30 Correct 7 ms 23380 KB Output is correct
31 Correct 8 ms 23388 KB Output is correct
32 Correct 7 ms 23388 KB Output is correct
33 Correct 7 ms 23644 KB Output is correct
34 Correct 7 ms 23388 KB Output is correct
35 Correct 6 ms 23500 KB Output is correct
36 Correct 8 ms 23388 KB Output is correct
37 Correct 7 ms 23536 KB Output is correct
38 Correct 6 ms 23388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 22872 KB Output is correct
2 Correct 4 ms 22876 KB Output is correct
3 Correct 6 ms 23080 KB Output is correct
4 Correct 4 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 4 ms 22876 KB Output is correct
7 Correct 6 ms 23064 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 4 ms 22876 KB Output is correct
12 Correct 4 ms 23056 KB Output is correct
13 Correct 4 ms 22872 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 4 ms 22876 KB Output is correct
16 Correct 4 ms 23064 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
18 Correct 4 ms 22872 KB Output is correct
19 Correct 4 ms 22876 KB Output is correct
20 Correct 4 ms 23012 KB Output is correct
21 Correct 4 ms 22876 KB Output is correct
22 Correct 7 ms 23388 KB Output is correct
23 Correct 7 ms 23388 KB Output is correct
24 Correct 7 ms 23388 KB Output is correct
25 Correct 7 ms 23388 KB Output is correct
26 Correct 8 ms 23368 KB Output is correct
27 Correct 8 ms 23364 KB Output is correct
28 Correct 7 ms 23384 KB Output is correct
29 Correct 7 ms 23388 KB Output is correct
30 Correct 7 ms 23380 KB Output is correct
31 Correct 8 ms 23388 KB Output is correct
32 Correct 7 ms 23388 KB Output is correct
33 Correct 7 ms 23644 KB Output is correct
34 Correct 7 ms 23388 KB Output is correct
35 Correct 6 ms 23500 KB Output is correct
36 Correct 8 ms 23388 KB Output is correct
37 Correct 7 ms 23536 KB Output is correct
38 Correct 6 ms 23388 KB Output is correct
39 Correct 285 ms 43872 KB Output is correct
40 Correct 268 ms 43404 KB Output is correct
41 Correct 312 ms 43980 KB Output is correct
42 Correct 272 ms 44388 KB Output is correct
43 Correct 297 ms 44416 KB Output is correct
44 Correct 282 ms 44272 KB Output is correct
45 Correct 408 ms 49848 KB Output is correct
46 Correct 214 ms 50772 KB Output is correct
47 Correct 235 ms 47084 KB Output is correct
48 Correct 165 ms 49276 KB Output is correct
49 Correct 278 ms 44996 KB Output is correct
50 Correct 215 ms 49480 KB Output is correct
51 Correct 194 ms 53260 KB Output is correct