1804: 有向无环图

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 187  Solved: 80
[Submit][Status][Web Board]

Description

Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始、点 v 结束的路径)。
为了方便,点用 1,2,…,n 编号。 设 count(x,y) 表示点 x 到点 y 不同的路径数量(规定 count(x,x)=0),Bobo 想知道
 
 
除以 (109+7) 的余数。
其中,ai,bj 是给定的数列。
 

Input

输入包含不超过 15 组数据。
每组数据的第一行包含两个整数 n,m (1≤n,m≤105).
接下来 n 行的第 i 行包含两个整数 ai,bi (0≤ai,bi≤109).
最后 m 行的第 i 行包含两个整数 ui,vi,代表一条从点 ui 到 vi 的边 (1≤ui,vi≤n)。
 

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的更多相关文章

  1. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  2. 2016年湖南省第十二届大学生计算机程序设计竞赛Problem A 2016 找规律归类

    Problem A: 2016 Time Limit: 5 Sec  Memory Limit: 128 MB Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) ...

  3. 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路

    1808: 地铁 Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i ...

  4. 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis

    1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...

  5. 湖南省第十二届大学生计算机程序设计竞赛 A 2016

    1803: 2016 Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量:   1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. In ...

  6. 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...

  7. 【最短路】【数学】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), ...

  8. 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...

  9. 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...

随机推荐

  1. 解读Unity中的CG编写Shader系列六(漫反射)

    转自 http://www.itnose.net/detail/6116553.html 如果前面几个系列文章的内容过于冗长缺乏趣味着实见谅,由于时间原因前面的混合部分还没有写完,等以后再补充,现在开 ...

  2. 64位win系统上面tomcat6启动不了 window不能再本地计算机启动

    64位的jdk装完之后,jre的bin目录下面没有client文件夹, 而tomcat6.0.20的默认配置启动在client文件夹下面. 所以打开tomcat6w,在java选项界面,取消Use d ...

  3. FastReport 使用说明

    FastReport TfrxReport 此为最主要的报表元件,一个 TfrxReport 元件组成一份报表.在设计时期,双击此 元件可打开报表设计器(Report Designer),此元件拥有所 ...

  4. 细看INNODB数据落盘

    本文来自:沃趣科技 http://www.woqutech.com/?p=1459 1.  概述 前面很多大侠都分享过MySQL的InnoDB存储引擎将数据刷新的各种情况.我们这篇文章从InnoDB往 ...

  5. cairo-1.14.6 static compiler msys mingw32

    gtk2.x 静态编译时 需要注意的是 cairo cairo 1.14.x 使用了 mutex , 用动态方式时 DllMain 中调用了 CAIRO_MUTEX_INITIALIZE () 在静态 ...

  6. Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()

    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...

  7. IOS - socket 编程初体验

    #import "ViewController.h" @interface ViewController () <NSStreamDelegate, UITextFieldD ...

  8. Java IO流题库

    一.    填空题 Java IO流可以分为   节点流   和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道,程序可以通过这个通道读取数 ...

  9. GridView中使用分页控件

    前台:导入<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix= ...

  10. windows系统查看80端口被占用的程序并结束该程序运行

    一.背景 最近系统更新以后,我在Idea中适用80端口启动项目的时候发现80端口被占用了,就查了资料看怎么找到占用80端口的程序并结束其运行,下面把解决方式共享给大家. 二.解决步骤 1.首先打开控制 ...