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

177 篇文章 1 订阅
87 篇文章 1 订阅

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值