【CodeForces - 638】C 【思维+DFS】

59 篇文章 0 订阅
25 篇文章 1 订阅

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.

Example
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.
题意
给一棵树,定义如果想修理一条边,必须要边的两个点出来维修一天(中途不能够转到别的边上修),问 现在想把所有的边都修理了,问如何安排修理才可以让时间最短。

分析 对于每个节点来说,和其相连的边肯定不能够一起修理,我们可以用dfs来从上往下开始安排是哪一天修理,只要满足 与每个节点相连的边都不在一个时间修就可以了。
代码

#include<bits/stdc++.h>
using namespace std;
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 200000+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

typedef pair<int,int>P;
vector<P>mp[MAXN];
vector<int>ans[MAXN];int size=0;
void dfs(int now,int pre,int fa){
    int id=0; // 对于每个节点都从1开始安排修理时间
    for(int i=0;i<mp[now].size();i++){
        int v=mp[now][i].first;
        if(v==pre) continue;
        if(id+1==fa) ++id; //如果当前节点的儿子边的修理时间和其父亲边的修理时间一样,多+1跳过就行
        ans[++id].push_back(mp[now][i].second); // 保存结果
        dfs(v,now,id);
    }
    size=max(size,id);
}
int main(){
    CLOSE();
//  fread();
//  fwrite();
    int n;scanf("%d",&n);
    for(int i=1;i<=n-1;i++) {
        int a,b;scanf("%d%d",&a,&b);
        P t;t.first=b;t.second=i;
        mp[a].push_back(t);
        t.first=a;  t.second=i;
        mp[b].push_back(t);
    }
    dfs(1,-1,-1);
    printf("%d\n",size);
    for(int i=1;i<=size;i++){
        int s=ans[i].size();
        printf("%d",s);
        for(int j=0;j<s;j++)
            printf(" %d",ans[i][j]);
         puts(""); 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值