#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define x first
#define y second
#define pb push_back
#define loop(i,s,e) for(int i=(s);i<(e);i++)
#define loopr(i,s,e) for(int i=(e)-1;i>=(s);i--)
#define chkmax(a,b) a = max(a,b)
#define chkmin(a,b) a = min(a,b)
#define all(x) x.begin(),x.end()
const int INF = 1e9, MOD = 1e9 + 7;
template <class A, class B> pair<A,B> operator+(pair<A,B>& a, pair<A,B>& b) {
return {a.x+b.x, a.y+b.y}; }
template <class A, class B> pair<A,B> operator-(pair<A,B>& a, pair<A,B>& b) {
return {a.x-b.x, a.y-b.y}; }
template <class A, class B> istream& operator>>(istream& is, pair<A,B>& a) {
return is >> a.x >> a.y; }
template <class A, class B> ostream& operator << (ostream& os, const pair<A,B>& a) {
return os << "< " << a.x << " , " << a.y << " >"; }
template<class T> ostream &operator<<(ostream &os, vector<T> v) { os << '['; if (!v.empty()) { os << v[0]; loop(i, 1, v.size()) os << ',' << v[i]; } return os << ']'; }
template<class T> ostream &operator<<(ostream &os, set<T> v) { os << '{'; if (!v.empty()) { os << *v.begin(); for(auto it = ++v.begin(); it != v.end(); ++it) os << ',' << *it; } return os << '}'; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto DIST = uniform_int_distribution<int>(0, INF);
int n;
vi sz;
vvi g;
vi ans;
vvi dp;
int dfssz(int cur, int p=-1){
for(int nei:g[cur]) if(nei!=p) sz[cur]+=dfssz(nei, cur);
return ++sz[cur];
}
int cent(int cur, int p=-1){
for(int nei:g[cur]) if(nei!=p && 2*sz[nei]>=n) return cent(nei, cur);
return cur;
}
void merge(int a, int b, int curd){
if (dp[a].size()<dp[b].size()) swap(dp[a], dp[b]);
loop(i,1,dp[b].size()){
chkmax(ans[2*i], dp[a][i] + dp[b][i] - 2 * curd+1);
chkmax(dp[a][i], dp[b][i]);
}
loop(i,1,dp[b].size()) dp[a].pb(curd);
}
void dfs(int cur, int p=-1, int d=0){
vii ch;
for(int nei:g[cur]) if(nei!=p){
dfs(nei, cur, d+1);
ch.pb({dp[nei].size(), nei});
}
sort(all(ch));
dp[cur] = {-1, d};
for(auto c:ch) merge(cur, c.y, d);
}
int32_t main(int argc, char *argv[]){
ios_base::sync_with_stdio(false);
cin >> n; g = vvi(n);
loop(i,1,n){
int a,b; cin>>a>>b; a--, b--;
g[a].pb(b), g[b].pb(a);
}
sz = vi(n);
dfssz(0);
int r = cent(0);
ans = vi(n+1,1), dp = vvi(n);
dfs(r);
loop(i,1,n+1) cout<<ans[i]<<endl;
return 0;
}
/*
color a
cls
g++ sol.cpp -o a & a
5
1 2
2 3
4 2
3 5
*/
Compilation message
meetings2.cpp: In function 'void merge(int, int, int)':
meetings2.cpp:13:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | #define loop(i,s,e) for(int i=(s);i<(e);i++)
| ^
meetings2.cpp:51:2: note: in expansion of macro 'loop'
51 | loop(i,1,dp[b].size()){
| ^~~~
meetings2.cpp:13:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | #define loop(i,s,e) for(int i=(s);i<(e);i++)
| ^
meetings2.cpp:55:2: note: in expansion of macro 'loop'
55 | loop(i,1,dp[b].size()) dp[a].pb(curd);
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
8 ms |
844 KB |
Output is correct |
23 |
Correct |
8 ms |
844 KB |
Output is correct |
24 |
Correct |
9 ms |
844 KB |
Output is correct |
25 |
Correct |
8 ms |
844 KB |
Output is correct |
26 |
Correct |
8 ms |
844 KB |
Output is correct |
27 |
Correct |
9 ms |
856 KB |
Output is correct |
28 |
Correct |
8 ms |
844 KB |
Output is correct |
29 |
Correct |
8 ms |
844 KB |
Output is correct |
30 |
Correct |
8 ms |
844 KB |
Output is correct |
31 |
Correct |
8 ms |
844 KB |
Output is correct |
32 |
Correct |
9 ms |
928 KB |
Output is correct |
33 |
Correct |
8 ms |
1100 KB |
Output is correct |
34 |
Correct |
11 ms |
792 KB |
Output is correct |
35 |
Correct |
7 ms |
844 KB |
Output is correct |
36 |
Correct |
9 ms |
844 KB |
Output is correct |
37 |
Correct |
8 ms |
836 KB |
Output is correct |
38 |
Correct |
9 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
8 ms |
844 KB |
Output is correct |
23 |
Correct |
8 ms |
844 KB |
Output is correct |
24 |
Correct |
9 ms |
844 KB |
Output is correct |
25 |
Correct |
8 ms |
844 KB |
Output is correct |
26 |
Correct |
8 ms |
844 KB |
Output is correct |
27 |
Correct |
9 ms |
856 KB |
Output is correct |
28 |
Correct |
8 ms |
844 KB |
Output is correct |
29 |
Correct |
8 ms |
844 KB |
Output is correct |
30 |
Correct |
8 ms |
844 KB |
Output is correct |
31 |
Correct |
8 ms |
844 KB |
Output is correct |
32 |
Correct |
9 ms |
928 KB |
Output is correct |
33 |
Correct |
8 ms |
1100 KB |
Output is correct |
34 |
Correct |
11 ms |
792 KB |
Output is correct |
35 |
Correct |
7 ms |
844 KB |
Output is correct |
36 |
Correct |
9 ms |
844 KB |
Output is correct |
37 |
Correct |
8 ms |
836 KB |
Output is correct |
38 |
Correct |
9 ms |
972 KB |
Output is correct |
39 |
Correct |
480 ms |
32576 KB |
Output is correct |
40 |
Correct |
518 ms |
32464 KB |
Output is correct |
41 |
Correct |
530 ms |
33276 KB |
Output is correct |
42 |
Correct |
500 ms |
33400 KB |
Output is correct |
43 |
Correct |
552 ms |
33668 KB |
Output is correct |
44 |
Correct |
529 ms |
33572 KB |
Output is correct |
45 |
Correct |
517 ms |
35640 KB |
Output is correct |
46 |
Correct |
445 ms |
42516 KB |
Output is correct |
47 |
Correct |
439 ms |
29504 KB |
Output is correct |
48 |
Correct |
451 ms |
30388 KB |
Output is correct |
49 |
Correct |
434 ms |
30188 KB |
Output is correct |
50 |
Correct |
476 ms |
30392 KB |
Output is correct |
51 |
Correct |
443 ms |
36920 KB |
Output is correct |