湖南省第十二届大学生计算机程序设计竞赛 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位翻转过来后的 ...
随机推荐
- Word Search I & II
Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 从其它系统登录到SharePoint 2010系统的单点登录
以前做的只是使用SharePoint的单一登录,用SharePoint去登录其他的系统,现在要反过来,用Form认证的系统来登录SharePoint. 我们都知道,SharePoint使用的是域认证系 ...
- selenium WebDriver 操作高德地图
String URL="http://www.amap.com/"; WebDriver driver = new FirefoxDriver(profile); driver.g ...
- Win8环境WPF打开和关闭软键盘
代码如下: public class KeyBoardHelper { #region 键盘控制 /// <summary> /// 显示键盘 /// </summary> p ...
- Match:Seek the Name, Seek the Fame(POJ 2752)
追名逐利 题目大意:给定一个字符串S,要你找到S的所有前缀后缀数组 还是Kmp的Next数组的简单应用,但是这一题有一个BUG,那就是必须输出字符串的长度(不输出就WA),然而事实上对于abcbab, ...
- 使用iScroll时,input等不能输入内容的解决方法(share)
最近做移动平台的应用,使用iscroll使屏幕上下滑动.发现当使用iscroll后,input等不能输入内容了.只要在iscroll.js文件中加入如下代码就ok了. function allowFo ...
- android中判断网络连接是否可用
一.判断网络连接是否可用 public static boolean isNetworkAvailable(Context context) { ConnectivityManager cm = (C ...
- IOS - 首次启动程序出现的画面介绍
1.在appdelegate.m中找到 “application:didFinishLaunchingWithOptions:” 通过NSUserDefaults 来进行判断 if (![[NSUse ...
- TCP/IP的Socket编程
1. TCP/IP.UDP的基本概念 TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,他是一个工业标准的协议集, ...
- NYOJ之喷水装置(一)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsUAAAJvCAIAAAAcLjvHAAAgAElEQVR4nO3drXLjzNaG4e8kzH0gof