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 ...
随机推荐
- Chrome浏览器 js 关闭窗口失效解决方法
//获取元素ID var DelHtml = document.getElementById("imgdel"); //alert(DelHtml); //添加点击事件 DelHt ...
- vc++6.0中右键点击"转到定义"为什么是"未定义符号"呢?
VC的问题,需要生成一下浏览信息...然后rebuild
- POJ 2886 Who Gets the Most Candies?(树状数组+二分)
题目链接 注意题目中给的顺序是顺时针的,所以在数组中应该是倒着存的.左就是顺时针,右就是逆时针.各种调试之后,终于A了,很多种情况考虑情况. #include <cstring> #inc ...
- HDFS运维和优化
常见问题 下面列举HDFS运行过程中可能出现的常见问题及解决方法,这些问题一般都会在日志中出现的相应的记录.Incompatible clusterIDs in … :namenode cluster ...
- 序列流、对象操作流、打印流、标准输入输出流、随机访问流、数据输入输出流、Properties(二十二)
1.序列流 * 1.什么是序列流 * 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.* 2.使用方式 * 整合两个 ...
- CollectionView网格布局
说句老实话,UICollectionView真的太强大了,而且要掌握高级部分是相当困难的.至少笔者是这么认为的,如果觉得自己比较厉害,可以轻而易举地掌握UICollectionView的使用的,希望可 ...
- C++中volatile及编译器优化
首先看一下单词"volatile"的释义: volatile [ˈvɑlətl] adj. 易变的,不稳定的; (液体或油)易挥发的; 爆炸性的; 快活的,轻快的; 下边是&qu ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- CocoaPods 安装相关问题
(1)pod install还是pod update都卡在Analyzing dependencies不动. 解决方法: 其实原因在于以上两个命令执行时会升级CocoaPods的spec仓库,加一个参 ...
- 洛谷P2148 E&D——打表
题目:https://www.luogu.org/problemnew/show/P2148 先打表找个规律: #include<iostream> #include<cstdio& ...