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代表未涂颜色,其他代表已经涂好了,连着 ...
随机推荐
- 第8月第16天 django pil
1. https://github.com/chaonet/forum/ sudo easy_install --find-links http://www.pythonware.com/produ ...
- 日期控件-my97DatePicker用法
网上资料,用法,只能选最近30天等等:http://jingyan.baidu.com/article/e6c8503c7244bae54f1a18c7.html
- 图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
https://blog.csdn.net/bigconvience/article/details/26697645 Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向 ...
- 2017/05/22 java 基础 随笔
多态:一种事物多种形态 前提:1.子父类继承关系 2.方法复写.重写 3.父类引用指向子类对象 成员变量: package com.huawei; public class Demo1 { publi ...
- binlog2sql的安装及使用
binlog2sql是大众点评开源的一款用于解析binlog的工具,在测试环境试用了下,还不错. DBA或开发人员,有时会误删或者误更新数据,如果是线上环境并且影响较大,就需要能快速回滚.传统恢复方法 ...
- JavaScript中对象与函数的某些事[JavaScript语言精粹-N1]
今天在读<JavaScript语言精粹>的时候,关于函数的一个部分,始终觉得有点难以理解,代码如下: 1: var obj = (function(){ 2: var value = 0; ...
- js input输入框的总结
一.输入框只能输入数字 原文:https://www.cnblogs.com/sese/p/5872144.html 分享下js限制输入框中只能输入数字的方法,包括整数与小数,分享几个例子,有需要的朋 ...
- numpy和pandas简单使用
numpy和pandas简单使用 import numpy as np import pandas as pd 一维数据分析 numpy中使用array, pandas中使用series numpy一 ...
- (原创)关于viewpager嵌套listview居中显示的问题
今天折腾了半天自定义控件的问题,如下图所示,我们UI设计了一种可以左右滑动的列表,而列表中又包含了listview.而且要居中显示listview 我一看UI,心想简单,不就是根据datas的数目进行 ...
- 为什么不要在Spring的配置里,配置上XSD的版本号
为什么不要在Spring的配置里,配置上XSD的版本号?因为如果没有配置版本号,取的就是当前jar里的XSD文件,减少了各种风险.而且这样约定大于配置的方式很优雅.