CodeForces #369 div2 D Directed Roads DFS
题目链接:D Directed Roads
题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边。这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方向反转),使得这个图里没有环。
思路:感觉关键是,n个点n条边,且每个点的出度为1,所以图里一定没有复环。想要使图里没环,对于每个连通块(点数为i)里的环(如果有环 点数为j),只要不是全翻和全不翻都是满足题意的set,
一共满足题意得set 即为 2^(i-j) * (2^j-2)。所有的连通块方案相乘即为最后的方案数ans.
找到所有的连通块并且得到里面的环的点数:邻接表存图dfs遍历连通块,全局变量标记当前连通块的点数,设置num数组标记每个点被访问时的次序,当再次被访问到时,两次标号相减
即为环的点数。但是这样的样例:
4
2 1 1 1
搜出来的环数会是1,因为3和4搜到1的时候确实1已经被标记了。而且覆盖了前面的2。然后... ... 本来想着先过样例吧...就在所有找到的环数里取max,觉得一定不对,比如这样:
10
2 1 1 1 1 1 1 1 1 1
居然是对的。
然后最后的ans被莫名其妙的%mod爆int。
附AC代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 2000100
#define LL long long
using namespace std;
const LL mod = 1e9+7; struct Node {
int u, v;
int nxt;
}edge[maxn]; //边数 int tot;
LL ans;
int num[maxn];
int numCir;
LL pow2[maxn];
int cntt;
int head[maxn];
bool vis[maxn]; void init() {
memset(num, 0, sizeof(num));
ans = 1;
tot = 0;
numCir = 0;
memset(head, -1, sizeof(head));
pow2[0] = 1;
for (int i=1; i<=maxn; ++i) {
pow2[i] = (pow2[i-1]*2)%mod;
}
// memset(vis, 0, sizeof(vis));
} void addEdge(int u, int v) {
edge[tot].u = u;
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
} void dfs(int id, int cnt) {
cntt++;
//cout << id << "@+++++" << cntt << endl;
for (int i=head[id]; i!=-1; i=edge[i].nxt) {
int v = edge[i].v;
if (!vis[v]) {
num[v] = cnt+1;
vis[v] = 1;
dfs(v, cnt+1);
}
else { ///找到环了
numCir = max(cnt + 1 - num[v], numCir);
}
}
///搜完了整个连通块
} int main() {
//freopen("in.cpp", "r", stdin);
int n;
while(~scanf("%d", &n)) {
init();
// build map
for (int i=1; i<=n; ++i) {
int temp;
scanf("%d", &temp);
addEdge(i, temp);
addEdge(temp, i);
}
memset(vis, 0, sizeof(vis)); for (int i=1; i<=n; ++i) {
//cout << i << "@\n";
if (vis[i]) continue;
vis[i] = 1;
num[i] = 1;
cntt = 0;
numCir = 0;
dfs(i, 1);
// cout << numCir << " " << cntt << endl;
if (numCir == 0) ans += pow2[cntt];
else ans = (ans * ((pow2[cntt-numCir]*(pow2[numCir]-2))%mod))%mod;
}
ans %= mod;
printf("%I64d\n", ans);
}
return 0;
}
CodeForces #369 div2 D Directed Roads DFS的更多相关文章
- Codeforces #369 div2 D.Directed Roads
D. Directed Roads time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...
- 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 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)
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 711 D. Directed Roads (DFS判环)
题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
- CodeForces 711D Directed Roads (DFS判环+计数)
题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
随机推荐
- objective-c第六章课后练习6
题6:接受从终端输入的整数,提取并用英语显示这个数的每一个数字,如932,显示nine three two (题目中注了.这个练习很难)的确有点难,自己想了很久网上也各种搜索.也算是找到参考了 cod ...
- innodb的表最大限制
相信大多数人都不知道,innodb的表最大限制为64TB,但是why? Each space is divided into pages, normally 16 kib each (this can ...
- Linux中执行shell脚本的4种方法
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- 简单的jQuery 四级分类实用插件
前言 最近因需要自己封装了一个很简单的四级分类的jQuery插件,主要用于后台数据的传输和获取.接下来就分享一下这个实用的插件吧. 正文 老规矩,先看一下效果,这个就很丑了,没有美化的,因为主要还是用 ...
- Worker Thread
http://www.codeproject.com/Articles/552/Using-Worker-Threads Introduction Worker threads are an eleg ...
- Argument list too long......
作为一名运维人员来说,这个错误并不陌生,在执行rm.cp.mv等命令时,如果要操作的文件数很多,可能会使用通配符批量处理大量文件,这时就可能会出现“Argument list too long”这个问 ...
- 十六、Swing高级组件
1.利用JTable类直接创建表格 (1)创建表格 构造方法:JTable(Object rowData,Object[] columnNames) (2)定制表格 编辑:isCellEditable ...
- php手册杂记
1, strcmp()是比较两个字符串的大小,两个字符串相同时返回0,第一个字符串大于第二个字符串时返回一个正值,否则返回负值.比较两个字符串的算法是:逐个比较两个串中对应的字符,字符大小按照ASCI ...
- NavBarControl
https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraNavBarNavBarControltopic Views h ...
- UI设计的分类
软件UI设计(界面设计包括硬件界面设计和软件界面设计,我们这里探讨的是软件界面设计)包括用户研究.交互设计.与界面设计三部分. 1,用户研究 我们再产品开发的前期,通过调查研究,了解用户的工作性质 ...