zoukankan      html  css  js  c++  java
  • Codeforces 711D dfs找环

    D. Directed Roads
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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条边,分别从每个点出发指向某个点,现在可以把某些边翻转,问总共可以得到多少种无换图?

    分析:

    要想得到无换图,那么就需要把环找出来,然后至少翻转一条边就可以了,总的方法数是2^x-2,2是一条边也不翻转和翻转所有边。

    其他不在环中的边可以翻转也可以不翻转,所以方案数是2^x

    #include<bits/stdc++.h>
    using namespace std;
    #define rep(i,s,t) for(int i=(s);i<(t);i++)
    #define per(i,t,s) for(int i=(t);i>=(s);i--)
    #define fi first
    #define se second
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int>pii;
    const int mod = 1e9 + 7;
    const int N = 2e5 + 9;
    vector<pii>g[N];
    int r;
    int d[N], vis[N];
    void dfs (int u, int fa, int deep) {
        if (vis[u]) {
            if (r == 0) r = deep - d[u];
            return;
        }
        vis[u] = 1;
        d[u] = deep;
        rep (i, 0, g[u].size() ) {
            if (g[u][i].se == fa) continue;
            dfs (g[u][i].fi, g[u][i].se, deep + 1);
        }
    }
    ll f[N];
    int main() {
        //freopen("f.txt","r",stdin);
        int n, x;
        scanf ("%d", &n);
        f[0] = 1;
        rep (i, 1, n + 1) f[i] = f[i - 1] * 2 % mod;
        rep (i, 1, n + 1) {
            scanf ("%d", &x);
            g[i].push_back (pii (x, i) );
            g[x].push_back (pii (i, i) );
        }
        ll ans = 1;
        int sum = 0;
        rep (i, 1, n + 1) if (!vis[i]) {
            r = 0;
            dfs (i, 0, 0);
            sum += r;
            //cout<<r<<endl;
            ans = ans * (f[r] - 2) % mod;
        }
        cout << ans* (f[n - sum]) % mod << endl;
        return 0;
    }
  • 相关阅读:
    Linux知识(7)----远程登录 和远程拷贝
    ROS知识(9)----安装Turtlebot2和远程控制Turtlebot2
    几张图理解Roll, Pitch, Yaw的含义
    GIT(1)----更新代码和上传代码操作的步骤
    ROS知识(8)----CMakeLists.txt文件编写的理解
    ROS知识(7)----ROS命令中的单引号`和‘的混淆问题
    ROS知识(6)----卸载ROS系统
    python知识(2)----python的多态
    DWR3.0框架入门(3) —— ScriptSession的维护及优化
    log4j.properties配置详解
  • 原文地址:https://www.cnblogs.com/01world/p/5821190.html
Copyright © 2011-2022 走看看