不难发现为了逐步确定每个点于其相邻点的相交情况,那么我们只可能有两种逐步构造的方式:从根开始往下构造,以及从子树往根上构造。经过很久的尝试,我发现从根往下构造是一件很困难的事情,于是我们可以反过来考虑第二种构造方式。

既然是从子树开始构造,那么我们先想一下最底层是如何构造的。不难发现最底层实质上是一个菊花图的形式,因为父亲和这些儿子必然有交,那么这些儿子之间一定也会有交,为了让儿子之间不会互相连边,我们只能让所有儿子区间一个包含另一个。为了方便起见,我们让所有儿子的区间和父亲区间的交在左端点,大致如下图所示:

下面继续考虑如何将子树合并。不难发现为了让子树之间不会互相连边,一个最简单的方法就是将不同子树内的点分割开来,但因为所有儿子还是要与父亲连边,因此我们还需将所有儿子构造成上面那种菊花图的形式,具体地大致如下图所示:

不难发现这样刚好用到的点数为 \(2n\) 个,具体实现时从根往儿子递归,传入当前子树内能使用的区间范围即可。

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define Next(i, u) for (int i = h[u]; i; i = e[i].next)
const int N = 500000 + 5;
struct edge {
int v, next;
}e[N << 1];
int n, u, v, tot, h[N], l[N], r[N], s[N], sz[N];
int read() {
char c; int x = 0, f = 1;
c = getchar();
while (c > '9' || c < '0') { if(c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
void add(int u, int v) {
e[++tot].v = v, e[tot].next = h[u], h[u] = tot;
e[++tot].v = u, e[tot].next = h[v], h[v] = tot;
}
void Prefix(int u, int fa) {
s[u] = 1;
Next(i, u) if(e[i].v != fa) Prefix(e[i].v, u), s[u] += s[e[i].v];
}
void dfs(int u, int fa, int L, int R) {
int ch = 0; sz[u] = 1;
Next(i, u) {
int v = e[i].v; if(v == fa) continue;
++ch, dfs(v, u, L + 2 * sz[u] - ch - 1, L + 2 * sz[u] - ch + 2 * (s[v] - 1));
r[v] = R - ch, sz[u] += sz[v];
}
l[u] = R - ch - 1; if(u == 1) r[u] = R;
}
int main() {
n = read();
rep(i, 1, n - 1) u = read(), v = read(), add(u, v);
Prefix(1, 0), dfs(1, 0, 1, 2 * n);
rep(i, 1, n) printf("%d %d\n", l[i], r[i]);
return 0;
}

CF1278E Tests for problem D的更多相关文章

  1. Unit Tests

    The Three Laws of TDD First Law : you may not write production code until you have written a failing ...

  2. Educational Codeforces Round 78 (Rated for Div. 2) 题解

    Shuffle Hashing A and B Berry Jam Segment Tree Tests for problem D Cards Shuffle Hashing \[ Time Lim ...

  3. Codeforces题解集 1.0

    记录 Codeforces 2019年12月19日到 2020年2月12日 的部分比赛题 Educational Codeforces Round 82 (Rated for Div. 2) D Fi ...

  4. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

  5. 转:VS2010调试NUnit测试项目 (Running or debugging NUnit tests from Visual Studio without any extensions)

    If you write unit tests and use NUnit test framework this may be helpful. I decided to write this si ...

  6. HDU 4910 Problem about GCD 找规律+大素数判断+分解因子

    Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. HDOJ 1914 The Stable Marriage Problem

    rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6553 ...

  8. 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集

    [题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...

  9. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

随机推荐

  1. Gumbel distribution

    目录 概 主要内容 定义 Gumbel-Max trick Gumbel trick 用于归一化 代码 概 感觉这个分布的含义很有用啊, 能预测'最大', 也就是自然灾害, 太牛了. 主要内容 定义 ...

  2. Interval Bound Propagation (IBP)

    目录 概 主要内容 IBP CROWN CROWN-IBP 训练的技巧 写在最后 代码 Gowal S., Dvijotham K., Stanforth R., Bunel R., Qin C., ...

  3. [Box] Robust Training and Initialization of Deep Neural Networks: An Adaptive Basis Viewpoint

    目录 概 主要内容 LSGD Box 初始化 Box for Resnet 代码 Cyr E C, Gulian M, Patel R G, et al. Robust Training and In ...

  4. Mysql数据库服务端的安装

    一般提到Mysql数据库的安装在工作当中是说的安装数据库管理软件的服务端,服务端的安装可以安装在Windows环境,也可以安装在Linux环境. Windows环境安装:目前安装比较流行的是5.7,增 ...

  5. [Git]解决Permission denied, please try again问题

    在gitlab上传项目的时候出现Permission denied, please try again问题, 网上有很多解释,但是都没能解决我的问题,后来经过自己尝试成功了,这里把经验分享给大家. 在 ...

  6. RotateRect(旋转矩形)的倾斜旋转变换矫正

    在Opencv中的图像处理中,经常要用到minAreaRect()函数求最小外接矩形,该函数的返回值就是一个RotatedRect类对象. RotatedRect类定义如下: class CV_EXP ...

  7. N-Empress

    全排列 基本思想:递归.散列 代码实现 #include<cstdio> const int maxn = 11; int n, P[maxn], hashTable[11] = {fal ...

  8. EntityFrameworkCore数据迁移(一)

    .net core出来已经有很长一段时间了,而EentityFrameworkCore(后面简称EFCore)是.net framework的EntityFramework在.net core中的实现 ...

  9. Nginx入门--从核心配置与动静分离开始

    写在前面 优化我们项目,服务器部署,不仅仅可以是分布式,Nginx一样可以通过动静分离,负载均衡来减轻我们服务器的压力.Nginx的知识链,学习周期相对比较长,博主也是刚刚入门,这篇就先从实现基础的动 ...

  10. Python+flask+flask-apscheduer实现定时下发任务

    Python+flask+flask-apscheduer实现定时下发任务 背景: 使用python+flask+mamaca实现的自动化用例管理平台,可以下发任务到具体的节点,进行执行测试用例,没有 ...