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

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

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

不难发现这样刚好用到的点数为 \(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. Codeforces 567D:One-Dimensional Battle Ships(二分)

    time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...

  2. iis站点下发布多个vue项目

    记录一下iis上某个域名下发布多个vue项目的过程,主要分为webpack打包前的配置和iis重定向的配置. vue打包配置: 1.在webpack 配置文件中(以vue.config.js为例),指 ...

  3. FastAPI快速查阅

    官方文档主要侧重点是循序渐进地学习FastAPI, 不利于有其他框架使用经验的人快速查阅 故本文与官方文档不一样, 并补充了一些官方文档没有的内容 安装 包括安装uvicorn $pip instal ...

  4. Nginx_配置文件nginx.conf配置详解

    user nginx nginx ; # Nginx用户及组:用户 组.window下不指定 worker_processes 8; # 工作进程:数目.根据硬件调整,通常等于CPU数量或者2倍于CP ...

  5. js- float类型相减 出现无限小数的问题

    6.3 -1.1 是不是应该等于5.2? 但是js 会导致得出 5.19999999999的结果 怎么办?可以先先乘100 后相减,然是用方法 舍入为最接近的整数,然后再除于100, Math.rou ...

  6. spring boot + redis --- 心得

    1.前言 习惯使用springMVC 配置 redis ,现在使用spring boot ,得好好总结怎么在spring boot 配置和使用 ,区别真的挺大的. 2.环境 spring boot  ...

  7. docker 环境安装 jenkins

    下载镜像运行 jenkins 默认服务器已经安装好了 docker 环境: 拉取 jenkins 镜像 docker pull jenkins/jenkins:lts 镜像的详细信息可以查看:http ...

  8. c++智能指针的使用,shared_ptr,unique_ptr,weak_ptr

    c++智能指针的使用 官方参考 普通指针的烦恼:内存泄漏,多次释放,提前释放 智能指针 负责自动释放所指向的对象. 三种智能指针 shared_ptr,unique_ptr,weak_ptr: 将sh ...

  9. Redis之持久化方式详解

    背景:Redis之所以能够在技术革新发展迅速的时代超越Memcache等其他Nosql数据库,最主要的一点是Redis提供数据持久化,能够根据持久化策略将缓存数据灵活的写到磁盘上,更好地满足了当下海量 ...

  10. 用 vite 构建项目,同时支持微前端

    得益于 esbuild 的超高性能,vite 在诞生之初就备受关注,且一直保持着活跃的开发迭代.截至目前,vite 已经迭代到了 2.7.10 版本,各方面也基本具备了在生产使用的条件.这段时间,我在 ...