zoukankan      html  css  js  c++  java
  • dfs找环

    http://acm.hdu.edu.cn/showproblem.php?pid=6736

    Forest Program

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 299    Accepted Submission(s): 111


    Problem Description
    The kingdom of Z is fighting against desertification these years since there are plenty of deserts in its wide and huge territory. The deserts are too arid to have rainfall or human habitation, and the only creatures that can live inside the deserts are the cactuses. In this problem, a cactus in desert can be represented by a cactus in graph theory.
    In graph theory, a cactus is a connected undirected graph with no self-loops and no multi-edges, and each edge can only be in at most one simple cycle. While a tree in graph theory is a connected undirected acyclic graph. So here comes the idea: just remove some edges in these cactuses so that the remaining connected components all become trees. After that, the deserts will become forests, which can halt desertification fundamentally.
    Now given an undirected graph with n vertices and m edges satisfying that all connected components are cactuses, you should determine the number of schemes to remove edges in the graph so that the remaining connected components are all trees. Print the answer modulo 998244353.
    Two schemes are considered to be different if and only if the sets of removed edges in two schemes are different.
     
    Input
    The first line contains two non-negative integers n, m (1 ≤ n ≤ 300 000, 0 ≤ m ≤ 500 000), denoting the number of vertices and the number of edges in the given graph.
    Next m lines each contains two positive integers u, v (1 ≤ u, v ≤ n, u = v), denoting that vertices u and v are connected by an undirected edge.
    It is guaranteed that each connected component in input graph is a cactus.
     
    Output
    Output a single line containing a non-negative integer, denoting the answer modulo 998244353.
     
    Sample Input
    3 3 1 2 2 3 3 1 6 6 1 2 2 3 3 1 2 4 4 5 5 2
     
    Sample Output
    7 49
     
    Source
     
    Recommend
    chendu   |   We have carefully selected several similar problems for you:  6742 6741 6740 6739 6738
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 998244353
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    const int maxn = 505005 ;
    const int maxm = 2050000;
    ll n , m , sum , ans , vis[maxn] , dfn[maxn] , cnt;
    ll head[maxn];
    struct Edge
    {
        ll to , next ;
    }e[maxm];
    
    void add(ll u , ll v)
    {
        e[cnt].to = v ;
        e[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    void init()
    {
        memset(vis , 0 , sizeof(vis));
        memset(dfn , 0 , sizeof(dfn));
        memset(head , -1 , sizeof(head));
        cnt = 0 , ans = 1 ;
    }
    ll qpow(ll base, ll n)
    {
        ll ans = 1;
        while(n)
        {
            if(n&1) ans=(ans%mod)*(base%mod)%mod;
            base = (base%mod) * (base%mod)%mod;
            n/=2;
        }
        return ans%mod;
    }
    
    void dfs(ll id , ll step , ll fa)
    {
        vis[id] = 1 , dfn[id] = step ;
        for(ll i = head[id] ; i != -1 ; i = e[i].next)
        {
            ll v = e[i].to ;
            //cout << i << " " << v << endl ;
            if(v == fa || vis[v] == 2) continue ;
            if(vis[v] == 1)
            {
                sum += step - dfn[v] + 1;
                ans *= (qpow(2 , step-dfn[v]+1)-1+mod) % mod ;
                ans %= mod ;
            }
            else
            {
                dfs(v , step+1 , id);
            }
        }
        vis[id] = 2 ;
    }
    
    int main()
    {
        scanf("%lld%lld" , &n , &m);
        init();
        for(ll i = 1 ; i <= m ; i++)
        {
            ll u , v ;
            scanf("%lld%lld" , &u , &v);
            add(u , v);
            add(v , u);
        }
        for(ll i = 1 ; i <= n ; i++)
        {
            if(!vis[i])
                dfs(i , 1 , -1);
        }
        ans *= qpow(2 , m - sum);
        ans %= mod ;
        printf("%lld
    " , ans);
    
    
        return 0;
    }
  • 相关阅读:
    Codeforces 1368F
    Codeforces 1364C
    Hiho1422 Harmonic Matrix Counter (高斯消元)
    Codeforces 19D
    P3164 [CQOI2014]和谐矩阵(高斯消元 + bitset)
    P4254 [JSOI2008]Blue Mary开公司 (李超树)
    P4755 Beautiful Pair (分治 + 主席树)
    P4587 [FJOI2016]神秘数(主席树)
    分布式锁实现,与分布式定时任务
    redis应用场景,缓存的各种问题
  • 原文地址:https://www.cnblogs.com/nonames/p/11606127.html
Copyright © 2011-2022 走看看