HDOJ6736 #图的遍历 无向图找环 DFS#

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_35850147/article/details/102508575

Forest Program

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

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

642ccpcQHD

Recommend

chendu   |   We have carefully selected several similar problems for you:  6742 6741 6740 6739 6738

Solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 3e5 + 10;
const int maxm = 5e5 + 10;
const int mod = 998244353;
int ecnt, lpcnt, head[maxn], vis[maxn], depth[maxn];
ll loop[maxn];
struct edge { int to, next; } e[maxm << 1];

inline const int read()
{
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

ll qpow(ll x, ll k)
{
    ll res = 1;
    while (k)
    {
        if (k & 1) res = res * x % mod;
        x = x * x % mod; k >>= 1;
    }
    return res;
}

void init()
{
    ecnt = lpcnt = 0;
    memset(head, -1, sizeof(head));
    memset(vis, 0, sizeof(vis));
    memset(depth, 0, sizeof(depth));
    memset(loop, 0, sizeof(loop));
}

void addEdge(int u, int v)
{
    e[ecnt].to = v;
    e[ecnt].next = head[u];
    head[u] = ecnt++;
}

void dfs(int cur, int pre, int dep)
{
    vis[cur] = 1; depth[cur] = dep;
    for (int i = head[cur]; ~i; i = e[i].next)
    {
        int nxt = e[i].to;
        if (nxt == pre) continue;
        if (vis[nxt] == 0) dfs(nxt, cur, dep + 1);
        else if (vis[nxt] == 1) loop[lpcnt++] = depth[cur] - depth[nxt] + 1;
    }
    vis[cur] = 2;
}

int main()
{
    int n, m;
    while (~scanf("%d%d", &n, &m))
    {
        init();
        for (int i = 0; i < m; i++)
        {
            int u = read(), v = read();
            addEdge(u, v); addEdge(v, u);
        }
        for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i, 0, 1);
        ll esum = 0, ans = 1;
        for (int i = 0; i < lpcnt; i++)
        {
            esum += loop[i];
            ans = ans * (qpow(2, loop[i]) - 1) % mod;
        }
        ans = ans * qpow(2, m - esum) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/102508575