【CodeForces - 260D】Black and White Tree (思维构造,猜结论,细节,构造一棵树)

题干:

The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn't contain any cycles.

Each node of the graph is painted black or white in such a manner that there aren't two nodes of the same color, connected by an edge. Each edge contains its value written on it as a non-negative integer.

A bad boy Vasya came up to the board and wrote number sv near each node v — the sum of values of all edges that are incident to this node. Then Vasya removed the edges and their values from the board.

Your task is to restore the original tree by the node colors and numbers sv.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 105) — the number of nodes in the tree. Next n lines contain pairs of space-separated integers cisi(0 ≤ ci ≤ 1, 0 ≤ si ≤ 109), where ci stands for the color of the i-th vertex (0 is for white, 1 is for black), and si represents the sum of values of the edges that are incident to the i-th vertex of the tree that is painted on the board.

Output

Print the description of n - 1 edges of the tree graph. Each description is a group of three integers viuiwi (1 ≤ vi, ui ≤ nvi ≠ ui, 0 ≤ wi ≤ 109), where vi and ui— are the numbers of the nodes that are connected by the i-th edge, and wi is its value. Note that the following condition must fulfill cvi ≠ cui.

It is guaranteed that for any input data there exists at least one graph that meets these data. If there are multiple solutions, print any of them. You are allowed to print the edges in any order. As you print the numbers, separate them with spaces.

Examples

Input

3
1 3
1 2
0 5

Output

3 1 3
3 2 2

Input

6
1 0
0 3
1 8
0 2
0 3
0 0

Output

2 3 3
5 3 3
4 3 2
1 6 0
2 1 0

题目大意:

   给你n个点,然后是每个点的颜色(用0或1表示)和 与这个点相邻的边的权值,让你恢复一棵树(输出格式为u,v,w(边权))

解题报告:

   首先告诉你一定有解了,那么我们只需要构造出一组解就可以了。(因为说明黑色的点的权值之和 等于 白色的点的权值之和)构造的方法选择贪心,每次选择两个最小的黑白点然后 把权值较小的全都连到权值较大的边上,最后别忘了特判剩下的顶点全为0的情况。。。细节不少啊。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
