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. BSOJ5458 [NOI2018模拟5]三角剖分Bsh 分治最短路

    题意简述 给定一个正\(n\)边形及其三角剖分,每条边的长度为\(1\),给你\(q\)组询问,每次询问给定两个点\(x_i\)至\(y_i\)的最短距离. 做法 显然正多边形的三角剖分是一个平面图, ...

  2. SpringBoot中资源初始化加载的几种方式

    一.问题 在平时的业务模块开发过程中,难免会需要做一些全局的任务.缓存.线程等等的初始化工作,那么如何解决这个问题呢?方法有多种,但具体又要怎么选择呢? 二.资源初始化 1.既然要做资源的初始化,那么 ...

  3. 0-4评价一个语言模型Evaluating Language Models:Perplexity

    有了一个语言模型,就要判断这个模型的好坏. 现在假设: 我们有一些测试数据,test data.测试数据中有m个句子;s1,s2,s3-,sm 我们可以查看在某个模型下面的概率: 我们也知道,如果计算 ...

  4. Activiti7入门(五)

    1 创建流程 首先选中存放图形的目录(本次我们选择 resources 下的 bpmn 目录),点击菜单: New-BpmnFile,如下图所示: 起完名字 holiday 后(默认扩展名为 bpmn ...

  5. php开发IDE选择

    优先选择Netbeans,理由如下:: 1.ZendStudio有的方便特性Netbeans也提供,如:ctrl+f5也支持ctrl+shift+r的文件选择功能,[git | svn]团队代码管理. ...

  6. es之java分页操作

    按照一般的查询流程来说,如果我想查询前10条数据: · 1 客户端请求发给某个节点 · 2 节点转发给个个分片,查询每个分片上的前10条 · 3 结果返回给节点,整合数据,提取前10条 · 4 返回给 ...

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

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

  8. Spring整合Hibernate实现Spring Data JPA (介绍和使用)

    Spring Data JPA是Spring基于Hibernate开发的一个JPA框架.如果用过Hibernate或者MyBatis的话,就会知道对象关系映射(ORM)框架有多么方便. 但是Sprin ...

  9. cefsharp 在高DPI下闪烁的问题

    今天有客户朋友说程序在他的surface下界面很闪烁,搜索了相关的资料,初步判定是DPI引起的问题,但也有可能是cefsharp 51版本在WIN10上面没有禁用GPU加速,苦于没有环境测试,所以抱着 ...

  10. 玩转visual studio系列之类设计图

    类设计图 vs 类设计图 微软  安装好vs2017后,在打开的IDE中,随便选择一个文件夹创建[类图],却发现没有该选项 类图辅助器 解决方案 1.点击[工具]选项卡,在下拉的菜单中选择第一项 获取 ...