题目链接【https://vjudge.net/problem/CSU-1804】

题意: 给出一个有向无环图,然后让你算下面的结果,count(i,j)表示i->j之间的路径条数。

题解: 根据公式,可以把SUMa[i]提出来,然后对于没给我i点求SUMcount(i,j)*bj;

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 15;
int N, M;
LL A[maxn], B[maxn], dp[maxn];
int vis[maxn];
struct Edge
{
int to, next;
Edge (int to = 0, int next = 0): to(to), next(next) {}
} E[maxn];
int head[maxn], tot;
void initedge()
{
for(int i = 0; i <= N; i++) head[i] = dp[i] = -1, vis[i] = 0;
tot = 0;
}
void addedge(int u, int v)
{
E[tot] = Edge(v, head[u]);
head[u] = tot++;
}
LL DFS(int u)
{
if(dp[u] != -1) return dp[u];
vis[u] = 1;
dp[u] = 0;
for(int k = head[u]; ~k; k = E[k].next)
{
int v = E[k].to;
dp[u] = (dp[u] + (B[v] + DFS(v)) % mod) % mod;
}
return dp[u];
}
int main ()
{
while(~scanf("%d %d", &N, &M))
{
for(int i = 1; i <= N; i++)
scanf("%lld %lld", &A[i], &B[i]);
initedge();
for(int i = 1; i <= M; i++)
{
int u, v;
scanf("%d %d", &u, &v);
addedge(u, v);
}
for(int i = 1; i <= N; i++)
if(!vis[i]) DFS(i);
LL ans = 0;
for(int i = 1; i <= N; i++)
ans = (ans + (dp[i] * A[i]) % mod) % mod;
printf("%lld\n", ans);
}
return 0;
}

2016 湖南省省赛B题《有向无环图》的更多相关文章

  1. 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题

    Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...

  2. 网络流24题 第三题 - CodeVS1904 洛谷2764 最小路径覆盖问题 有向无环图最小路径覆盖 最大流 二分图匹配 匈牙利算法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - CodeVS1904 题目传送门 - 洛谷2764 题意概括 给出一个有向无环图,现在请你求一些路径,这些路径 ...

  3. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  4. 2016 湖南省省赛 Problem A: 2016

    Problem A: 2016 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 296  Solved: 171 Description  给出正整数 n ...

  5. ZOJ 3940 Modulo Query (2016年浙江省赛E题,区间折叠 + map运用)

    题目链接  2016 ZJCPC Problem E 考虑一个开区间$[0, x)$对$a_{i}$取模的过程. $[0, x)$中小于$a_{i}$的部分不变,大于等于$a_{i}$的部分被切下来变 ...

  6. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)

    题目链接  2016 Qingdao Online Problem I 题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...

  7. 2016湖大校赛 L题 The Sequence likes Ladder

    题意:S1=a,Sn=a*(Sn-1)^k%m,且有(a,m)=1,给出i,求Si. 思路:首先我们可以写出Sn的通项a^(1+k+k^2+...k^n-1);其次注意到m的范围是10000以内,所以 ...

  8. 2016年省赛G题, Parenthesis

    Problem G: Parenthesis Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 398  Solved: 75[Submit][Status ...

  9. 2016广东工业大学校赛 E题 GDUT-oj1173

    Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...

随机推荐

  1. 将oh-my-zsh编程真正的my zsh

    环境: Ubuntu 32位 oh-my-zsh安装: 1.安装zsh: sudo apt-get install zsh 2.将当前用户的shell环境修改为zsh:  chsh -s /bin/z ...

  2. 【BZOJ】4596: [Shoi2016]黑暗前的幻想乡

    [题意]给定n个点的无向完全图,有n-1个公司各自分管一部分路,要求所有公司都有修路的生成树数.n<=17. [算法]容斥原理+生成树计数(矩阵树定理) [题解]每个生成树方案是一个公司有无修路 ...

  3. centOS7 vsftp ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) 启动失败问题?

    [root@localhost c]# systemctl status vsftpd.service ● vsftpd.service - Vsftpd ftp daemon Loaded: loa ...

  4. CodeForces 869B

    Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...

  5. vue--------脚手架vue-cli搭建

    今天在看公司的项目的时候,用到的是Vue框架,哈哈,Vue已经火好久了,想必大家也晓得哈,这里宝宝就不瞎渣渣了~ 由于宝宝已经三个月木有看过代码了,所以对新公司的很多的架构和代码都是懵逼的,再加上宝宝 ...

  6. 无key值的json数组解析

    [    [        {            "cartId": 9223,            "factoryId": 143,          ...

  7. Mysql储存过程3:if语句

    --if/else语句 if 条件 then SQL语句 else SQL语句elseifSQL语句 end if; create procedure test1( number int ) begi ...

  8. 南邮PHP反序列化

    题目如下: <?php class just4fun { var $enter; var $secret; } if (isset($_GET['pass'])) { $pass = $_GET ...

  9. MGR Switch single-Primary to Muti_primary

    MGR single_primary 切换 Muti-Primary 模式 root@localhost [(none)]>select * from performance_schema.re ...

  10. java中String的==和equals的区别

    首先看代码1: public static void main(String[] args) { List<String> list=new ArrayList<String> ...