【LG3244】[HNOI2015]落忆枫音
题面
题解
20pts
枚举每一条边是否在树中即可。
另10pts
我们考虑一张\(DAG\)中构成树的方法数,每个点选一个父亲即可,那么有
\]
\(deg_i\)表示点\(i\)的入度,其中\(deg_1=1\)。
\(100pts\)
考虑在上面的基础上容斥,
考虑连\(y\rightarrow x\)后出现一个环的情况数,其实就是环上的点固定了父亲,
那么最后答案就是
\]
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int Mod = 1e9 + 7;
const int MAX_N = 1e5 + 5, MAX_M = 2e5 + 5;
int fpow(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res = 1ll * res * x % Mod;
x = 1ll * x * x % Mod;
y >>= 1;
}
return res;
}
struct Graph { int to, next; } e[MAX_M]; int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; }
int N, M, sx, sy, deg[MAX_N], f[MAX_N];
bool vis[MAX_N];
void dfs(int x) {
if (vis[x]) return ; vis[x] = 1;
if (sx == x) return (void)(f[x] = 1ll * fpow(deg[x], Mod - 2));
for (int i = fir[x]; ~i; i = e[i].next) dfs(e[i].to), f[x] = (f[x] + f[e[i].to]) % Mod;
f[x] = 1ll * f[x] * fpow(deg[x], Mod - 2) % Mod;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
clearGraph();
N = gi(), M = gi(), sx = gi(), sy = gi();
for (int u, v, i = 1; i <= M; i++) u = gi(), v = gi(), Add_Edge(u, v), ++deg[v];
++deg[1];
int ans = 1, res = 1;
for (int i = 1; i <= N; i++) {
if (i == sy) ans = 1ll * ans * (deg[i] + 1) % Mod;
else ans = 1ll * ans * deg[i] % Mod;
res = 1ll * res * deg[i] % Mod;
}
dfs(sy); ans = (ans - 1ll * res * f[sy] % Mod + Mod) % Mod;
printf("%d\n", ans);
return 0;
}
【LG3244】[HNOI2015]落忆枫音的更多相关文章
- BZOJ 4011: [HNOI2015]落忆枫音( dp )
DAG上有个环, 先按DAG计数(所有节点入度的乘积), 然后再减去按拓扑序dp求出的不合法方案数(形成环的方案数). ---------------------------------------- ...
- bzoj4011[HNOI2015]落忆枫音 dp+容斥(?)
4011: [HNOI2015]落忆枫音 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1125 Solved: 603[Submit][Statu ...
- [HNOI2015]落忆枫音 解题报告
[HNOI2015]落忆枫音 设每个点入度是\(d_i\),如果不加边,答案是 \[ \prod_{i=2}^nd_i \] 意思是我们给每个点选一个父亲 然后我们加了一条边,最后如果还这么统计,那么 ...
- 4011: [HNOI2015]落忆枫音
4011: [HNOI2015]落忆枫音 链接 分析: 原来是一个DAG,考虑如何构造树形图,显然可以给每个点找一个父节点,所以树形图的个数就是$\prod\limits_u deg[u]$. 那么加 ...
- BZOJ4011: [HNOI2015]落忆枫音
Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出 这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂……我们 ...
- [HNOI2015]落忆枫音
题目描述 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂......我们也不可能再 ...
- BZOJ4011:[HNOI2015]落忆枫音(DP,拓扑排序)
Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂……我们也 ...
- BZOJ 4011: [HNOI2015]落忆枫音 计数 + 拓扑排序
Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出 这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂……我们 ...
- [bzoj4011] [洛谷P3244] [HNOI2015] 落忆枫音
Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出 这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂--我们也 ...
随机推荐
- [POI2015]LOG
题目 发现询问是针对整个区间,也就是说位置什么用都没有 发现我们需要构造出\(s\)个长度为\(c\)的数列,每个数只能在一个数列中出现一次,且一个数最多的使用次数是其大小 对于那些大于等于\(s\) ...
- hctf2016_302跳转绕csp---总结
页面目录如下: register.php注册页面. user.php可发送消息给其他用户. profile.php可配置参数添加用户头像(加载eval js文件). static存在redirect. ...
- Mysql 创建普通用户、数据库、表、插入记录,用户赋权
C:\phpStudy\MySQL\bin>mysql -uroot -proot -h127.0.0.1 //创建用户 mysql> insert into mysql.user (ho ...
- MAC 下 安装redis 并配置 php redis 扩展
下载 redis redis-3.1.2.tgz sudo tar -xzf redis-3.1.2.tgz cd redis-3.1.2 sudo phpize grep: /usr/include ...
- 搭建elk的坑
npm install 时总是报phantomjs-prebuilt@2.1.16安装失败: npm cache clean --force npm -g install phantomjs-pre ...
- Spring@PostConstruct注解和构造方法的调用顺序
先看下@PostConstruct的注解 * The PostConstruct annotation is used on a method that needs to be executed * ...
- SDN测量论文粗读(三)9.24
Jaal: Towards Network Intrusion Detection at ISP Scale 论文来源:CoNext 发表时间:2015 解决问题及所做贡献:Jaal:大规模精细网络入 ...
- Oracle 执行计划的查看方式
访问数据的方法:一.访问表的方法:1.全表扫描,2.ROWID扫描 二.访问索引的方法:1.索引唯一性扫描,2.索引范围扫描,3.索引全扫 ...
- HTML5纯Web前端也能开发直播,不用开发服务器(使用face2face)
前段时间转载了某位大神的一篇文章,开发Web版一对一远程直播教室只需30分钟 - 使用face2face网络教室.非常有意思.看起来非常简单,但作为一名前端开发人员来说,还是有难度.因为要开发服务器端 ...
- windows7平台 nginx+python 环境搭建
参考了这篇文章,感谢原文作者:https://blog.csdn.net/foxgod/article/details/78929201 最近正在学习Python,发现除了写一点py脚本在idlex上 ...