codeforces638C. Road Improvement【dfs】

C. Road Improvement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.

In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.

Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.

Input

The first line of the input contains a positive integer n (2 ≤ n ≤ 200 000) — the number of cities in Berland.

Each of the next n - 1 lines contains two numbers uivi, meaning that the i-th road connects city ui and city vi (1 ≤ ui, vi ≤ nui ≠ vi).

Output

First print number k — the minimum number of days needed to repair all the roads in Berland.

In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di — the number of roads that should be repaired on the i-th day, and then di space-separated integers — the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.

If there are multiple variants, you can print any of them.

Examples
input
4
1 2
3 4
3 2
output
2
2 2 1
1 3
input
6
3 4
5 4
3 2
1 3
4 6
output
3
1 1 
2 2 3 
2 4 5 
Note

In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 — on the second day.


题意:N个城市给出城市间的连通关系,在每个城市有一个修路队修复一条路需要这条路相连的两个城市的修路队同时工作一天,问最少需要多少天能修复所有的路

一个城市修路时与其相连的其他城市都不能再与该城市在同一天修路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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值