Nastya and Time Machine CodeForces - 1341F(dfs构造)

Having come to Nastya, Denis discovered that she was not happy to see him… But the young man has the last hope. He wants to buy all things that Nastya likes. Then she will certainly agree to talk to him.

The map of the city in which our heroes live has a lot of squares, some of which are connected by roads. From any square, you can get to any other in exactly the same way using these roads and not visiting the same square twice. It turns out that the graph of the city is a tree.

Denis is located at vertex 1 at the time 0. He wants to visit every vertex at least once and get back as soon as possible.

Denis can go along any road in 1 time. Unfortunately, the city is so large that it will not work out quickly. Therefore, Denis took a desperate step. He pulled out his pocket time machine, which he constructed in his basement. With its help, Denis can be standing still, change the time to any non-negative time, which is less than the current time.

But the time machine has one feature. If the hero finds himself in the same place and at the same time twice, there will be an explosion of universal proportions and it will not work to make Nastya happy. Therefore, Denis asks you to get him a route using a time machine such that he will get around all squares and will return to the first and at the same time the maximum time in which he visited will be minimal.

Formally, Denis’s route can be represented as a sequence of pairs: {v1,t1},{v2,t2},{v3,t3},…,{vk,tk}, where vi is number of square, and ti is time in which the boy is now.

The following conditions must be met:

The route starts on square 1 at time 0, i.e. v1=1,t1=0 and ends on the square 1, i.e. vk=1.
All transitions are divided into two types:
Being in the square change the time: {vi,ti}→{vi+1,ti+1}:vi+1=vi,0≤ti+1<ti.
Walk along one of the roads: {vi,ti}→{vi+1,ti+1}. Herewith, vi and vi+1 are connected by road, and ti+1=ti+1
All pairs {vi,ti} must be different.
Among v1,v2,…,vk all squares are found.
You need to find a route such that the maximum time Denis visits will be minimal, that is, the route for which max(t1,t2,…,tk) will be the minimum possible.

Input
The first line contains a single integer n (1≤n≤105) — the number of squares in the city.

The next n−1 lines contain two integers u and v (1≤v,u≤n,u≠v) - the numbers of the squares connected by the road.

It is guaranteed that the given graph is a tree.

Output
In the first line print the integer k (1≤k≤106) — the length of the path of Denis.

In the next k lines print pairs vi,ti — pairs that describe Denis’s route (as in the statement).

All route requirements described in the statements must be met.

It is guaranteed that under given restrictions there is at least one route and an answer whose length does not exceed 106. If there are several possible answers, print any.

Example
Input
5
1 2
2 3
2 4
4 5
Output
13
1 0
2 1
3 2
3 1
2 2
4 3
4 1
5 2
5 1
4 2
2 3
2 0
1 1
Sponsor

题意:
一颗树。时刻0从1出发。

  1. 可以选择传送回以前的节点。
  2. 进入一个相连的节点,时间加一。

要求时间最大值最小,并且 ( x , t ) (x,t) 不会重复

思路:
最大出度就是所需最大时间,设其为 m x mx 。之后要保证节点所得时间不大于 m x mx ,且最后能按照按照到达自己的时间回到父节点。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn = 1e5 + 7;
vector<int>G[maxn];
vector<pair<int,int> >ans;
int mx;

void dfs(int u,int fa,int t) {
    int ct = t - 1;
    ans.push_back({u,t}); //t时间到达这个点
    int num = mx - G[u].size();
    for(int i = 0;i < G[u].size();i++) {
        int v = G[u][i];
        if(v == fa) continue;
        if(t == mx) {
            t = num;
            ans.push_back({u,t}); //传送到以前的一个点,保证时间不重复
        }
        t++;
        dfs(v,u,t); //走到一个子节点
        ans.push_back({u,t}); //又回到这个点
    }
    if(u != 1 && t != ct) {
        ans.push_back({u,ct}); //要是这个点没有进入[num,mx]范围,那么传送回ct,保证回到父节点时候时间没有变。要是进入了[num,mx]范围,那么最后的时间肯定是ct。
    }
}

int main() {
    int n;scanf("%d",&n);
    for(int i = 1;i < n;i++) {
        int x,y;scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
        mx = max(mx,(int)max(G[x].size(),G[y].size()));
    }
    dfs(1,-1,0);
    printf("%d\n",ans.size());
    for(int i = 0;i < ans.size();i++) {
        printf("%d %d\n",ans[i].first,ans[i].second);
    }
    return 0;
}

原创文章 930 获赞 38 访问量 5万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/105778480