考虑删除每一个点之后,对所有相连的支干大小进行背包
#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define debug(x) cout<<#x<<" is "<<x<<endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define DBG 0
#define lc(x) (x<<1)
#define rc(x) ((x << 1) | 1)
const int N = 5000 + 5;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LLINF = (1LL<<60);
using namespace std;
const int mod = 998244353;
ll fast_pow(ll a,ll b){ll ans = 1;while(b){if(b&1)ans = (ans * a)%mod;a = (a * a)%mod;b>>=1;}return (ans%mod);}
inline ll read(){ll X=0; bool flag=1; char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}if(flag) return X;return ~(X-1);}
inline void write(ll X){if(X<0) {X=~(X-1); putchar('-');}if(X>9) write(X/10);putchar(X%10+'0');}
typedef pair<int,int> pii;
typedef vector<int> vi;
inline void fastIO(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
inline void time_cost(clock_t start_time,clock_t end_time){cout << "The run time is: " <<(double)(end_time - start_time) / CLOCKS_PER_SEC << "s" << endl;}
inline void init(){
}
vector<int> g[N];
int sz[N];
int dp[N][N];
int flag[N];
int n;
void dfs(int u,int pre){
sz[u] = 1;
vector<int> tmp;
dp[u][0] = 1;
for(auto v : g[u])if(v != pre){
dfs(v,u);
tmp.pb(sz[v]);
sz[u] += sz[v];
}
if(n - sz[u] > 0)tmp.pb(n -sz[u]);
for(int i = 0;i < tmp.size();i++){
int tt = tmp[i];
for(int j = n - 2 - tt;j >= 0;j--){
dp[u][j + tt] |= dp[u][j];
}
}
for(int i = 1;i < n - 1;i++)flag[i] |= dp[u][i];
}
inline void solve(){
init();
cin >> n;
for(int i = 1;i <= n - 1;i++){
int u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1,-1);
int ans = 0;
for(int i = 1;i < n - 1;i++){
if(flag[i])
ans++;
}
cout << ans << endl;
for(int i = 1;i < n - 1;i++){
if(flag[i])
cout << i <<" " << n - i - 1 << endl;
}
}
int main(){
fastIO();
#if DBG
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
solve();
return 0;
}