湖南省第十二届大学生计算机程序设计竞赛 B 有向无环图 拓扑DP
1804: 有向无环图
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 187 Solved: 80
[Submit][Status][Web Board]
Description

Input
Output
Sample Input
3 3
1 1
1 1
1 1
1 2
1 3
2 3
2 2
1 0
0 2
1 2
1 2
2 1
500000000 0
0 500000000
1 2
Sample Output
4
4
250000014
题解:
quality:dp[u]表示从u出发能到达的所有v的count[v]*b[v]之和,那么u的一个后继v对dp[u]有dp[v]+b[v]的贡献,按照反向拓扑序dp
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e6+, mod = 1e9+, inf = ; int n,m;
LL a[N],b[N];
int d[N],t,head[N];
LL dp[N]; struct ss{int to,next;}e[N * ];
void add(int u,int v) {e[t].to=v;e[t].next = head[u];head[u]=t++;} int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
for(int i = ; i <= n; ++i) scanf("%I64d%I64d",&a[i],&b[i]);
memset(d,,sizeof(d));t = ;
memset(head,,sizeof(head));
for(int i = ; i <= m; ++i) {
int u,v;
scanf("%d%d",&u,&v);
add(v,u);
d[u] += ;
}
memset(dp,,sizeof(dp));
queue<int > q;
for(int i = ; i <= n; ++i) {
if(d[i] == ) {
q.push(i);
}
}
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
dp[to] += (dp[u] + b[u]);
dp[to] %= mod;
d[to]--;
if(d[to] == ) q.push(to);
}
} LL ans = ;
for(int i = ; i <= n; ++i) {
// cout<<dp[i]<<endl;
ans += ((a[i] * dp[i])%mod);
ans %= mod;
}
cout<<(ans+mod)% mod<<endl;
}
return ;
}
湖南省第十二届大学生计算机程序设计竞赛 B 有向无环图 拓扑DP的更多相关文章
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛Problem A 2016 找规律归类
Problem A: 2016 Time Limit: 5 Sec Memory Limit: 128 MB Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) ...
- 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路
1808: 地铁 Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i ...
- 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis
1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...
- 湖南省第十二届大学生计算机程序设计竞赛 A 2016
1803: 2016 Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. In ...
- 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...
- 【最短路】【数学】CSU 1806 Toll (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1806 题目大意: N个点M条有向边,给一个时间T(2≤n≤10,1≤m≤n(n-1), ...
- 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...
- 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...
随机推荐
- mysql性能优化学习笔记-存储引擎
mysql体系架构 客户端(java.php.python等) mysql服务层(连接管理器.查询解析器.查询优化器.查询缓存) mysql存储引擎(innodb.myisam等) 存储引擎针对表而言 ...
- 如何恢复低版本的FlashPlayer
本人做页游开发时,游戏用户那边经常会遇到一些很奇怪的问题.比如: 1.用户进入游戏,只显示游戏部分界面,chrome浏览器是正常的,就IE死活不行. 2.进入游戏时白屏或者一直加载不上. 3.玩游戏时 ...
- ios8 新增的 showViewController 和 showDetailViewController
1.showViewController 先看看说明: You use this method to decouple the need to display a view controller fr ...
- ios7 上 UIActivity 用的image
在ios8 上UIActivityCategoryShare类型的UIActivity的图标支持彩色图片了,但是在ios7上不行,ios8上的 UIActivityCategoryAction类型也不 ...
- ABAP ALV单个单元格状态编辑
*&---------------------------------------------------------------------* *& Report ZPPR0024 ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【python】datetime获取日期,前一天日期
1.获取字符串型当前日期 2016-10-09格式 import datetime today = datetime.date.today() #datetime.date类型当前日期 str_tod ...
- cocos2d-x 第二篇 HelloWorld的流程
这篇博客主要是带领大家一起了解整个游戏的执行过程,其中涉及的一些譬如导演,场景,层之类的概念将会在后面讲解. 看main函数的区别: #import <UIKit/UIKit.h> // ...
- UIDynamic动画
UIDynamic是从iOS7开始引入的技术 属于UIkit框架 可以模拟显示生活中的物理现象 如碰撞 抖动 摆动等 一.使用UIDynamic步骤: 1.创建一个动力效果器UIDynamicAnim ...
- google关于ssh key的解释(转)转的google的wiki的
SSH keys (简体中文) SSH 密钥对可以让您方便的登录到 SSH 服务器,而无需输入密码.由于您无需发送您的密码到网络中,SSH 密钥对被认为是更加安全的方式.再加上使用密码短语 ( ...