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≤105) — 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
Note
The tree from the second sample:

思路:挺考验思维的一道题目。我们可以想一下,0和1一定是可以在同一路径上的,因此最小价值一定大于等于2.那么我们就尽可能的去往2上靠。要想是2的话,那么0,1,2肯定不能在同一路径上,这样的话,我们找到一个度大于等于3的点,然后安排上0,1,2,剩下的随便放置,就可以了。对于没有度数大于等于3的点来说,就是一条链,最终结果肯定是n-1了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e5+100;
struct node{int x,y;
}p[maxx];
vector<int> v[maxx];
int n;inline void init()
{for(int i=1;i<=n;i++) v[i].clear();
}
int main()
{scanf("%d",&n);init();for(int i=1;i<n;i++){scanf("%d%d",&p[i].x,&p[i].y);v[p[i].x].push_back(p[i].y);v[p[i].y].push_back(p[i].x);}map<pair<int,int>,int> mp;mp.clear();int cnt=0;for(int i=1;i<=n;i++){if(v[i].size()>=3){for(int j=0;j<3;j++) mp[make_pair(i,v[i][j])]=++cnt;break;}}for(int i=1;i<n;i++){if(mp[make_pair(p[i].x,p[i].y)]) cout<<mp[make_pair(p[i].x,p[i].y)]-1<<endl;else if(mp[make_pair(p[i].y,p[i].x)]) cout<<mp[make_pair(p[i].y,p[i].x)]-1<<endl;else cout<<cnt++<<endl;}return 0;
}

努力加油a啊,(o)/~

Ehab and Path-etic MEXs CodeForces - 1325C(思维+贪心)相关推荐

  1. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  2. Codeforces 1093C (思维+贪心)

    题面 传送门 题目大意: 有一个长n(n为偶数)的序列a 已知a满足 \(a_1≤a_2≤⋯≤a_n\) 给出一个长度为\(\frac{n}{2}\) 的序列b,定义\(b_i=a_i+a_{n-i+ ...

  3. [codeforces 1325C] Ehab and Path-etic MEXs 绕不开的叶节点+特判

    Codeforces Round #628 (Div. 2)   比赛人数9400 [codeforces 1325C]  Ehab and Path-etic MEXs   绕不开的叶节点+特判 总 ...

  4. F 魏迟燕的自走棋(思维+贪心+并查集维护联通块/左部点配对边<=2的匈牙利)

    https://ac.nowcoder.com/acm/contest/9984/F 参考:F 魏迟燕的自走棋(贪心+并查集) 将每个人看成一个点,武器的能力值抽象成边,这样就转化成图论的模型了. 然 ...

  5. CodeForces - 1325C Ehab and Path-etic MEXs

    You are given a tree consisting of nn nodes. You want to write some labels on the tree's edges such ...

  6. Codeforces 1325C. Ehab and Path-etic MEXs(构造)

    Description You are given a tree consisting of n nodes. You want to write some labels on the tree's ...

  7. Ehab and Prefix MEXs CodeForces - 1364C(思维)

    Given an array a of length n, find another array, b, of length n such that: for each i (1≤i≤n) MEX({ ...

  8. 【Codeforces Round #525(Div. 2)】Ehab and another another xor problem(思维+异或)

    题目链接 D. Ehab and another another xor problem time limit per test 1 second memory limit per test 256 ...

  9. CodeForces - 1174D Ehab and the Expected XOR Problem(构造+思维+位运算)

    题目链接:点击查看 题目大意:给出一个 n,再给出一个 x,要求构造一个数列,满足该数列的所有子串的异或和都不等于 0 且都不等于 x,在满足上面的条件下尽可能长 题目分析:因为这个题目最终的目标是需 ...

最新文章

  1. 内存对齐的规则以及作用
  2. mac下完全卸载postgresql的方法
  3. hibernate 多条件组合查询之sql拼接
  4. c语言怎么用movc指令,【图片】求助大佬用c语言帮忙编写下程序【c程序吧】_百度贴吧...
  5. 所有表单对象_表单太多汇总太累?请看这里,我们带你一键汇总
  6. PreScan快速入门到精通第十一讲之PreScan道路标记,建筑物、抽象物体及交通标识
  7. oracle数据库课后报告,ORACLE数据库课程设计报告
  8. linux中vim命令详解(操作大全)
  9. windows杀进程
  10. python大众点评霸王餐_如何抽中大众点评霸王餐?
  11. SOC堡垒机运维管理平台
  12. autocad application 版本
  13. Vue中使用防抖与截流
  14. 京训钉怎么快速看完_在钉钉用培训机构CRM管理系统,助力协同办公一体化
  15. 全球首位 AI 律师出庭,花 100 万美元找“传话筒”!网友:头脑正常的人谁会同意?...
  16. selenium+chrome使用webrtc音频或视频时,默认开启麦克风和摄像头
  17. 使用python实现mysql测试数据的准备(大批量导入数据)
  18. 开发工具合集专题《管理工具,DoxygenAPI文档自动生成》
  19. nginx是什么?有什么用?
  20. 在打印服务器中新增纸张规格后,在打印机首选项中的自定义纸张中看不到的原因

热门文章

  1. Android—MVC、MVP、MVVM
  2. 第11章 Internet 服务器应用课后习题答案
  3. tsp 选边 matlab,【转载】蚁群算法TSP(旅行商问题)通用matlab程序
  4. mysql慕课网笔记_mysql学习笔记
  5. html 点击文本框则选中,JS事件 内容选中事件(onselect)选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。...
  6. php 下拉菜单 搜索,DedeCMS实现百度搜索下拉菜单提示信息功能
  7. 如何更改指定用户在windows系统目录的权限
  8. Android开机自启监听网络改变源码
  9. jcenter和maven下载失败Can't connect to SOCKS proxy:Connection refused: connect
  10. 计算机科学与教育信息化国际会议,A Courses Ontology System for Computer Science Education...