C. Road Improvement

 题目大意:

给定一个联通的树,n个点,n-1条边,但是一开始所有的路都是坏掉的,需要把所有的路全部修好,修一条路的时候需要这条路的两个端点u,v同时开工,一个点一天只能修一条路,修一条路需要一天时间。

问最少需要多少天才能把所有的路修好,同时输出每一个天修的路的编号(按照输入顺序)。

容易得出最少需要的天数等于最大的点的度数(假设为k),因为一个点一天只能开工一条路。

然后找一个点开始DFS,假设一条边被修好的那天为d,那么1<=d<=k,搜索的时候只要保证接下来的边的d不和之前的搜索过来的边的d一样就可以。

// #pragma GCC optimize(2)
// #include <random>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
#define lson(x) (x << 1)
#define rson(x) (x << 1 | 1)
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e5 + 10;
const int maxm = 2e4 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
// int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Edge
{
    int next, to, id, col;
} e[maxn << 2];
int n, m, k = 0;
int head[maxn];
int du[maxn];
int tot;
vector<int> ans[maxn];
void add(int u, int v, int c)
{
    e[k].next = head[u];
    e[k].to = v;
    e[k].id = c;
    head[u] = k++;
}
void DFS(int u, int fa, int col)
{
    int x = 1;
    for (int i = head[u]; ~i; i = e[i].next)
    {
        int v = e[i].to;
        if (v == fa)
            continue;
        if (x == col)
            x++;
        e[i].col = x;
        ans[x].push_back(e[i].id);
        DFS(v, u, x);
        x++;
    }
    return;
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
    //  freopen("out.txt", "w", stdout);
#endif
    IO;
    int x, y;
    cin >> n;
    int c = 1;
    memset(head, -1, sizeof head);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> x >> y;
        add(x, y, c);
        add(y, x, c);
        ++c;
        du[x]++;
        du[y]++;
        tot = max(tot, du[x]);
        tot = max(tot, du[y]);
    }
    cout << tot << "\n";
    DFS(1, 0, 0);
    for (int i = 1; i <= tot; i++)
    {
        cout << ans[i].size() << " ";
        for (int j = 0; j < ans[i].size(); j++)
            cout << ans[i][j] << " ";
        cout << "\n";
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值