DFS,把和当前结点相连的点全都括在当前结点左右区间里,它们的左端点依次++,然后对这些结点进行DFS,优先对左端点更大的进行DFS,这样它右端点会先括起来,和它同层的结点(后DFS的那些)的区间会把它括起来,这样它们就不会相交了。

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int cnt=;
vector<int>v[];
int l[],r[];
void dfs(int x,int fa){
for(int i=;i<v[x].size();++i)
if(v[x][i]!=fa)
l[v[x][i]]=++cnt;
r[x]=++cnt;
for(int i=v[x].size()-;i>=;--i)
if(v[x][i]!=fa)
dfs(v[x][i],x);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<n;++i){
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(,);
l[]=;
for(int i=;i<=n;++i)
cout<<l[i]<<" "<<r[i]<<"\n";
return ;
}

Educational Codeforces Round 78 (Rated for Div. 2)E(构造,DFS)的更多相关文章

  1. Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree

    链接: https://codeforces.com/contest/1278/problem/D 题意: As the name of the task implies, you are asked ...

  2. Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam

    链接: https://codeforces.com/contest/1278/problem/C 题意: Karlsson has recently discovered a huge stock ...

  3. Educational Codeforces Round 78 (Rated for Div. 2) B. A and B

    链接: https://codeforces.com/contest/1278/problem/B 题意: You are given two integers a and b. You can pe ...

  4. Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing

    链接: https://codeforces.com/contest/1278/problem/A 题意: Polycarp has built his own web service. Being ...

  5. 【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)

    比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 ...

  6. Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配)

    题:https://codeforces.com/contest/1278/problem/B 思路:还是把1~n分配给俩个数,让他们最终相等 假设刚开始两个数字相等,然后一个数字向前走了abs(b- ...

  7. Educational Codeforces Round 78 (Rated for Div. 2)

    A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...

  8. Educational Codeforces Round 78 (Rated for Div. 2) --补题

    链接 直接用数组记录每个字母的个数即可 #include<bits/stdc++.h> using namespace std; int a[26] = {0}; int b[26] = ...

  9. 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 ...

  10. Educational Codeforces Round 78 (Rated for Div. 2) C - Berry Jam(前缀和)

随机推荐

  1. Unable to connect to Elasticsearch at http://elasticsearch:9200. statu red Kibana 安装

    解决方案 docker run -d -p 5601:5601 --link elasticsearch -e "elasticsearch_url=容器ip:9200" --na ...

  2. SpringMVC起步

    SpringMVC: SpringMVC是Spring的一个组件,作为控制器,可以替代Servlet SpringMVC的开发过程: 请求发送 DispatcherServlet查询一个或多个Hand ...

  3. wpf实现一个windows定时关机的工具

    基本界面 起源 在家睡前喜欢用电脑放情景喜剧看,电脑需要定时关机,一开始直接命令行定时关机,感觉有点小麻烦, 于是最近弄了个有界面的 主要功能 在指定的时间之后执行 关机|休眠|重启 的操作, 支持取 ...

  4. Oracle11g配置监听

    步骤 1.在windows系统上安装好Oracle后,点击右下角开始菜单Oracle目录下选择Net Manager进行配置,也可以使用Net Configuration Assistant(建议使用 ...

  5. AcWing 1012. 友好城市

    #include<iostream> #include<algorithm> using namespace std ; typedef pair<int,int> ...

  6. Django | 解决“(1146, "Table 'mydb.django_session' doesn't exist")”报错的方法

    我只写了下面一行 就生成了session表 manage.py makemigrations sessions manage.py migrate sessions 参考:https://www.cn ...

  7. windows下tesseract-ocr的安装及使用

    For CentOS 7 run the following as root: yum-config-manager --add-repo https://download.opensuse.org/ ...

  8. Centos7部署jenkins

    1.       下载rpm包: a)         下载地址:https://pkg.jenkins.io/redhat-stable/ b)         点选一个下载即可,例如点选:“jen ...

  9. energy/heating data source

    HeatManDataLake 1. schema: hfors tables ambient_temperature_emd record the ambient temperature hourl ...

  10. Redis 数据结构的底层实现 (二) dict skiplist intset

    一.REDIS_INCODING_HT (dict字典,hashtable) dict是一个用于维护key和value映射关系的数据结构.redis的一个database中所有的key到value的映 ...