2019 China Collegiate Programming Contest Qinhuangdao Onsite F. Forest Program(DFS计算图中所有环的长度)
题目链接:https://codeforces.com/gym/102361/problem/F
题意
有 \(n\) 个点和 \(m\) 条边,每条边属于 \(0\) 或 \(1\) 个环,问去掉一些边使得图变为森林的方案个数。
题解
找出所有环的长度 \(c_i\),每个环可以去掉 \(1,2,\dots,c_i\) 条边,方案各为 \(C_{c_i}^1,C_{c_i}^2, \dots C_{c_i}^{c_i}\),即 \(2^{c_i} - 1\) 。
所有环的去边方案共有 \(\prod \limits _{i = 1}^{c.size} 2^{c_i} - 1\) 。
余下的边为 \(m - \sum \limits _{i=1}^{c.size} c_i\),可以去掉 \(0,1,2,\dots,m - \sum \limits _{i=1}^{c.size} c_i\) 条边,方案总数为 \(2^{m - \sum \limits _{i=1}^{c.size} c_i}\) 。
所以总的方案个数即为:\({2^{m - \sum \limits _{i=1}^{c.size} c_i}} \times ({\prod \limits _{i = 1}^{c.size} 2^{c_i} - 1})\) 。
下面就是如何计算所有环的长度,利用 \(dfs\) 记录每个点的深度:
- 如果下一个点深度小于当前点且访问过,那么二者深度之差加一即为所在环的长度。
- 如果下一个点深度大于等于当前点,则该点之前已访问过且环的长度也已经计算过,可以跳过。
Tips
图可能不连通,所以需要对每个未访问过的点都进行 \(dfs\) 。
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 998244353;
int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> G(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
G[u].push_back(v);
G[v].push_back(u);
}
vector<int> dep(n), cycle;
function<void(int, int)> dfs = [&](int u, int p) {
if (dep[u] != 0) {
if (dep[u] < dep[p])
cycle.push_back(dep[p] - dep[u] + 1);
} else {
dep[u] = (p == -1 ? 0 : dep[p]) + 1;
for (auto v : G[u]) {
if (v != p) {
dfs(v, u);
}
}
}
};
for (int i = 0; i < n; i++) {
if (dep[i] == 0) {
dfs(i, -1);
}
}
long long ans = binpow(2, m - accumulate(cycle.begin(), cycle.end(), 0));
for (auto i : cycle) {
ans *= binpow(2, i) - 1;
ans %= MOD;
}
cout << ans << "\n";
return 0;
}
2019 China Collegiate Programming Contest Qinhuangdao Onsite F. Forest Program(DFS计算图中所有环的长度)的更多相关文章
- 2019 China Collegiate Programming Contest Qinhuangdao Onsite
传送门 D - Decimal 题意: 询问\(\frac{1}{n}\)是否为有限小数. 思路: 拆质因子,看是不是只包含2和5即可,否则除不尽. Code #include <bits/st ...
- The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners
链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word ...
- The 2019 China Collegiate Programming Contest Harbin Site
题解: https://files.cnblogs.com/files/clrs97/HarbinEditorialV2.zip Code: A. Artful Paintings /* let x= ...
- The 2019 China Collegiate Programming Contest Harbin Site I. Interesting Permutation
链接: https://codeforces.com/gym/102394/problem/I 题意: DreamGrid has an interesting permutation of 1,2, ...
- 模拟赛小结:The 2019 China Collegiate Programming Contest Harbin Site
比赛链接:传送门 上半场5题,下半场疯狂挂机,然后又是差一题金,万年银首也太难受了. (每次银首都会想起前队友的灵魂拷问:你们队练习的时候进金区的次数多不多啊?) Problem J. Justify ...
- The 2019 China Collegiate Programming Contest Harbin Site K. Keeping Rabbits
链接: https://codeforces.com/gym/102394/problem/K 题意: DreamGrid is the keeper of n rabbits. Initially, ...
- The 2019 China Collegiate Programming Contest Harbin Site J. Justifying the Conjecture
链接: https://codeforces.com/gym/102394/problem/J 题意: The great mathematician DreamGrid proposes a con ...
- The 2015 China Collegiate Programming Contest A. Secrete Master Plan hdu5540
Secrete Master Plan Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- The 2015 China Collegiate Programming Contest Game Rooms
Game Rooms Time Limit: 4000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
随机推荐
- #3使用html+css+js制作网页 番外篇 使用python flask 框架 (I)
#3使用html+css+js制作网页 番外篇 使用python flask 框架(I 第一部) 0. 本系列教程 1. 准备 a.python b. flask c. flask 环境安装 d. f ...
- Docker PHP 扩展配置
# PHP 容器配置 # 从官方基础版本构建 FROM php:7.2-fpm # 官方版本默认安装扩展: # Core, ctype, curl # date, dom # fileinfo, fi ...
- Eslint提示const关键字被保留
如果在使用eslint的时候提示: error Parsing error: The keyword 'const' is reserved 有可能是因为eslint默认审查的es5,需要明确让他审查 ...
- Spring Cloud实战 | 第十篇 :Spring Cloud + Seata 1.4.1 + Nacos1.4.0 整合实现微服务架构中逃不掉的话题分布式事务
Seata分布式事务在线体验地址:https://www.youlai.store 本篇完整源码地址:https://github.com/hxrui/youlai-mall 有想加入开源项目开发的童 ...
- 【SpringBoot1.x】SpringBoot1.x 分布式
SpringBoot1.x 分布式 分布式应用 Zookeeper&Dubbo ZooKeeper 是用于分布式应用程序的高性能协调服务.它在一个简单的界面中公开了常见的服务,例如命名,配置管 ...
- Invalid bound statement (not found): com.xxx.xxx.dao.ShopMapper.insertShop
mybatis在编写完SQL,进行测试的时候出现了错误,显示 org.apache.ibatis.binding.BindingException: Invalid bound statement ( ...
- Thread线程源码解析,Java线程的状态,线程之间的通信
线程的基本概念 什么是线程 现代操作系统在运行一个程序的时候,会为其创建一个进程.例如,启动一个Java程序,操作系统就会创建一个Java进程.线代操作系统调度的最小单位是线程.也叫做轻量级进程.在一 ...
- 同一个网段内所有服务器virtual_router_id设置相同的后果
/var/log/messages中一直报的错 one or more VIP associated with VRID mismatch actual MASTER advert bogus VRR ...
- P2024 [NOI2001]食物链(种类并查集)
题目链接: https://www.luogu.org/problemnew/show/P2024 题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 ...
- 三十二:WEB漏洞-文件操作之文件下载读取全解
文件下载读取 原路,检测,利用,修复 利用 数据库配置文件下载或者读取后续 接口密钥信息文件下载或者读取后续 文件名,参数值,目录符号 read.xxx?filename= down.xxx?file ...