【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 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出 这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂--我们也 ...
随机推荐
- 随手练——LintCode 433 - 小岛数量
LintCode 433: https://www.lintcode.com/problem/number-of-islands/description LintCode 434: https://w ...
- 【bbs】logout.php
字体大小通过js设定,并结合@media,可实现自适应. 图片自适应窗口 实现流水灯手机端不滚动,script嵌套 多余文字省略号显示 http://www.cnblogs.com/yujihang ...
- face++
1.链表反转 2.快排 3.m*k n*k两矩阵计算欧几里得距离np.tile 4.链表排序,要求时间复杂度小于O(N^2),空间O(1),不允许改变链表的值 5.2sum及其变体 6.给一个数组 ...
- vector详讲(一)
<vector>头文件里带有两个类型参数的类模板,第一个参数是需要指定的数据类型,第二个是分配器(allocator)类型 template<class T, class Alloc ...
- [转]MBTiles 1.2 规范翻译
MBTiles 1.2 可以参考超图的文档MBTiles扩展具体实现可以参考浅谈利用SQLite存储离散瓦片的思路和实现方法 mapbox提供了一个简单实现测试代码,github地址在这里https: ...
- JNI由浅入深_10_JNI 综合开发
1.使用ndk-build时如果找不到某个类,可以使用下面两种方法解决: 1.1 进入src目录 D:\project3\JNIAndroid\src>set classpath=D:\proj ...
- 二叉查找树(二叉排序树)(C语言)
#include<stdio.h> #include "fatal.h" struct TreeNode; typedef struct TreeNode *Posit ...
- G1 GC日志:Application time: 0.8766273 seconds
启动日志一直循环: 1.159: Application time: 0.8766273 seconds 1.160: Total time for which application threads ...
- Extjs6 组件浅谈
一.Component组件 一个 Ext JS 应用的界面由一个或多个叫做 组件的控件组成. 所有的组件都是 Ext.Component 类的子类,这个类可以允许参与包含实例化.渲染.自动大小和位置以 ...
- Mave实战(1)——Maven介绍
目录 1. Maven介绍 1.1. 何为Maven 1.1.1. Maven是优秀的构建工具 1.1.2. Maven不仅仅是构建工具 1. Maven介绍 1.1. 何为Maven Maven是一 ...