【Codeforces Round #369 (Div. 2)】Codeforces 711D Directed Roads

原创 2016年08月30日 14:15:43

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.

可以发现,边一开始的方向是没有关系的,问题就是有多少种安排边的方向的方式使图中没有有向环。
对于一个大小为x的无向简单环,安排他的方式有2^x-2,即只要避免所有的边全向着一个方向即可。
因为边数恰好为n,且每个点都至少有一条边,所以每个连通块里最多有一个无向简单环。这就意味着这些环都可以O(n)地dfs找出。
设第i个环的大小为size[i],则最后的答案为
2^(n-Σsize)*π 2^(size[i]-2)。
快速幂即可。

#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
using namespace std;
#define LL long long
const int mod=1000000007;
int n,size[200010],tot;
vector<int> to[200010],num[200010];
stack<int> sta;
bool vis[200010];
void dfs(int u,int fa)
{
    int i,j,k,v,x,y,z;
    vis[u]=1;
    sta.push(u);
    for (i=0;i<to[u].size();i++)
      if ((num[u][i]^1)!=fa)
      {
        v=to[u][i];
        if (vis[v])
        {
            if (!size[tot])
            {
                do
                {
                    x=sta.top();
                    sta.pop();
                    size[tot]++;
                }
                while (x!=v);
            }
        }
        else dfs(v,num[u][i]);
      }
    if (!size[tot]) sta.pop();
}
LL pow(int p)
{
    LL ans=1,base=2;
    for (;p;p>>=1,base=(base*base)%mod)
      if (p&1) ans=(ans*base)%mod;
    return ans;
}
int main()
{
    int i,j,k,p,q,x,y,z,nn;
    LL ans;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    {
        scanf("%d",&x);
        to[x].push_back(i);
        num[x].push_back(i*2);
        to[i].push_back(x);
        num[i].push_back(i*2+1);
    }
    for (i=1;i<=n;i++)
      if (!vis[i])
      {
        tot++;
        while (!sta.empty()) sta.pop();
        dfs(i,-1);
      }
    nn=n;
    for (i=1;i<=tot;i++)
      nn-=size[i];
    ans=pow(nn);
    for (i=1;i<=tot;i++)
      ans=(ans*(pow(size[i])-2+mod))%mod;
    printf("%I64d\n",ans);
}
版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。

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

传送门:D. Directed Roads 描述: D. Directed Roads time limit per test 2 seconds memory li...

Codeforces Round #369 (Div. 2) -- D. Directed Roads (DFS找环)

大体题意: 给你一个有向图,可能会有环,你的操作是反向一条路,求得使得图中没有环所有方案数? 思路: 假如图中没有环的话,有n条边,答案就是2^n 如果有个m边的环,间接法考虑,总方案是2^m...

[Codeforces Round #369 (Div. 2)D. Directed Roads]Tarjan强连通分量+组合计数

[Codeforces Round #369 (Div. 2)D. Directed Roads]Tarjan强连通分量题目链接:[Codeforces Round #369 (Div. 2)D. D...

Codeforces Round #369 (Div. 2) D. Directed Roads

题目链接题意:给你一个n个点,n条有向边的图,你可以使任意条边反向,但是每条边只能反向一次,请求出使图不存在环的所有方案数量仔细思考我们发现,对于一个点数为x的环,除去全部不反向和全部反向两种情况,其...
  • naipp
  • naipp
  • 2016年08月30日 23:28
  • 118

Codeforces #369 div2 D.Directed Roads

codeforces

CodeForces 369 div2 D Directed Roads 图论 数论

D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces 369 D.Directed Roads(数学,判断图中每个环的边数)

题目链接 D. Directed Roads time limit per test 2 seconds memory limit per test 256 megaby...
  • fouzhe
  • fouzhe
  • 2016年09月03日 10:16
  • 238

[基环] codeforces 711D. Directed Roads

Directed Roads题意:给n个点,和P[i],表示从第i个点到P[i]有一条有向边,每条边可以至多翻转方向一次,问有多少种方法翻转后没有环。思路:n个点n条边,无自环,每点出度为1,这个模型...
  • kg20006
  • kg20006
  • 2016年08月30日 14:39
  • 256

【34.40%】【codeforces 711D】Directed Roads

time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandar...

Codeforces Problem 711D Directed Roads(Tarjan判环)

Codeforces Problem 711D Directed Roads(Tarjan判环)
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Codeforces Round #369 (Div. 2)】Codeforces 711D Directed Roads
举报原因:
原因补充:

(最多只允许输入30个字)