关闭

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

标签: dfs快速幂
121人阅读 评论(0) 收藏 举报
分类:

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);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:119530次
    • 积分:9413
    • 等级:
    • 排名:第1915名
    • 原创:857篇
    • 转载:3篇
    • 译文:0篇
    • 评论:45条
    联系方式
    QQ:1723010279
    邮箱:1723010279@qq.com
    欢迎交流。
    文章分类
    最新评论