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的点的个数最少,求最少 ...
随机推荐
- ubuntu16.04 安装samba
安装samba 1.更新当前软件 sudo apt-get upgrade sudo apt-get update sudo apt-get dist-upgrade 2.执行 sudo apt-ge ...
- MongoDB的安装以及启动
1.首先什么是MongoDB? MongoDB是一个基于分布式文件存储的数据库,是由c++语言编写的.为web应用提供可扩展的高性能数据的存储方案.是一个介于关系型数据库和非关系型数据库 的中间产品, ...
- 【LuoguP4770】[NOI2018] 你的名字
题目链接 题意简述 给定一个串 \(S\) 多组询问 , 每次给定一个串 \(T\) 和一个 区间 \([l,r]\) 求串\(T\) 有多少个本质不同的子串 满足不是 \(S[l...r]\) 的子 ...
- POJ-2516-Minimum Cost(网络流, 最小费用最大流)
链接: https://vjudge.net/problem/POJ-2516 题意: Dearboy, a goods victualer, now comes to a big problem, ...
- 【Heaven Cow与God Bull】题解
题目 Description __int64 ago,there's a heaven cow called sjy... A god bull named wzc fell in love with ...
- CSS3——制作人物走路的小动画
一个很简单的小动画,但是还挺有意思的,就是找这种图片很麻烦,我这里把我找的一张图片贴上来,这张图片是我在网上找的,又改了背景色和大小. <!DOCTYPE html> <html l ...
- HRESULT: 0x80040228
When run the arcgis engine codes in the console application, may come to the exception: HRESULT: 0x8 ...
- (容量超大)or(容量及价值)超大背包问题 ( 折半枚举 || 改变 dp 意义 )
题意 : 以下两个问题的物品都只能取有且只有一次 ① 给你 N 个物品,所有物品的价值总和不会超过 5000, 单个物品的价格就可达 10^10 ,背包容量为 B ② 给你 N (N ≤ 40 ) 个 ...
- Maven的settings.xml配置详解
子节点详细介绍转载:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension 全局配置 ...
- H. The Game of Life
题目链接:http://exam.upc.edu.cn/problem.php?id=5206 题意:邻居为八个方向.若一个活人有2或3个邻居,遗传一代,否则死亡:若一个死人有3个邻居,则下一代复活. ...