• cf711D. Directed Roads(环)


    题意

    题目链接

    (n)个点(n)条边的图,有多少种方法给边定向后没有环

    Sol

    一开始傻了,以为只有一个环。。。实际上N个点N条边还可能是基环树森林。。

    做法挺显然的:找出所有的环,设第(i)个环的大小为(w_i)

    (ans = 2^{N - sum w_i} prod (2^{w_i} - 2))

    最后减掉的2是形成环的情况

    #include<bits/stdc++.h> 
    #define Pair pair<int, int>
    #define MP(x, y) make_pair(x, y)
    #define fi first
    #define se second
    #define int long long 
    #define LL long long 
    #define Fin(x) {freopen(#x".in","r",stdin);}
    #define Fout(x) {freopen(#x".out","w",stdout);}
    //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
    //char buf[(1 << 22)], *p1 = buf, *p2 = buf;
    using namespace std;
    const int MAXN = 1e6 + 10,  mod = 1e9 + 7, INF = 1e9 + 10;
    const double eps = 1e-9, PI = acos(-1);
    template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
    template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
    template <typename A> inline void debug(A a){cout << a << '
    ';}
    template <typename A> inline LL sqr(A x){return 1ll * x * x;}
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = (x * 10 + c - '0') % mod, c = getchar();
        return x * f;
    }
    int N, dep[MAXN], w[MAXN], top, po2[MAXN], vis[MAXN];
    vector<int> v[MAXN];
    void dfs(int x, int d) {
    	dep[x] = d; vis[x] = 1;
    	for(auto &to : v[x]) {
    		if(!vis[to])dfs(to,  d + 1);
    		else if(vis[to] == 1) w[++top] = dep[x] - dep[to] + 1;
    	}
    	vis[x] = 2;
    }
    signed main() {
    	N = read(); 
    	po2[0] = 1;
    	for(int i = 1; i <= N; i++) po2[i] = mul(2, po2[i - 1]);
    	for(int i = 1; i <= N; i++) {
    		int x = read(); 
    		v[i].push_back(x);
    	}
    	for(int i = 1; i <= N; i++) 
    		if(!dep[i]) 
    			dfs(i, 1);
    	int sum = 0, res = 1;
    	for(int i = 1; i <= top; i++) sum += w[i], res = mul(res, po2[w[i]] - 2 + mod);
    	printf("%d
    ", mul(po2[N - sum], res));
    	
    	return 0;
    }
    
  • 相关阅读:
    推荐:VisualStudio 2005/2008的“Consolas”字体包
    [转]Calendar 動態產生子控制項的 Event Handler, 模拟__doPostBack() 回发
    Ajax ToolKit ModelPopupExtender应用经验二则
    [转]Calendar 控件日期复选
    ASP.NET偷懒大法六(可空类型的动态赋值)
    弹出层(Div)屏蔽父窗口并且让父窗口变暗
    CSS巧用expression来区分只读文本框
    formValidator用户注册表单自动验证
    基于Session原理的验证码方案
    jQuery解决IE6下PNG图片背景透明问题
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10339112.html
Copyright © 2020-2023  润新知