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

沙漠中的每一个连通块都是一棵仙人掌;一个连通块是一棵仙人掌当且仅当连通块中不存在重边和自环,并且每一条边仅被至多一个简单环覆盖。

经过一番评估,Z 国决定通过删去沙漠中的一些边,最终将沙漠变为森林。这里我们定义森林满足:森林中每一个连通块都是一棵树,而树是边数等于点数减一的连通块。现在给定一个包含 n 个点的沙漠,请你求出 Z 国一共有多少种满足要求的沙漠改造方案。两种方案不同当且仅当方案中被删去的边集不同。由于答案可能很大,请将最终答案对 998244353 取模后输出。

思路:

直接dfs。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int);
const int N=(int)5e5+;
int read(){
int x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';
return x*f;
}
ll qpow(ll a,ll b,ll mod)
{
ll ans;
// a%=mod;
ans=;
while(b!=)
{
if(b&)
ans=(ans*a)%mod;
b/=;
a=(a*a)%mod;
}
return ans;
} bool vis[N];
ll dep[N];
vector<vector<int> >G(N); ll loop,other,ans;
void dfs(int u,int v)
{
// cout<<u<<' '<<v<<endl;
if(vis[v])
{
if(dep[u]<=dep[v])return;
ans*=qpow(,dep[u]+-dep[v],mod)-;
ans=(ans+mod)%mod;
loop+=dep[u]+-dep[v];
return;
}
vis[v]=;
dep[v]=dep[u]+;
int sz=G[v].size();
for(int i=;i<sz;++i)
{
int to=G[v][i];
if(!dep[v]<=dep[to]&&to!=u)
{
dfs(v,to);
}
}
} int main()
{
int n,way;
while(~sc("%d%d",&n,&way))
{
ans=;loop=other=;
for(int i=;i<=n;++i)
G[i].clear(),dep[i]=vis[i]=;
for(int i=;i<=way;++i)
{
int u,v;
u=read(),v=read();
G[u].push_back(v);
G[v].push_back(u);
}
// bfs({0,1});
dfs(,);
other=way-loop;
ans*=qpow(,other,mod);
ans%=mod;
// if(loop==0)ans=0;
pr("%lld\n",ans);
//cout<<loop<<' '<<other<<endl;
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

Forest Program(dfs方法---树上的环)的更多相关文章

  1. 2019CCPC秦皇岛 F Forest Program

    队友过的:https://blog.csdn.net/liufengwei1/article/details/101632506 Forest Program Time Limit: 2000/100 ...

  2. HDU 1524 树上无环博弈 暴力SG

    一个拓扑结构的图,给定n个棋的位置,每次可以沿边走,不能操作者输. 已经给出了拓扑图了,对于每个棋子找一遍SG最后SG和就行了. /** @Date : 2017-10-13 20:08:45 * @ ...

  3. 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组

    题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...

  4. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  5. 【bzoj4940】[Ynoi2016]这是我自己的发明 DFS序+树上倍增+莫队算法

    题目描述 给一个树,n 个点,有点权,初始根是 1. m 个操作,每次操作: 1. 将树根换为 x. 2. 给出两个点 x,y,从 x 的子树中选每一个点,y 的子树中选每一个点,如果两个点点权相等, ...

  6. 2019 China Collegiate Programming Contest Qinhuangdao Onsite F. Forest Program(DFS计算图中所有环的长度)

    题目链接:https://codeforces.com/gym/102361/problem/F 题意 有 \(n\) 个点和 \(m\) 条边,每条边属于 \(0\) 或 \(1\) 个环,问去掉一 ...

  7. 2019ccpc秦皇岛/Gym102361 F Forest Program 仙人掌上dfs

    题意: 某地沙漠化严重,沙漠里长了很多仙人掌,现在要让你删掉仙人掌的一些边让它的所有连通分量都是树,就完成了沙漠绿化(什么鬼逻辑?)让你计算删边的方案数. 仙人掌是一种特殊的图,它的每一条边只属于1或 ...

  8. 【bzoj3488】[ONTAK2010]Highways DFS序+树上倍增+树状数组

    题目描述 一棵n个点的树,给定m条路径,q次询问包含一条路径的给定路径的个数+1 输入 The first line of input contains a single integer N(1< ...

  9. BZOJ4424/CF19E Fairy(dfs树+树上差分)

    即删除一条边使图中不存在奇环.如果本身就是个二分图当然任意一条边都可以,先check一下.否则肯定要删除在所有奇环的交上的边. 考虑怎么找这些边.跑一遍dfs造出dfs树,找出返祖边构成的奇环.可以通 ...

随机推荐

  1. CDialog::DoModal()问题和_WIN32_WINNT

    1.从CDialogEx派生自己的CMyDialog,到DoModal()时总提示 error C2039: "DoModal": 不是"CMyDialog"的 ...

  2. codeforces405D

    Toy Sum CodeForces - 405D Little Chris is very keen on his toy blocks. His teacher, however, wants C ...

  3. Go http包执行流程

    Go 语言实现的 Web 服务工作方式与其他形式下的 Web 工作方式并没有什么不同,具体流程如下: -- http包执行流程 Request:来自用户的请求信息,包括 post.get.Cookie ...

  4. 8 HashMap

    1.Map接口 public interface Map<K, V> 将键映射到值的对象,一个映射不能包含重复的键,每个键只能映射到一个值. 具体的实现:HashMap,TreeMap, ...

  5. LSTM参数和结构的本质理解——num_units参数/batch_size/cell计算

    参考 ———— 图例讲解 https://blog.csdn.net/u014518506/article/details/80445283 理解:cell其实只有一个 : sequence leng ...

  6. 解决 无法启动此程序,因为计算机中丢失opencv_world341.dll。请尝试重新安装改程序已解决此问题

    在运行OpenCV程序时报错:“无法启动此程序,因为计算机中丢失opencv_world341.dll.请尝试重新安装改程序已解决此问题”. 解决方法 我的bin目录是 D:\opencv\build ...

  7. nodejs服务端实现post请求

    博客之前写过一篇php实现post请求的文章. 今天想到好久没有输出了,重新认识到输出的重要性.百般思索该写些什么?想来想去,想到了两点: 逐步熟练nodejs各种场景知识,针对mysql数据交互和f ...

  8. LC 794. Valid Tic-Tac-Toe State

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  9. UiUtils

    import android.app.Activity; import android.app.Dialog; import android.content.Context; import andro ...

  10. HashMap ArrayList 和 List对象的转换

    public static void main(String[] args) { List list = new ArrayList<>(); HashMap map = new Hash ...