一个城市修路时与其相连的其他城市都不能再与该城市在同一天修路dfs即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=200010;
vector<int>ans[maxn];
vector<pair<int,int> >G[maxn];
int num=0;
void dfs(int now,int pre,int cnt){
int day=0;
for(int i=0;i<G[now].size();++i){
if(G[now][i].first==pre)continue;
day++;if(day==cnt)day++;
ans[day].push_back(G[now][i].second);
num=max(day,num);
dfs(G[now][i].first,now,day);
}
}
int main()
{
int n,i,j,k,a,b;
scanf("%d",&n);
for(i=1;i<n;++i){
scanf("%d%d",&a,&b);
G[a].push_back(make_pair(b,i));
G[b].push_back(make_pair(a,i));
}
dfs(1,0,0);
printf("%d\n",num);
for(i=1;i<=num;++i){
printf("%d",ans[i].size());
for(j=0;j<ans[i].size();++j){
printf(" %d",ans[i][j]);
}
printf("\n");
}
return 0;
}