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

Input

输入包含不超过 15 组数据。
每组数据的第一行包含两个整数 n,m (1≤n,m≤10 5).
接下来 n 行的第 i 行包含两个整数 a i,b i (0≤a i,b i≤10 9).
最后 m 行的第 i 行包含两个整数 u i,v i,代表一条从点 u i 到 v i 的边 (1≤u i,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

Hint

思路:

由有向图拓扑序的性质可以知道,拓扑序在后的节点是没有指向拓扑序在前的节点。

那么我们在对整个图进行拓扑的时候,把起始点 x 的a[x] 值 附加到 这个边的终点 y 的a[y] 值上。

并且对于每一个边,我们维护一个 long long 类型的 答案 ans,ans+= a[x]*b[y]  (  边 是 x ~> y )

那么这里就介绍刚刚我们为什么要附加数值a[x] 到a[y]上。

如果由三个点  x y z ,由如下的连接关系 x ~> y ~> z 那么x到y的边我们会算一次对答案的贡献值,y到z的边我们也会算一次。

还有一个x到z的边,我们仍然需要算。因为x可以到达z,那么如果我们在拓扑的时候把a[y]加上a[x] 时, 我们在算 b到z 的边的时候就顺便的加上了a到z的边。

因为x 的拓扑优先级比y高,并且x可以到达y,那么x就可以到达y能到达的所有边,那么就解释了这样做的原因。

这样做的时间复杂度就会转化为 O ( N )

主要是理解后就很好写代码,可以自己动手画图理解一下上面 讲的内容。

实现细节见ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
std::vector<int> son[maxn];
const ll mod=1e9+7ll;
ll ans=0ll;
ll a[maxn];
ll b[maxn];
ll num[maxn];
int du[maxn];
int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
int n,m;
while(cin>>n>>m)
{
repd(i,,n)
{
son[i].clear();
}
MS0(du);
MS0(num);
repd(i,,n)
{
cin>>a[i]>>b[i];
}
int x,y;
repd(i,,m)
{
cin>>x>>y;
son[x].push_back(y);
du[y]++;
}
queue<int> q;
repd(i,,n)
{
if(!du[i])
{
q.push(i);
}
} ans=0ll;
while(q.size())
{
int temp=q.front();
q.pop();
for(auto x:son[temp])
{
ans=(ans+(a[temp]*b[x])%mod+mod)%mod;
a[x]+=a[temp];
a[x]=(a[x]+mod)%mod;
du[x]--;
if(!du[x])
{
q.push(x);
}
}
}
cout<<ans<<endl; } return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题的更多相关文章

  1. 第十二届湖南省赛G - Parenthesis (树状数组维护)

    Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...

  2. 第十二届湖南省赛 A - 2016 ( 数学,同余转换)

    给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量:       1. 1≤a≤n,1≤b≤m;   2. a×b 是 2016 的倍数.   Input   输入包含不超过 30 ...

  3. 湖南省第十二届大学生计算机程序设计竞赛 B 有向无环图 拓扑DP

    1804: 有向无环图 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 187  Solved: 80[Submit][Status][Web Board ...

  4. 湖南省第十二届省赛:Parenthesis

    Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions. The i-t ...

  5. HZNU第十二届校赛赛后补题

    愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...

  6. CSU 1511 残缺的棋盘 第十届湖南省赛题

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 题目大意:在一个8*8的棋盘中,给你一个起点位置和一个终点位置,同时也给你一个陷阱 ...

  7. CSU 1507 超大型LED显示屏 第十届湖南省赛题

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1507 解题思路:这是一道模拟题,看了那么多人的代码,我觉得我的代码是最简的,哈哈,其实就 ...

  8. 【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804 题目大意: 一个有向无环图(DAG),有N个点M条有向边(N,M<=105 ...

  9. Little Sub and Traveling(杭师大第十二届校赛E题) 欧拉回路

    题目传送门 题目大意: 从0出发,每次只能跳到(i*2)%n或者(i*2+1)%n,求字典序最大的哈密顿回路. 思路: 首先n为奇数时无解,先来证明这一点. 先假设n为奇数,若要回到原点,则必定有一步 ...

随机推荐

  1. 回顾:Linux环境 Mysql新建用户和数据库并授权

    回顾:Linux环境 Mysql新建用户和数据库并授权 一.新建用户 //登录Mysql @>mysql -u root -p @>密码 //创建用户 mysql> insert i ...

  2. mysql 数据库 命令行的操作——对表和字段的操作

    一.对表的操作 1.查看所有表 show tables: 2.创建表 create table 表名(字段1 类型1 约束1 ,字段2 类型2 约束2): 3.修改表的名字 rename table ...

  3. 查看iPhone电池寿命

    iBackupBot 软件:iBackupBot for iTunes (收费软件) 官网:http://www.icopybot.com/download.htm iBackupBot for iT ...

  4. 使用administrator身份启动Vs2017

    日常开发中有些项目工程需要按照Administrator 身份进行启动,我们的操作是在vs2017 上右键,administrator 身份启动. 如下图: 但是这样每次都要右键,移动鼠标进行点击. ...

  5. 怎么将后缀为.opt,.frm,.myd,.myi文件还原或者是导入mySQL中

    其实这个问题的解决方案很简单,把这些文件连同这些文件所在的文件夹原封不动地复制到你的 mysql 文件夹下的 data 里面 (在我的电脑里面是D:\xampp\mysql\data), 然后你进my ...

  6. qt designer设置界面是label中文字与文本框对齐设置

    往往在使用 qt designer布置界面时,添加的label和文本框中是直接从工具箱中拖进去的,由于每个控件尺寸大小不一,就会造成label中的文字相对于文本框比较较偏上,看下面未经调整的直接效果 ...

  7. 【转】win2008 中iis7设置404页面但返回状态200的问题解决办法

    今天根据SEO反馈,某个站点中设置的404页面返回的http状态为200.通过站长工具进行查询,发现返回的状态确实为200. 通过彻查问题,发现这个网站的服务器环境为windows2008 服务器为i ...

  8. 使用Eclipse打jar包 包含依赖jar包

    1.在项目根目录新建MANIFEST.MF文件 //版本号 Manifest-Version: 1.0 //依赖jar包路径 多个用空格隔开 Class-Path: lib/commons-loggi ...

  9. 设置ansible与windows连通性

    1.确认powershell的版本,必须是3.0以上 $PSVersionTable.PSVersion 2.确认winrm是否开启命令:winrm quickconfig 3.在windows配置w ...

  10. vue数据绑定数组,改变元素时不更新view问题

    关于这个问题,官网上说的很清楚官方文档  写个例子HTML<body> <div class="box"> <div v-for="aa i ...