Codeforces round #628 C.Ehab and Path-etic MEXs

You are given a tree consisting of n nodes. You want to write some labels on the tree’s edges such that the following conditions hold:

Every label is an integer between 0 and n−2 inclusive.
All the written labels are distinct.
The largest value among MEX(u,v) over all pairs of nodes (u,v) is as small as possible.
Here, MEX(u,v) denotes the smallest non-negative integer that isn’t written on any edge on the unique simple path from node u to node v.

Input

The first line contains the integer n (2≤n≤1e5) — the number of nodes in the tree.

Each of the next n−1 lines contains two space-separated integers u and v (1≤u,v≤n) that mean there’s an edge between nodes u and v. It’s guaranteed that the given graph is a tree.

Output

Output n−1 integers. The ith of them will be the number written on the ith edge (in the input order).

Examples

input

3
1 2
1 3

output

0
1

input

6
1 2
1 3
2 4
2 5
5 6

output

0
3
2
4
1

题目看似复杂其实很简单,我们考虑往边上填数字,有下面两种情况:
1.边的两个端点都不是叶子节点,即这条边为长链的一截,我们用贪心的策略不难发现,长链要往上面填大的数字,这样 MEX(u,v)MEX(u,v)MEX(u,v) 才能尽可能小
2.边上有一个结点为叶子节点,此时这条边构成一条长为1的短链,我们要从小往大填
这样就能构成一种最优解,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
vector<int>G[maxn];
struct node{int u,v;
}E[maxn];
int main()
{int n,u,v;scanf("%d",&n);for(int i=1;i<n;i++){scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);E[i].u=u,E[i].v=v;}int mx=n-2,mn=0;for(int i=1;i<n;i++){if(G[E[i].u].size()==1 || G[E[i].v].size()==1){printf("%d\n",mn);mn++;}else{printf("%d\n",mx);mx--;}}return 0;
}

Codeforces round #628 C.Ehab and Path-etic MEXs相关推荐

  1. [Codeforces Round #628]1325C - Ehab and Path-etic MEXs[思维][图]

    1325C - Ehab and Path-etic MEXs[思维][图] time limit per test memory limit per test input output 1 seco ...

  2. Codeforces round #628 C. Ehab and Path-etic MEXs(构造)

    讲解:https://www.cnblogs.com/Sxy_Limit/p/12495969.html #include<cstdio> #include<cstring> ...

  3. Codeforces Round #628 (Div. 2) F. Ehab‘s Last Theorem dfs树

    传送门 文章目录 题意: 思路: 题意: 给你个nnn个点mmm条边的图,可以选择完成以下两个任务中的一个: (1)(1)(1)找出大小恰好为n\sqrt nn​的一个独立集. (2)(2)(2)找出 ...

  4. Codeforces Round #628 (Div. 2) E. Ehab‘s REAL Number Theory Problem 巧妙的质因子建图

    传送门 文章目录 题意: 思路: 题意: 给你nnn个数,每个数的因子个数不超过777个,选出最少的数使其乘积为平方数. n≤1e5n\le 1e5n≤1e5 思路: 由于因子不超过777个,所以由约 ...

  5. Codeforces Round #628 (Div. 2)C - Ehab and Path-etic MEXs

    C - Ehab and Path-etic MEXs 题目链接 简要描述: 从一个点到另一个点出发,所经过的边组成一个集合, 这些集合中所不包含的最小自然数为mex(u,v),要求使所有的mex(u ...

  6. Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs

    C. Ehab and Path-etic MEXs 题目链接-C. Ehab and Path-etic MEXs Note The tree from the second sample: 题目大 ...

  7. Codeforces Round #628 (Div. 2) D. Ehab the Xorcist

    D. Ehab the Xorcist 题目链接-D. Ehab the Xorcist 题目大意 给定两个数字 u 和 v ,构造一个最短的序列a满足a1⊕a2⊕-⊕an=u 并且 a1+a2+-+ ...

  8. Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(贪心+思维)

    题目链接 思路:这个题做法貌似很多,我用的就是从叶子节点开始赋值,不过听大佬说的从度大于2的节点赋值也可以.比赛的时候wa test19了,特判一下n=2就行了. #include<bits/s ...

  9. Codeforces Round #628 (Div.2) C.Ehab and Path-etic MEXs(树,思维)

    传送门 题意: 给一颗n个结点的数,然后n-1条边,我们要做的就是把0-n-2,这n-1个数赋给n-1条边,然后使得所有MEX(u,v)最大值最小,输出每条边赋的值 MEX(u,v)是u到v这条路径上 ...

最新文章

  1. 请教context:component-scan/和mvc:annotation-driven/的区别20
  2. 九度 1408 寻找表达式 (中缀转后缀)
  3. SQLServer中进行sql除法运算结果为小数时显示0的解决方案
  4. Myeclipse的破解步骤
  5. DL之GANDCGNNcGAN:GANDCGNNcGAN算法思路、关键步骤的相关配图和论文集合
  6. 抢购网站服务器时间表,js获取服务器时间,实现抢购倒计时
  7. 找出SAP OData service出错根源的小技巧
  8. #抵抗3#(#Resistance 3#) 绝对值得体验的冒险历程
  9. 这份精子保存了1亿年,不仅长度惊人还刷新了一项记录
  10. Validator验证Ajax提交表单的方法
  11. centos7共享网络盘_实验08:局域网文件和互联网文件的共享
  12. character-RNN模型介绍以及代码解析
  13. 阿里、腾讯双双辟谣:云计算魅力彰显
  14. mysql与_mysql常见的运算符及使用
  15. StretchDIBits 函数
  16. 短视频剪辑入门技巧,简单却重要
  17. 手机停机后你们知道怎么打电话?教你鲜为人知的手机锦囊
  18. python对整数进行因数分解_浅谈将一个正整数分解质因数的逻辑思维和Python开发设计...
  19. 01背包问题 动态规划求解方法 动态方程的详细解释 能理解的解释(附python代码)
  20. 微信视频号如何直播游戏效果最好?教你最简单的方法

热门文章

  1. 我的世界基岩版种子和java版种子_我的世界:值得珍藏3个种子,号称MC最“完美”的三座山脉!...
  2. Seurat | 强烈建议收藏的单细胞分析标准流程(差异分析与细胞注释)(五)
  3. 心路历程:爬虫实战——从数据到产品
  4. SECS/GEM300mm通讯协议概念
  5. microsoft exchange server 2007 mrm
  6. 捷联惯导系统学习4.1(惯导数值更新算法)
  7. 计算机组成原理分时传送电路设计,计算机组成原理和系统结构实验仪,FPGA设计,上海求育...
  8. 个人永久性免费-Excel催化剂功能第96波-地图数据挖宝之全国天气查询(区域最细可到区县,最长预报4天)...
  9. 强化学习 | Multi Agents | Trust Region | HATRPO | HAPPO
  10. 如何在PC桌面上添加便笺