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 = ;
const int maxm = ;
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 , , sizeof(vis));
memset(dfn , , sizeof(dfn));
memset(head , - , sizeof(head));
cnt = , ans = ;
}
ll qpow(ll base, ll n)
{
ll ans = ;
while(n)
{
if(n&) ans=(ans%mod)*(base%mod)%mod;
base = (base%mod) * (base%mod)%mod;
n/=;
}
return ans%mod;
} void dfs(ll id , ll step , ll fa)
{
vis[id] = , dfn[id] = step ;
for(ll i = head[id] ; i != - ; i = e[i].next)
{
ll v = e[i].to ;
//cout << i << " " << v << endl ;
if(v == fa || vis[v] == ) continue ;
if(vis[v] == )
{
sum += step - dfn[v] + ;
ans *= (qpow( , step-dfn[v]+)-+mod) % mod ;
ans %= mod ;
}
else
{
dfs(v , step+ , id);
}
}
vis[id] = ;
} int main()
{
scanf("%lld%lld" , &n , &m);
init();
for(ll i = ; i <= m ; i++)
{
ll u , v ;
scanf("%lld%lld" , &u , &v);
add(u , v);
add(v , u);
}
for(ll i = ; i <= n ; i++)
{
if(!vis[i])
dfs(i , , -);
}
ans *= qpow( , m - sum);
ans %= mod ;
printf("%lld\n" , ans); return ;
}

dfs找环的更多相关文章

  1. # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)

    「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...

  2. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  3. CodeForces - 103B(思维+dfs找环)

    题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...

  4. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  5. 与图论的邂逅06:dfs找环

    当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...

  6. HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)

    求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...

  7. UVaLive 6950 && Gym 100299K Digraphs (DFS找环或者是找最长链)

    题意:有n个只包含两个字母的字符串, 要求构造一个m*m的字母矩阵, 使得矩阵的每行每列都不包含所给的字符串, m要尽量大, 如果大于20的话构造20*20的矩阵就行了. 析:开始吧,并没有读对题意, ...

  8. [NOI2008]假面舞会——数论+dfs找环

    原题戳这里 思路 分三种情况讨论: 1.有环 那显然是对于环长取个\(gcd\) 2.有类环 也就是这种情况 1→2→3→4→5→6→7,1→8→9→7 假设第一条链的长度为\(l_1\),第二条为\ ...

  9. [蓝桥杯2018初赛]小朋友崇拜圈(dfs找环)

    传送门 思路: 题意大意:n条有向边,找出最大环. 我们发现,如果一个小朋友没有被任何人崇拜,那么他一定不位于环中.为此我们可以设置一个indug数组预处理.如果2被崇拜了那么indug[2]就加加, ...

  10. New Reform---cf659E(dfs找环)

    题目链接:http://codeforces.com/problemset/problem/659/E 给你n个点,m条双向边,然后让你把这些边变成有向边,使得最后的图中入度为0的点的个数最少,求最少 ...

随机推荐

  1. 36.React基础介绍——2019年12月24日

    2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...

  2. A1009

    两个多项式的乘积 两个数组,一个放多项式1,一个放结果 注意:arr2[j+exp]+=arr1[j]*coe; 因为有指数相加相同的情况下需要合并系数 #include<cstdio> ...

  3. 如何解决DEDE织梦友情链接字数限制与链接个数限制的问题?

    如何解决DEDE织梦友情链接字数限制与链接个数限制的问题!织梦网站非常适合网站搭建以及网站优化,而友情链接是做优化必不可少的模块,我们经常搭建织梦网站发现织梦系统的友情链接模板有时候会限制字数不显示以 ...

  4. Java——API文档

    Sun下载JDK--解压缩--javadoc文件(Constuctor Summary[构造方法]+Method Summary[方法])   [Object]   Object类是所有Java类的根 ...

  5. SQL GROUP BY两个列

    首先group by 的简单说明: group by 一般和聚合函数一起使用才有意义,比如 count sum avg等,使用group by的两个要素:   (1) 出现在select后面的字段 要 ...

  6. Python学习笔记(二)Sublime Text 3 安装Package Control

    原来Subl3安装Package Control很麻烦,现在简单的方法来了 一.简单的安装方法 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码: ...

  7. WinForm实现最小化右下角

    首先,要在窗体里面加入这么两个控件,左边的是托盘控件,右边的是菜单控件. 然后设置窗体的FormClosing事件: if (e.CloseReason == CloseReason.UserClos ...

  8. LNMP 搭建 wordpress 站点 安装及配置过程

    0x00 环境 阿里云ECS云服务器 CPU:1核 内存:4G 操作系统:Centos 系统盘:100G 0x01 安装及配置 主要使用 nginx . php 和 mysql 注意:如果下面的设置不 ...

  9. PHP图片处理

    开启GD扩展(php_gd2.dll) 创建画布 画布:一种资源型数据,可以操作的图像资源. 创建新画布(新建) ImageCreate(宽,高);创建基于调色板的画布. imageCreateTru ...

  10. MongoDB--(NoSQL)入门介绍

    NoSQL中比较优秀的产品 windows 下载安装 shell 基本操作(javascript 引擎) BSON扩充的数据类型(JSON的扩展,浮点型,日期型的扩充) step1.创建数据库 use ...