关闭

Codeforces Round #369 (Div. 2) D DFS

208人阅读 评论(0) 收藏 举报
分类:



链接:戳这里


D. Directed Roads
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
input
3
2 3 1
output
6
input
4
2 1 1 1
output
8
input
5
2 4 2 5 3
output
28
Note
Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are , ,  initially. Number the roads 1 to 3 in this order.

The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip.

The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.


题意:

给出n个点n条边的有向图。由于存在环,现在任意选择一个边的集合(size==2^n)翻转边的反向使得不存在环。

问有多少种这样的边集合


思路:

对于当前的环,大小为num,那么我们可以选择1条、2条、num-1条边翻转使得环不存在

ans+=2^(num)-2 对于不是环上的点,可以选择翻转或者不翻转 ans*=2^(n-num)


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
#define mod 1000000007
struct edge{
    int v,next;
}e[1000100];
int head[200100],tot=0;
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
int vis[200100],pre[200100],cnt,num;
void DFS(int u,int fa,int deep){
    ++num;
    vis[u]=1;
    pre[u]=deep;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(vis[v]==0) DFS(v,u,deep+1);
        else if(v!=fa) {
            cnt=abs(pre[v]-pre[u])+1;
        }
    }
}
ll f[200100];
int n;
int main(){
    mst(head,-1);
    f[0]=1LL;
    for(int i=1;i<=200000;i++) f[i]=2LL*f[i-1]%mod;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        int x;
        scanf("%d",&x);
        Add(i,x);
        Add(x,i);
    }
    ll ans=1LL;
    int m=n;
    for(int i=1;i<=n;i++){
        if(vis[i]) continue;
        cnt=num=0;
        DFS(i,0,0);
        m-=cnt;
        ans=ans*((f[cnt]-2LL)%mod+mod)%mod;
    }
    ans=ans*f[m]%mod;
    printf("%I64d\n",(ans+mod)%mod);
    return 0;
}


0
0
查看评论

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作,都会拿走一个横行,一个竖行。 所以一共会操作min(横行,竖行)次。 #include #include #include #include #include #include #include #include #pragma commen...
  • rowanhaoa
  • rowanhaoa
  • 2014-07-25 14:39
  • 1479

Codeforces Round #369 (Div. 2) D. Directed Roads (dfs+组合数学 图论)

传送门:D. Directed Roads 描述: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i...
  • guhaiteng
  • guhaiteng
  • 2016-08-30 19:06
  • 374

Codeforces Round #460 (Div. 2) D. Substring(拓扑排序)

题目链接题意:有一个n个点m条边的有向图,每个节点有一个字母,路径的权值是路径上相同字母的最大个数。求最大的路径权值。思路:因为只有26个字母,所以直接假设其中一个字母为相同字母数最大的字母。枚举每一个字母,通过拓扑排序找到最大权值,取其中的最大值就是答案。当图中存在环时,答案就是无穷大,输出-1;...
  • cyf199775
  • cyf199775
  • 2018-02-01 13:55
  • 41

Codeforces Round #327 (div.2)(A B C D)

codeforces round #327 (div.2)
  • lincifer
  • lincifer
  • 2015-10-26 13:11
  • 1094

Codeforces Round #444 (Div. 2) D. Ratings and Reality Shows 贪心

http://codeforces.com/problemset/problem/887/D 题意:有一个模特,她有一个初始rating值 s。她每天可能进行拍照或show中的一种活动。每拍一次照,rating增加 a, 每参加一次show,rating减少 b。她现在想参加一次脱口秀。参加完...
  • qq_36553623
  • qq_36553623
  • 2017-11-08 23:47
  • 189

Codeforces Round #460 (Div. 2) D. Substring

题目链接:D. Substring 题目大意 一张有向图,每个结点上有一个字母,定义一条路径的权值为路径中出现次数最多的字母的出现次数,求图中权值最大的路径的权值,如果权值可以无限大,输出-1 思路 26次DFS,,每次只统计一个字母的权值,cnt[i]记录从i点开始的路径当前字母出...
  • litmxs
  • litmxs
  • 2018-01-31 23:25
  • 67

Codeforces Round #427 (Div. 2) A B C D

A. Key races time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two boys decided ...
  • sinat_35406909
  • sinat_35406909
  • 2017-08-01 02:03
  • 699

【解题报告】Codeforces Round #401 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017-02-25 17:55
  • 413

Codeforces Round #395 (Div. 2)(ABCD)

ps:打完这场cf才知道自己真的很菜,还是停留在AB题的水平,有时候CD其实很简单,但就是想不到,别人一眼看出而我就是想不到,有时候想到了点子上但就是突破不了 题目链接:  Codeforces Round #395 (Div. 2) A题: &...
  • qq_34731703
  • qq_34731703
  • 2017-02-03 12:50
  • 498

【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017-03-31 20:03
  • 276
    个人资料
    • 访问:106822次
    • 积分:4140
    • 等级:
    • 排名:第8836名
    • 原创:320篇
    • 转载:1篇
    • 译文:0篇
    • 评论:37条
    最新评论