【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 711D. Directed Roads 找环

D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standar...
  • FTQOOO
  • FTQOOO
  • 2016年08月31日 21:09
  • 569

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

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

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

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017年02月25日 17:55
  • 417

Codeforces Round #460 (Div. 2) D. Substring(图论,记忆化DFS)

描述 You are given a graph with n nodes and m directed edges. One lowercase letter is assigned ...
  • riba2534
  • riba2534
  • 2018年02月01日 10:03
  • 110

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

题目链接题意:有一个n个点m条边的有向图,每个节点有一个字母,路径的权值是路径上相同字母的最大个数。求最大的路径权值。思路:因为只有26个字母,所以直接假设其中一个字母为相同字母数最大的字母。枚举每一...
  • cyf199775
  • cyf199775
  • 2018年02月01日 13:55
  • 48

Codeforces Round #442 (Div. 2)

A. #include using namespace std; typedef long long ll; int main() { string s; cin >> s; ...
  • Egqawkq
  • Egqawkq
  • 2017年10月24日 02:15
  • 241

Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)

D. Winter is here time limit per test 3 seconds memory limit per test 256 megabytes input stan...
  • Jaihk662
  • Jaihk662
  • 2017年08月14日 14:52
  • 352

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

codeforces round #327 (div.2)
  • lincifer
  • lincifer
  • 2015年10月26日 13:11
  • 1104

Codeforces Round #460 (Div. 2)

C. Seat Arrangements time limit per test 1 second memory limit per test 256 megabytes ...
  • sinat_37668729
  • sinat_37668729
  • 2018年02月01日 10:08
  • 85

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

题目链接:D. Substring 题目大意 一张有向图,每个结点上有一个字母,定义一条路径的权值为路径中出现次数最多的字母的出现次数,求图中权值最大的路径的权值,如果权值可以无限大,输出-1...
  • litmxs
  • litmxs
  • 2018年01月31日 23:25
  • 71
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Codeforces Round #369 (Div. 2)】Codeforces 711D Directed Roads
举报原因:
原因补充:

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