vector<pair<int, int> > a,b;//1==黑 0==白
int main()
{int n;//cout << (1,0) << endl;cin>>n;for(int i=1,col,w; i<=n; i++) {scanf("%d%d",&col,&w);if(col) a.pb(pm(w,i));else b.pb(pm(w,i));}sort(a.begin(),a.end());sort(b.begin(),b.end());
//	for(int i = 0; i<a.size(); i++) printf("%d %d\n",a[i].fi,a[i].se);int cnt1=0,cnt2=0;int up1=a.size();int up2=b.size();
//	int zero1=0,zero2=0;
//	while(a[zero1].fi == 0) zero1++;
//	while(b[zero2].fi == 0) zero2++;while(cnt1 < up1 && cnt2 < up2) {int tmp = min(a[cnt1].fi,b[cnt2].fi);printf("%d %d %d\n",a[cnt1].se,b[cnt2].se,tmp);a[cnt1].fi -= tmp;b[cnt2].fi -= tmp;
//		if(a[cnt1].fi) cnt2++;
//		else if(b[cnt2].fi) cnt1++;if(a[cnt1].fi == 0 && cnt1 < up1-1) cnt1++;else if(b[cnt2].fi == 0 && cnt2 < up2-1) cnt2++;else if(cnt1 < up1-1) cnt1++;else cnt2++;}return 0 ;}

 或者用上面那两行注释的也可以。

这样写是不对的:

	while(cnt1 < up1 && cnt2 < up2) {int tmp = min(a[cnt1].fi,b[cnt2].fi);printf("%d %d %d\n",a[cnt1].se,b[cnt2].se,tmp);a[cnt1].fi -= tmp;b[cnt2].fi -= tmp;
//		if(a[cnt1].fi) cnt2++;
//		else if(b[cnt2].fi) cnt1++;if(a[cnt1].fi == 0 && cnt1 < up1) cnt1++;else if(b[cnt2].fi == 0 && cnt2 < up2) cnt2++;

对于这样的样例

6
0 0
1 0
0 0
1 0
0 0
1 0

是过不了的、、所以必须cnt1<up1-1这样,对于其他的情况再另行判断 

总结:

  好cai啊、、、5555又被虐了

 其实为了避免上面那些 繁琐的情况,还可以用优先队列写,最后清空的时候直接暴力清空就可以了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441212.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

手游 自建服务器,英灵神殿自己搭建服务器怎么弄

英灵神殿是一款冒险生存类游戏&#xff0c;在英灵神殿游戏中小伙伴要自己搭建服务器才能玩&#xff0c;那么要怎么搭建&#xff0c;有什么技巧吗?接下来和小编一起来看看吧!Valheim英灵神殿服务器搭建技巧第一步&#xff1a;SteamCMD和安装内容从这里下载SteamCMD。将其提取到…

描写火车站场景_关于描写火车站的句子

关于描写火车站的句子关于描写火车站的句子所以至今&#xff0c;我仍很爱乘火车&#xff0c;卧铺&#xff0c;靠在枕头上看书&#xff0c;沉沉睡去&#xff0c;听铁轨一层一层的荡漾&#xff0c;在记忆中昏暗地穿行&#xff0c;如果我爱谁&#xff0c;我们一定会乘火车去很远的…

【POJ - 1463】Strategic game (树上最小点覆盖,树形dp)

题干&#xff1a; Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree…

vue css自定义标签,Vue如何使用CSS自定义变量

目录在 css 自定义变量的功能以及出来许久了&#xff0c;但实际开发中大家使用并不多。归其原因是因为 less、sass 等预处理器已经拥有定义变量的功能&#xff0c;以及Vue、react很方便设置 style 样式&#xff0c;大家可能觉得使用 css 定义变量不方便且没必要。实则不然举个最…

【CodeForces - 260A】Adding Digits (思维构造)

题干&#xff1a; Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to repeat the operation of lengthening number a n times. One operation of lengthening a number means adding exactly one digit to the number (in the…

人工智能在fpga的具体应用_人工智能带动了FPGA的发展

未来几年&#xff0c;人工智能芯片的需求将急剧上升。联合市场研究公司(Allied Market Research)预估&#xff0c;未来5年&#xff0c;人工智能芯片的需求将以每年近50&#xff05;的速度增长&#xff0c;因为该技术将在汽车、医疗保健等多个领域得到广泛应用。Xilinx深知其中存…

恐怖黎明稳定服务器,恐怖黎明新人联机图文教程 怎么联机-游侠网

恐怖黎明怎么联机?不少玩家想体验联机&#xff0c;但是不知道方法&#xff0c;小编这里给大家带来了新人联机图文教程&#xff0c;不会的萌新来学习下吧。联机图文教程:A1 联机&#xff1a;因为gd(grimdawn)没有自己的战网平台(就是专供联机玩的专职服务器 server)&#xff0c…

【CodeForces - 260B 】Ancient Prophesy (暴力匹配,BF算法,日期字符串)

题干&#xff1a; A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters "-". Well say that some date is mentioned in the Prophesy if there is a su…

github 上传代码_leetcode爬虫:爬取代码;生成readme;上传github

Leetcode-Helper哪个程序员 不想一键下写过的代码&#xff0c;自动上传Github&#xff0c;并且还能生成好看的README呢&#xff1f;有用的话点个⭐吧&#xff0c;谢谢你。Leetcode-Helper传送门​github.com主要功能 模拟登陆力扣中国站(leetcode-cn)爬取每题提交的ac代码&…

绝地求生信号枪只能在服务器吗,绝地求生信号枪怎么用?信号枪刷新点及用法详解...

绝地求生信号枪怎么用&#xff1f;信号枪刷新点及用法详解2018-03-15 15:22:12来源&#xff1a;吃鸡小助手编辑&#xff1a;野狐禅评论(0)绝地求生近日更新中悄悄加入了信号枪&#xff0c;引得广大玩家热情满满的在游戏中寻找&#xff0c;信号枪到底怎么用呢&#xff1f;下面就…

spring的beanutils工具类_基于spring-beans实现工具类BeanUtils基于Class实例化注入对象及查找方法、复制属性等操作...

一、前言基于spring-beans(4.1.4)的工具类org.springframework.beans.BeanUtils对注入spring对象按照Class实例化instantiateClass、class对象方法名称methodName查找findMethod、属性查找对于class类信息findPropertyType、对象属性复制copyProperties等常用操作&#xff0c;具…

【CodeForces - 260C】Balls and Boxes (思维模拟,有坑,时光倒流)

题干&#xff1a; Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right. Once Vasya chose one of the boxes, lets assume that its number is i, took all balls out from it (it …

文件服务器共享文件夹访问权限,5对文件服务器中的共享文件夹进行访问权限控制...

对文件服务器中的共享文件夹进行访问权限控制1. 实训目的在Windows Server 2003环境下设置文件服务器的目的是要对多用户进行资源共享&#xff0c;这其中经常遇到不同用户应该分配不同权限的问题&#xff0c;通过这个实训希望读者了解Windows Server 2003中访问权限设置方法和具…

java生日正则表达式_java之正则表达式、日期操作

正则表达式和日期操作正则表达式简介正则表达式就是使用一系列预定义的特殊字符来描述一个字符串的格式规则&#xff0c;然后使用该格式规则匹配某个字符串是否符合格式要求。作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的规…

渲染服务器位置,如何用服务器做渲染

如何用服务器做渲染 内容精选换一换&#xfffd;&#xfffd;&#xfffd;&#xfffd;BoostKit ARMԭ&#xfffd;&#xfffd;ʹ&#xfffd;&#xfffd;&#xfffd;׼&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;嵥&#xfffd;&#xfffd…

【HDU - 2376】Average distance (树,平均距离,算贡献)

题干&#xff1a; Given a tree, calculate the average distance between two vertices in the tree. For example, the average distance between two vertices in the following tree is (d 01 d 02 d 03 d 04 d 12 d 13 d 14 d 23 d 24 d 34)/10 (63799131510122)/10…

读取ppt并存入数据库_Java解析Excel文件并把数据存入数据库

前段时间做一个小项目&#xff0c;为了同时存储多条数据&#xff0c;其中有一个功能是解析Excel并把其中的数据存入对应数据库中。花了两天时间&#xff0c;不过一天多是因为用了"upload"关键字作为URL从而导致总报同一个错&#xff0c;最后在同学的帮助下顺利解决&a…

两台虚拟服务器如何级联,[教程] 利用open vswitch建立vxlan隧道实现不同主机上的虚拟交换机级联...

写在开头在某些环境下&#xff0c;需要实现两台物理机中的openvswitch交换机级联&#xff0c;以实现两台交换机中的设备互相通讯&#xff0c;这里使用vxlan隧道技术&#xff0c;将数据包封装在UDP中&#xff0c;通过以太网实现数据包传输。VXLAN是一种大二层的虚拟技术&#xf…

【POJ - 3041】Asteroids (二分图,最小点覆盖)

题干&#xff1a; Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 < N < 500). The grid contains K asteroids (1 < K < 10,000), which are conveniently located at the lattice points of the…

华为虚拟服务器lanip地址,2018软考网络工程师《华为基础实验》十九配置路由器为DHCPServer...

原标题&#xff1a;2018软考网络工程师《华为基础实验》十九配置路由器为DHCPServer实验要求:在R1上使能DHCP 功能。创建三个全局地址池&#xff0c;用于为三个不同部门的PC分配IP 地址。配置地址池的相关属性。在R1的接口下配置基于全局地址池的服务方式&#xff0c;实现DHCP …