Codeforces Round #369 (Div. 2) D. Directed Roads 数学
D. Directed Roads
题目连接:
http://www.codeforces.com/contest/711/problem/D
Description
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n.
There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai ≠ i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town A to town B before the flip, it will go from town B to town A after.
ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns A1, A2, ..., Ak (k > 1) such that for every 1 ≤ i < k there is a road from town Ai to town Ai + 1 and another road from town Ak to town A1. In other words, the roads are confusing if some of them form a directed cycle of some towns.
Now ZS the Coder wonders how many sets of roads (there are 2n variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.
Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.
Input
The first line of the input contains single integer n (2 ≤ n ≤ 2·105) — the number of towns in Udayland.
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ai ≠ i), ai denotes a road going from town i to town ai.
Output
Print a single integer — the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109 + 7.
Sample Input
3
2 3 1
Sample Output
6
Hint
题意
给你一个\(n\)点\(n\)边的无向图,你可以翻转任意几条边,但是每条边只能翻转一次,问你有多少种方案,使得这个图不存在环。
题解:
由于n点n边,所以不存在环套环的情况。
对于每一个环,我们都有2n-2种方案使得这个环不存在,就是2n减去全部翻转,和全部不翻转的方案。
然后不在环上的边,爱翻翻,乘上去这个答案就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 2e5+7;
long long f[maxn];
int a[maxn],n,cnt;
int vis[maxn];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
f[0]=1;
for(int i=1;i<=n;i++)
f[i]=f[i-1]*2ll%mod;
int num = n;
long long ans = 1;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
int x=i;
int now=cnt;
while(vis[x]==0)
{
vis[x]=++cnt;
x=a[x];
}
if(vis[x]>now)
{
int pnum=cnt-vis[x]+1;
num=num-pnum;
ans=ans*(f[pnum]-2+mod)%mod;
}
}
}
ans=ans*f[num]%mod;
cout<<ans<<endl;
}
Codeforces Round #369 (Div. 2) D. Directed Roads 数学的更多相关文章
- 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 Round #369 (Div. 2)-D Directed Roads
题目大意:给你n个点n条边的有向图,你可以任意地反转一条边的方向,也可以一条都不反转,问你有多少种反转的方法 使图中没有环. 思路:我们先把有向边全部变成无向边,每个连通图中肯定有且只有一个环,如果这 ...
- Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路
D - Destroying Roads Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...
- Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
- Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路
题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)
题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...
随机推荐
- 使用 maven 搭建web开发基本架构
我觉得可能,对于还没有使用过 IDEA 进行开发的童鞋来说,直接撸代码是有些尴尬的.那么我会把示例代码之前的那些事再在这里写一遍 按图步骤进行即可进行基本项目结构搭建 现在基本流行 maven 管理项 ...
- ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等)
以下是关于ASP.NET中保存各种信息的对象的比较,理解这些对象的原理,对制作完善的程序来说是相当有必要的(摘至互联网,并非原创--xukunping) 在ASP.NET中,有很多种保存信息的对象.例 ...
- TED_Topic7:How we unearthed the spinosaurus
By Nizar Ibrahim A 50-foot-long carnivore who hunted its prey in rivers 97 million years ago, the sp ...
- java代码实现图片处理功能。对图片质量进行压缩。
java图片处理有点头疼,找了很多资料.在这里进行一个汇总,记录下个人的体验,也希望对大家有所帮助. 需求:浏览的图片需要在1M一下. 1.真正对图片的质量进行压缩的(不是通过修改图片的高,宽进行缩小 ...
- 20155212 2016-2017-2 《Java程序设计》第8周学习总结
20155212 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 Chapter14 1. Channel架构与操作 想要取得Channel的实作对象,可以使 ...
- __new__[转载]
转载自https://www.cnblogs.com/MnCu8261/p/6365665.html 实际上,实例化类时调用的第一个方法并不是__init__,而是__new__,其作用正是创建并返回 ...
- SqlMapConfig.xml全局配置文件介绍——(四)
----------mybatis的全局配置文件SqlMapConfig.xml,配置内容如下:----------- properties(属性) settings(全局配置参数) typeAlia ...
- LOJ 2249: 洛谷 P2305: 「NOI2014」购票
题目传送门:LOJ #2249. 题意简述: 有一棵以 \(1\) 号节点为根节点的带边权的树. 除了 \(1\) 号节点的所有节点上都有人需要坐车到达 \(1\) 号节点. 除了 \(1\) 号节点 ...
- C++ 螺旋矩阵算法
清理磁盘空间的时候翻出了多年前写过的螺旋矩阵,代码效率和水平较低,纪念一下,保存到博客园! // ConsoleApplication3.cpp : 定义控制台应用程序的入口点. // #includ ...
- Jenkins与代码上线解决方案
Jenkins是一个用Java编写的开源的持续集成工具.在与Oracle发生争执后,项目从Hudson项目独立. Jenkins提供了软件开发的持续集成服务.它运行在Servlet容器中(例如Apac ...