C. Road Improvement

题目连接:

http://www.codeforces.com/contest/638/problem/C

Description

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 ui, vi, meaning that the i-th road connects city ui and city vi (1 ≤ ui, vi ≤ n, ui ≠ 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.

Sample Input

4

1 2

3 4

3 2

Sample Output

2

2 2 1

1 3

Hint

题意

给你一棵树,n点n-1条边,现在这些边都不存在,你要修起来。

你可以选择一条边修理,如果这个边的两边的城市都没有在修理其他边的话。

每次修理需要一天的时间。

问你最少多少天可以修完,并且把方案输出。

题解:

直接dfs就好了,我们记录一个上一个边传过来的时间和这条边修理的时间就好了

保证不重复就行了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
vector<pair<int,int> >E[maxn];
vector<int>ans[maxn];
int tot = 0;
void dfs(int x,int fa,int te)
{
int now = 0;
for(int i=0;i<E[x].size();i++)
{
int v = E[x][i].first;
if(v==fa)continue;
now++;if(now==te)now++;
tot = max(tot,now);
ans[now].push_back(E[x][i].second);
dfs(v,x,now);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(make_pair(y,i));
E[y].push_back(make_pair(x,i));
}
dfs(1,-1,0);
cout<<tot<<endl;
for(int i=1;i<=tot;i++)
{
printf("%d ",ans[i].size());
for(int j=0;j<ans[i].size();j++)
printf("%d ",ans[i][j]);
printf("\n");
}
}

VK Cup 2016 - Qualification Round 2 C. Road Improvement dfs的更多相关文章

  1. VK Cup 2016 - Qualification Round 2 A. Home Numbers 水题

    A. Home Numbers 题目连接: http://www.codeforces.com/contest/638/problem/A Description The main street of ...

  2. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland

    今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...

  3. VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力

    D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...

  4. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题

    B. Making Genome in Berland 题目连接: http://www.codeforces.com/contest/638/problem/B Description Berlan ...

  5. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) D. Running with Obstacles 贪心

    D. Running with Obstacles 题目连接: http://www.codeforces.com/contest/637/problem/D Description A sports ...

  6. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) C. Promocodes with Mistakes 水题

    C. Promocodes with Mistakes 题目连接: http://www.codeforces.com/contest/637/problem/C Description During ...

  7. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) B. Chat Order 水题

    B. Chat Order 题目连接: http://www.codeforces.com/contest/637/problem/B Description Polycarp is a big lo ...

  8. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) A. Voting for Photos 水题

    A. Voting for Photos 题目连接: http://www.codeforces.com/contest/637/problem/A Description After celebra ...

  9. VK Cup 2016 - Qualification Round 1——A. Voting for Photos(queue+map)

    A. Voting for Photos time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. 日常开发技巧:x11-forward,使用远程机器的gui程序

    背景 日常用过ssh登录服务器进行工作,尽管大部分时间,都只需要终端操作,编辑源码也是vim就够用了. 但有时候,还是需要使用gui程序的,比如打开一份pdf,word,ppt,excel等. 碰到这 ...

  2. [转载]FFmpeg完美入门[2] - FFmpeg参数说明

     1 通用选项 -L license -h 帮助 -fromats 显示可用的格式,编解码的,协议的. -f fmt 强迫采用格式fmt -i filename 输入文件 -y 覆盖输出文件 -t d ...

  3. Python学习笔记——迭代器和生成器

    1.手动遍历迭代器 使用next函数,并捕获StopIteration异常. def manual_iter(): with open('./test.py') as f: try: while Tr ...

  4. ireport报表制作, 当一个字段显示的数据太多时(数据过长),则需要自动换行

    1.当一个字段显示的数据太长,一个表格放不下,则需要自动换行,选中要更改的表格(要显示动态内容的字段),设置属性Stretch with overflow 为钩选状态. 未勾选之前: 勾选之后: 2. ...

  5. (MHA+MYSQL-5.7增强半同步)高可用架构设计与实现

           架构使用mysql5.7版本基于GTD增强半同步并行复制配置 reploication 一主两从,使用MHA套件管理整个复制架构,实现故障自动切换高可用        优势:       ...

  6. HDU 2102 A计划(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目大意:公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输 ...

  7. LightOJ - 1010 Knights in Chessboard(规律)

    题目链接:https://vjudge.net/contest/28079#problem/B 题目大意:给你一个nxm的棋盘,问你最多可以放几个骑士让他们互相攻击不到.骑士攻击方式如下图: 解题思路 ...

  8. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8

  9. Linux 基础——常用的Linux网络命令

    一.学Linux网络命令有什么好处 网络的出现,我们的生活更方便了,处理事情的效率也越来越高,也可以看到全世界文化的差异.同时我们接受新事物的信息越来越来强,新事物的信息也越来越来多.网络对于我们尔等 ...

  10. javascript练习(二)

    案例 输出100个数字 案例  打印100以内 7的倍数 案例  打印100以内的奇数 案例  打印100以内所有偶数的和 打印图形 ********** ********** ********** ...