【HDU6736】Forest Program(搜索)

Online JudgeOnline ExerciseOnline TeachingOnline ContestsExercise Author
F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts

Problem Archive
Realtime Judge Status
Authors Ranklist
 

     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China

Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Author xwt20172139
Mail Mail 0(0)
Control Panel Control Panel 
Sign Out Sign Out

 

Forest Program

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


 

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.

 

题目大意:现在给你一个仙人掌图,仙人掌图的定义就是每一条边最多在一个简单环上,然后问你把现在他给你的这个图弄成森林的删边的方案数,

思路:

首先我们考虑的是每一个环上的数量,应该是除了不删的情况都可以,如果这个环是由K个边的,那么也就是2^k-1个,每环都乘起来,然后就是裸露在外面的没有成环的边了,这些边是没有任何限制的也就是2^k个,乘起来就可以了

比赛的时候想到了点双,我们是用点双过的,但是很耗费时间,感觉还是搜索好一点,最起码时间上没问题了,

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int maxn=3e5+10;
const ll mod=998244353;
int vis[maxn];
vector<int >G[maxn];
int n,m;
int ans,num;
ll quick_pow(ll a,ll b)
{
    ll res=1;
    while(b>0)
    {
        if(b%2)
            res=(res*a)%mod;
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
void DFS(int u,int fa)
{
    vis[u]=vis[fa]+1;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(fa==v) continue;
        if(vis[v]==0)
            DFS(v,u);
        else if(vis[u]>vis[v])
        {
            int temp=vis[u]-vis[v]+1;
            num+=temp;
            /*cout<<"u,v :"<<u<<" "<<v<<endl;
            cout<<"vis u,v :"<<vis[u]<<" "<<vis[v]<<endl;;
            cout<<"quick_pow:"<<quick_pow(2,temp)-1<<endl;*/
            ans=(ans%mod*(quick_pow(2,temp)-1)%mod)%mod;
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            G[i].clear();
        ans=1,num=0;
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
                DFS(i,0);
        }
        /*for(int i=1;i<=n;i++)
            cout<<vis[i]<<" ";
        cout<<endl;*/
        ans=(ans%mod*(quick_pow(2,m-num))%mod)%mod;
        cout<<ans<<endl;
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值