Codeforces 711 D. Directed Roads (DFS判环)

题目链接:http://codeforces.com/problemset/problem/711/D

给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环。

每个连通量必然有一个环,dfs的时候算出连通量中点的个数y,算出连通量的环中点的个数x,所以这个连通量不成环的答案是2^(y - x) * (2^x - 2)。

最后每个连通量的答案相乘即可。

 1 //#pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef __int64 LL;
15 typedef pair <int, int> P;
16 const int N = 2e5 + 5;
17 struct Edge {
18     int next, to;
19 }edge[N << 1];
20 LL mod = 1e9 + 7, d[N], ans, f[N], sum;
21 int head[N], cnt;
22 bool vis[N];
23
24 LL fpow(LL a, LL b) {
25     LL res = 1;
26     while(b) {
27         if(b & 1)
28             res = res * a % mod;
29         a = a * a % mod;
30         b >>= 1;
31     }
32     return res;
33 }
34
35 inline void add(int u, int v) {
36     edge[cnt].next = head[u];
37     edge[cnt].to = v;
38     head[u] = cnt++;
39 }
40
41 void dfs(int u, int p, int dep) {
42     if(!ans && vis[u]) {
43         ans = dep - d[u] ? dep - d[u] : 2;
44         return ;
45     } else if(vis[u]) {
46         return ;
47     }
48     d[u] = dep;
49     vis[u] = true;
50     ++sum;
51     for(int i = head[u]; ~i; i = edge[i].next) {
52         int v = edge[i].to;
53         if(v == p)
54             continue;
55         dfs(v, u, dep + 1);
56     }
57 }
58
59 int main()
60 {
61     f[0] = 1;
62     for(int i = 1; i < N; ++i) {
63         f[i] = f[i - 1]*2LL % mod; //2的阶乘预处理
64     }
65     memset(head, -1, sizeof(head));
66     cnt = 0;
67     int n, u;
68     scanf("%d", &n);
69     for(int i = 1; i <= n; ++i) {
70         scanf("%d", &u);
71         add(u, i);
72         add(i, u);
73     }
74     LL res = 1;
75     for(int i = 1; i <= n; ++i) {
76         if(!vis[i]) {
77             ans = sum = 0;
78             dfs(i, -1, 1);
79             res = ((res * (f[ans] - 2) % mod + mod) % mod * f[sum - ans]) % mod;
80         }
81     }
82     printf("%lld\n", res);
83     return 0;
84 }

时间: 2024-11-06 02:51:46

Codeforces 711 D. Directed Roads (DFS判环)的相关文章

CodeForces 711D Directed Roads (DFS判环+计数)

题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如果有环呢?假设x是这个连通块的边数, y是这个环的边数,那么就一共有2^x * (2 ^ y - 2) 种,减去这两种就是一边也不变,和所有的边都就变,这样就形成环了. 那么怎么计算呢?这个题的边很特殊,所以我们可以利用这个特征,我们在每个连通块时,用vis记录一下开始的父结点,用cnt记录每一个到

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. They realize that it consists of n towns numbered from 1to n. There are n directed roads in the Udayland. i-th of them goes from town i to some other town 

CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS

题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个解,否则判断是否能平局 题解: 看到这个题首先我想到了强连通分量,但是事实证明求出强连通分量,缩点对解决问题没有什么帮助.... 能写一些看似正确的算法,但其实是假算法来的.. ........... 所以应该先分析策略,肯定是能赢就赢,不能赢就求平局,最后才算输 平局很好判断,有向图上,从$S$点

UVA818-Cutting Chains(二进制枚举+dfs判环)

Problem UVA818-Cutting Chains Accept:393  Submit:2087 Time Limit: 3000 mSec  Problem Description What a ?nd! Anna Locke has just bought several links of chain some of which may be connected. They are made from zorkium, a material that was frequently

2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 10^5105 的字符串.现在魔法师想要通过一系列魔法使得这两个字符串相同.每种魔法形如 (u,\ v),\ u,\ v \le 10^5(u, v), u, v≤105,可以将一个字符 uu改成一个字符 vv,并且可以使用无限次.出于种种原因,魔法师会强行指定这两个串能否进行修改. 若失败输出 -

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can

hdu4975 网络流解方程组(网络流+dfs判环或矩阵DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4975 A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 579    Accepted Submission(s): 194 Problem Description Drag

hdu4888 Redraw Beautiful Drawings 最大流+判环

hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2007    Accepted Submission(s): 447 Problem Description Alice and Bob are playing together. Alice is crazy abou

CodeForces 659E New Reform (图的遍历判环)

Description Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It isnot guaranteed that you can get from any city to any other one, using only the