Codeforces711D-Directed Roads(强连通分量+乘法原理)
来源:互联网 发布:80端口哪个运营商 编辑:程序博客网 时间:2024/06/15 23:44
题目链接
http://codeforces.com/contest/711/problem/D
思路
这道题的输入很神奇,由输入可以得出一个结论就是每个点最多只会有一条出边,因此每个点最多在一个环中。
然后对于一个环比如1 -> 2 -> 3 -> 1,其方案数是
对所有环,由输入得到的性质,因此结果直接相乘即可
代码
#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)const int maxn = 2 * 100000 + 5;const int mod = 1e9 + 7;vector<int> G[maxn];int n, scc_cnt, sccno[maxn], pre[maxn], lowlink[maxn], dfs_clock;stack<int> S;void dfs(int u) { pre[u] = lowlink[u] = ++dfs_clock; S.push(u); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (!pre[v]) { dfs(v); lowlink[u] = min(lowlink[u], lowlink[v]); } else if (!sccno[v]) { lowlink[u] = min(lowlink[u], pre[v]); } } if (lowlink[u] == pre[u]) { scc_cnt++; while (1) { int x = S.top(); S.pop(); sccno[x] = scc_cnt; if (x == u) break; } }}void find_scc() { dfs_clock = scc_cnt = 0; memset(sccno, 0, sizeof(sccno)); memset(pre, 0, sizeof(pre)); for (int i = 1; i <= n; i++) { if (!sccno[i]) dfs(i); }}LL power(int x) { LL ans = 1; for (int i = 1; i <= x; i++) (ans <<= 1) %= mod; return ans - 2;}int main() { scan(n); for (int i = 1; i <= n; i++) { int x; scan(x); G[i].push_back(x); } find_scc(); int cnt[maxn]; memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; i++) cnt[sccno[i]]++; LL res = 1; for (int i = 1; i <= n; i++) { LL x = (LL)(cnt[i]); if (!x) break; if (x >= 2) ((res *= power(x))) %= mod; else if (x == 1) (res *= 2) %= mod; } printf("%I64d\n", res); return 0;}
0 0
- Codeforces711D-Directed Roads(强连通分量+乘法原理)
- [Codeforces Round #369 (Div. 2)D. Directed Roads]Tarjan强连通分量+组合计数
- codeforces711D Directed Rodes(dfs)
- 强连通分量 (转)
- 强连通分量(桥)
- 强连通分量(kosaraju)
- 强连通分量(Tarjan)
- 强连通分量(模板)
- 强连通分量(方法)
- poj 1236(tarjam)强连通分量
- poj-2553(强连通分量)
- 强连通分量SCC模版(LRJ)
- Strongly connected(hdu4635(强连通分量))
- 迷宫城堡(hdu1269,强连通分量)
- hdu 4635(强连通分量)
- 强连通分量(LRJ训练指南)
- 强连通分量(tarjan算法)
- 强连通分量+缩点(poj2553)
- swift3新路程(5)? 和 ??
- Android——滑动监听RecyclerView线性流+左右划删除+上下移动
- 环信群聊功能
- 堆和栈的区别(转过无数次的文章)
- DOM初探(4)
- Codeforces711D-Directed Roads(强连通分量+乘法原理)
- Talk about the structure of a html page
- Android入门三 Http通信(实例)
- 存储过程
- 各种Map的特点
- 封装原生的文件上传类
- 小熊仔要开启奔跑之旅啦
- static关键字所导致的内存泄漏问题
- poj--3249 Test for Job(topsort + dp)