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
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.
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.
1 2
2 3
3 1
6 6
1 2
2 3
3 1
2 4
4 5
5 2
49
//#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找环的更多相关文章
- # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)
「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...
- 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 ...
- CodeForces - 103B(思维+dfs找环)
题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- 与图论的邂逅06:dfs找环
当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...
- HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)
求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...
- UVaLive 6950 && Gym 100299K Digraphs (DFS找环或者是找最长链)
题意:有n个只包含两个字母的字符串, 要求构造一个m*m的字母矩阵, 使得矩阵的每行每列都不包含所给的字符串, m要尽量大, 如果大于20的话构造20*20的矩阵就行了. 析:开始吧,并没有读对题意, ...
- [NOI2008]假面舞会——数论+dfs找环
原题戳这里 思路 分三种情况讨论: 1.有环 那显然是对于环长取个\(gcd\) 2.有类环 也就是这种情况 1→2→3→4→5→6→7,1→8→9→7 假设第一条链的长度为\(l_1\),第二条为\ ...
- [蓝桥杯2018初赛]小朋友崇拜圈(dfs找环)
传送门 思路: 题意大意:n条有向边,找出最大环. 我们发现,如果一个小朋友没有被任何人崇拜,那么他一定不位于环中.为此我们可以设置一个indug数组预处理.如果2被崇拜了那么indug[2]就加加, ...
- New Reform---cf659E(dfs找环)
题目链接:http://codeforces.com/problemset/problem/659/E 给你n个点,m条双向边,然后让你把这些边变成有向边,使得最后的图中入度为0的点的个数最少,求最少 ...
随机推荐
- 转 git 本地文件添加远程git
好的博客膜拜一下 https://www.liaoxuefeng.com/wiki/896043488029600/898732864121440 现在的情景是,你已经在本地创建了一个Git仓库后,又 ...
- spring security基本知识(四) WebSecurity
1.创建一个Filter 现在web.xml文档中声明一个filter class="org".springframework.web.filter.DelegatingFil ...
- 1.Linux命令行快捷键、Vim
1. 命令终端的快捷键使用 ctrl+b 左移光标 ctrl+f 右移光标 ctrl+u 删除光标左边的内容 ctrl+k 删除光标右边的内容 ctrl+w 删除光标前的一个单词 =esc+ctrl+ ...
- flex的12个属性
容器(父元素)的属性: flex-direction属性决定主轴的方向 flex-wrap 属性决定项目在一行排不下的情况下是否换行 flex-flow flex-flow属性是flex-direct ...
- 1. Spring Security 框架简介
官网:https://projects.spring.io/spring-security/Spring Security 是强大的,且容易定制的实现认证,与授权的基于 Spring 开发的框架.Sp ...
- 回声状态网络ESN(Echo State Networks)
1.1 网络结构 ESN通过随机地部署大规模系数链接的神经元构成网络隐层,一般称为"储备池".ESN网络具有的特点如下: (1)包含数目相对较多的神经元: (2)神经元之间的连接关 ...
- jQuery插件simplePagination的使用-踩坑记_03
jQuery插件simplePagination的使用 正在熟悉项目上的代码,新添加了一个需要,需要对表单进行分页,之前的代码中是有分页的代码的,看了老半天,也没看太明白.之前的项目比较久远,继续熟悉 ...
- python中的实例属性和类属性
在python中,类属性和实例属性的区别是什么? 我认为是作用域的不同,实例对象可以访问类属性,类对象不可以访问实例属性.(类的概念本身就是作用域的概念,你不能让一只猫会飞,猫属于猫类,这一类都不会飞 ...
- Linux下查看分区内目录及文件占用空间容量
转载linux下使用 du查看某个文件或目录占用磁盘空间的大小 du -ah --max-depth=1 这个是我想要的结果 a显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘 ...
- 新上线MySQL数据库规划
新上线MySQL数据库规划1.删除test库2.删除root用户或者让root用户只可在本机登陆而对于有业务访问的数据库,在做删除root用户前需要依次确认 function.procedure.ev ...