Chemistry in Berland CodeForces - 846E
题意:
有n种化学物质,第i种物质现有bi千克,需要ai千克。有n-1种,编号为2-n的转换方式,每种都为(x,k),第i行是编号为i+1的转换方式,编号为i的转换方式(xi,ki)表示ki千克的xi物质可以转换成1千克的i物质,1千克的i物质可以转换成1千克的xi物质。问是否可能通过转换得到足够的需要的物质。(1 ≤ xj + 1 ≤ j)
重点:上面标红的条件。如果只保留ki千克的xi物质可以转换成1千克的i物质产生的一条有向边,表明xi物质连出的边一定是指向编号大于i的物质的(x[i+1]<=i,x[i]<=i-1,x[i]<i),而又恰好有n-1条边,也就是这种情况下这是一棵树。
方法:建树,dfs自底向上递推(我用的方法是只保留ki千克的xi物质可以转换成1千克的i物质产生的一条xi->i的有向边),如果某样物质不够就从父结点那儿转换,如果某样物质多了就把多的转换成父结点的物质。(如果父结点的物质不够,直接减就行,减成负数也没关系)
奇怪的地方:貌似这道题极限数据会爆longlong,然后直接在爆longlong的时候判为NO就行?

#include<cstdio>
#include<cstdlib>
#define inf 110000000000000000
typedef long long LL;
struct Edge
{
LL to,dis,next;
}edge[];
LL n,num_edge;
LL first1[];
LL a[],b[];
double tem;
void dfs(LL x,LL fa,LL p)
{
LL k=first1[x];
while(k!=)
{
dfs(edge[k].to,x,edge[k].dis);
k=edge[k].next;
}
// if(a[x]<b[x])
// b[fa]+=b[x]-a[x];
// else if(a[x]>b[x])
// b[fa]-=p*(a[x]-b[x]);
if(a[x]<b[x])
b[fa]+=b[x]-a[x];
else if(a[x]>b[x])
{
tem=(double)(b[x]-a[x])*p;//为何要double?
if(tem<-inf)
{
printf("NO");
exit();
}
b[fa]-=p*(a[x]-b[x]);
if(b[fa]<-inf)
{
printf("NO");
exit();
}
}
}
int main()
{
LL i,x,k;
scanf("%lld",&n);
for(i=;i<=n;i++)
scanf("%lld",&b[i]);
for(i=;i<=n;i++)
scanf("%lld",&a[i]);
for(i=;i<=n;i++)
{
scanf("%lld%lld",&x,&k);
edge[++num_edge].to=i;
edge[num_edge].dis=k;
edge[num_edge].next=first1[x];
first1[x]=num_edge;
}
k=first1[];
while(k!=)
{
dfs(edge[k].to,,edge[k].dis);
k=edge[k].next;
}
if(b[]<a[])
printf("NO");
else
printf("YES");
return ;
}
Chemistry in Berland CodeForces - 846E的更多相关文章
- Day4 - M - Roads in Berland CodeForces - 25C
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Eac ...
- [CF846E]Chemistry in Berland题解
这题乍一看是一道水树形DP(其实事实上它确实是树形DP),然后设f[i]表示第i个点所多余/需要的材料,然后我们愉快的列出了式子: if(f[v]<0) f[u] += f[v] * edges ...
- 【Educational Codeforces Round28】
咸鱼选手发现自己很久不做cf了,晚节不保. A.Curriculum Vitae 枚举一下间断点的位置. #include<bits/stdc++.h> using namespace s ...
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library set
B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- CodeForces 445B DZY Loves Chemistry
DZY Loves Chemistry Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力
C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library 模拟
B. Berland National LibraryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
随机推荐
- ViewFlow增强onItemClick功能及ViewFlow AbsListView源代码分析
先看实现效果,上图: ViewFlow是一个非常好用的,用于不确定item个数的水平滑动切换的开源项目. 可是从github上下载的ViewFlow事实上是不支持onItemClick功能的,tou ...
- Cookie 和 Session 的区别 与联系
一. 概念理解 你可能有留意到当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为 你浏览某个网页的时候,W ...
- Session 钝化机制
- bash_action
https://stackoverflow.com/questions/12076326/how-to-install-maven2-on-redhat-linux #!/bin/bash # Tar ...
- 解析腾讯企业邮箱到自己域名,设置mail的cname
之前注册了腾讯企业邮的免费邮箱,后来想把企业邮箱和域名绑定起来,发现了一些问题. 先来看正常的部分,假设你已经注册过了腾讯企业邮箱免费版,并且已经绑定好了域名. 然后在域名提供商那里设置域名解析的MX ...
- android 中的常用组件
gradle gradle 是个啥,一开始我也没弄清,官方解释是: Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化建构工具 那么Apache Ant和Apache ...
- kbmMW实现sql查询(图文并茂)
kbmMW对于Delphi来说,是最好的多层框架,没有之一,无论是效率.稳定及架构都让人无可挑剔,尤其自Delphi支持跨平台开发以来,随着Delphi支持ios及Android移动开发,KbmMW也 ...
- model.js
var Model = { inherited: function () {}, created: function () {}, prototype: { init: function (attrs ...
- window.name应用于浏览器端数据存储
本代码简单地分享利用window.name实现浏览器端数据存储: 1.在同一个页面一个地方设置window.name = "abc",另外一个地方读取window.name,自然能 ...
- POJ1474:Video Surveillance(求多边形的核)(占位)
A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- me ...