cf711D. Directed Roads(环)
题意
\(n\)个点\(n\)条边的图,有多少种方法给边定向后没有环
Sol
一开始傻了,以为只有一个环。。。实际上N个点N条边还可能是基环树森林。。
做法挺显然的:找出所有的环,设第\(i\)个环的大小为\(w_i\)
\(ans = 2^{N - \sum w_i} \prod (2^{w_i} - 2)\)
最后减掉的2是形成环的情况
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9, PI = acos(-1);
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = (x * 10 + c - '0') % mod, c = getchar();
return x * f;
}
int N, dep[MAXN], w[MAXN], top, po2[MAXN], vis[MAXN];
vector<int> v[MAXN];
void dfs(int x, int d) {
dep[x] = d; vis[x] = 1;
for(auto &to : v[x]) {
if(!vis[to])dfs(to, d + 1);
else if(vis[to] == 1) w[++top] = dep[x] - dep[to] + 1;
}
vis[x] = 2;
}
signed main() {
N = read();
po2[0] = 1;
for(int i = 1; i <= N; i++) po2[i] = mul(2, po2[i - 1]);
for(int i = 1; i <= N; i++) {
int x = read();
v[i].push_back(x);
}
for(int i = 1; i <= N; i++)
if(!dep[i])
dfs(i, 1);
int sum = 0, res = 1;
for(int i = 1; i <= top; i++) sum += w[i], res = mul(res, po2[w[i]] - 2 + mod);
printf("%d\n", mul(po2[N - sum], res));
return 0;
}
cf711D. Directed Roads(环)的更多相关文章
- Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量
D. Directed Roads ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...
- Codeforces #369 div2 D.Directed Roads
D. Directed Roads time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...
- CodeForces #369 div2 D Directed Roads DFS
题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Code Forces 711D Directed Roads
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Directed Roads
Directed Roads 题目链接:http://codeforces.com/contest/711/problem/D dfs 刚开始的时候想歪了,以为同一个连通区域会有多个环,实际上每个点的 ...
- Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 711D Directed Roads - 组合数学
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...
随机推荐
- 【Java初探01】——Java简介及相关
Java 简介 java 是一种高级的面向对象的程序设计语言,使用Java语言编写的程序时跨平台的.从pc到手机,都有Java开发的程序和游戏,Java程序可以在任何计算机,操作系统和支持的Java的 ...
- 小记 ArchLinux 下 Typora 无法输入中文
今天准备写一篇 Linux 下的打印机文章,打开 Typora 时我发现不管我怎么设置都无法输入中文. pacman -R typora pacman -S typora 重新安装是无效的,我突然想起 ...
- Spring 全局异常处理
[参考文章]:Spring全局异常处理的三种方式 [参考文章]:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 [参考文章]:@ControllerAdvic ...
- DockerFile(保你会版本)(七)
一.什么是dockerfile Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile自定义快速创建属于自己的镜像,Dockerfile是通过很多的参数指令编写的文件,通过do ...
- Linux下删除某些非法字符文件名的文件
1.首先利用 ls -i 查找ID 2.find ./ -inum 20718697 -exec rm '{}' \;
- 世界上最短的bash脚本
世界上最短的bash脚本长这样: #!/bin/bash 为啥呢?见下图: 推荐一篇文章,讲解为啥shell脚本开头总是"#!/bin/bash".文风太清奇,不好翻译,看原文吧: ...
- Spring Boot 解决方案 - JUnit 测试
简单的 JUnit 项目 回顾一下创建并运行简单的 JUnit 测试项目,先添加 JUnit 依赖然后编写类似如下模板的测试类,使用 IDE 的话直接用插件运行就行, 使用 Maven 的话运行命令 ...
- spring mvc 404页面制作
1.404页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- ActiveMQ——activemq的使用java代码实例(精选)
ActiveMQ 在java中的使用,通过单例模式.工厂实现 Jms规范里的两种message传输方式Topic和Queue,两者的对比如下表(): Topic Queue 概要 Publish ...
- 自己实现IOC过程
阅读了<架构探险>这本书之后简单梳理一下自己实现IOC的基本流程 首先要自己定义一个注解 import java.lang.annotation.ElementType; import j ...