Codeforces 260D - Black and White Tree

2024-05-29 07:38
文章标签 codeforces tree black white 260d

本文主要是介绍Codeforces 260D - Black and White Tree,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://codeforces.com/problemset/problem/260/D

题意: 有n个点的一个树,同一条边的两个点涂成不同的颜色(black & white),每条边有一个权值,题目给出n , 和每个点的颜色 和 与每个点相连的边的权值和。

求出n-1条边 的端点和权值。

n的 大小  10e5.  这有是一个构造的 题目。

由于题目说明 肯定可以构造出来, 所以,白色点的权值和的 总和 == 黑色点的权值和的 总和 。

可以分别把白色点 和黑色点  按照 权值和大小 排序。 然后分别从 两堆点中,取出最小两个点,这两点连边,权值为 两者的min, 之后两点的权值和分别减去 这个min,把权值为0的点delete, 直到处理完 n-1个点。 (注意,如果取出两个点 的权值都为0 的情况)

code: 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define INF 1000000000
#define N 100005
int ad1,ad2,n;
struct node
{int su,id;
};
struct node a[N],b[N];
bool cmp(struct node x,struct node y)
{return x.su<y.su;
}
int main()
{scanf("%d",&n);int col,su;ad1=ad2=0;for(int i=1;i<=n;i++){scanf("%d%d",&col,&su);if(col){a[++ad1].su=su;a[ad1].id=i;}else{b[++ad2].su=su;b[ad2].id=i;}}sort(a+1,a+1+ad1,cmp);sort(b+1,b+1+ad2,cmp);for(int i=1,j=1;i<=ad1 && j<=ad2;){int tmp=min(a[i].su,b[j].su);printf("%d %d %d\n",a[i].id,b[j].id,tmp);a[i].su-=tmp; b[j].su-=tmp;if(a[i].su) j++;else if(b[j].su) i++;else if(i<ad1) i++;else j++;}return 0;
}


这篇关于Codeforces 260D - Black and White Tree的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1013050

相关文章

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

玩转Web之easyui(二)-----easy ui 异步加载生成树节点(Tree),点击树生成tab(选项卡)

关于easy ui 异步加载生成树及点击树生成选项卡,这里直接给出代码,重点部分代码中均有注释 前台: $('#tree').tree({ url: '../servlet/School_Tree?id=-1', //向后台传送id,获取根节点lines:true,onBeforeExpand:function(node,param){ $('#tree').tree('options'

【C++PCL】点云处理Kd-tree原理

作者:迅卓科技 简介:本人从事过多项点云项目,并且负责的项目均已得到好评! 公众号:迅卓科技,一个可以让您可以学习点云的好地方 重点:每个模块都有参数如何调试的讲解,即调试某个参数对结果的影响是什么,大家有问题可以评论哈,如果文章有错误的地方,欢迎来指出错误的地方。 目录         1.原理介绍 1.原理介绍         kd-tree是散乱点云的一种储存结构,它是一种

[leetcode] 107. Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II 描述 Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example

[leetcode] 102. Binary Tree Level Order Traversal

Binary Tree Level Order Traversal 描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20

[leetcode] 515. Find Largest Value in Each Tree Row

Find Largest Value in Each Tree Row 描述 You need to find the largest value in each row of a binary tree. Example: Input: 1/ \3 2/ \ \ 5 3 9 Output: [1, 3, 9] 我的代码 简单的dfs。 要使

[leetcode] 257. Binary Tree Paths

* Binary Tree Paths* 描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [“1->2->5”, “1->3”] 我的代码

论文《Tree Decomposed Graph Neural Network》笔记

【TDGNN】本文提出了一种树分解方法来解决不同层邻域之间的特征平滑问题,增加了网络层配置的灵活性。通过图扩散过程表征了多跳依赖性(multi-hop dependency),构建了TDGNN模型,该模型可以灵活地结合大感受场的信息,并利用多跳依赖性进行信息聚合。 本文发表在2021年CIKM会议上,作者学校:Vanderbilt University,引用量:59。 CIKM会议简介:全称C