队友过的:https://blog.csdn.net/liufengwei1/article/details/101632506

Forest Program

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 124    Accepted Submission(s): 47

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

题解:

找出所有环,每个环至少选择一条边删掉,那么方案数就是2^size-1,不在环上的边为m条,可以随便删,方案数就是2^resm。

点双抄一遍就过了,也可以直接dfs

 
参考代码:
#include<bits/stdc++.h>
#define maxl 500010
using namespace std; const int mod=; int n,m,top,cnt,ind,sum,rt,dcccnt;
vector <int> dcc[maxl];
long long ans;
int dfn[maxl],low[maxl],ehead[maxl],s[maxl];
long long num[maxl];
bool in[maxl],cut[maxl];
struct ed
{
int to,nxt;
}e[maxl<<]; inline void add(int u,int v)
{
e[++cnt].to=v;e[cnt].nxt=ehead[u];ehead[u]=cnt;
} inline void tarjan(int u)
{
dfn[u]=low[u]=++ind;s[++top]=u;
if(u==rt && ehead[u]==)
{
dcc[++dcccnt].push_back(u);
return;
}
int son=,v;
for(int i=ehead[u];i;i=e[i].nxt)
{
v=e[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
{
son++;
if(u!=rt || son>)
cut[u]=true;
dcccnt++;
int d;
do
{
d=s[top--];
dcc[dcccnt].push_back(d);
}while(d!=v);
dcc[dcccnt].push_back(u);
}
}
else
low[u]=min(low[u],dfn[v]);
}
} inline void prework()
{
for(int i=;i<=dcccnt;i++)
dcc[i].clear();
dcccnt=;
for(int i=;i<=n;i++)
{
dfn[i]=low[i]=;in[i]=false;
ehead[i]=;cut[i]=false;
}
int u,v;cnt=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
ind=;
for(int i=;i<=n;i++)
if(dfn[i]==)
{
rt=i;top=;
tarjan(i);
}
} inline void mainwork()
{
int resm=m;
ans=;
for(int i=;i<=dcccnt;i++)
{
sum=dcc[i].size();
if(sum>=)
ans=ans*num[sum]%mod,resm-=sum;
}
ans=ans*(num[resm]+)%mod;
} inline void print()
{
printf("%lld\n",ans);
} int main()
{
//freopen("1006.in","r",stdin);
num[]=;
for(int i=;i<maxl;i++)
num[i]=2ll*num[i-]%mod;
for(int i=;i<maxl;i++)
num[i]=((num[i]-)%mod+mod)%mod;
while(~scanf("%d%d",&n,&m))
{
prework();
mainwork();
print();
}
return ;
}
 
 

2019CCPC秦皇岛 F Forest Program的更多相关文章

  1. [CCPC2019秦皇岛] F. Forest Program

    [CCPC2019秦皇岛 F] Link https://codeforces.com/gym/102361/problem/F Description 给定一个仙人掌,删去一些边可以让它变成一个森林 ...

  2. Forest Program(2019ccpc秦皇岛F)

    题:http://acm.hdu.edu.cn/showproblem.php?pid=6736 题意:删掉一些边使得图不存在点双,求方案数. 分析:若一条边不属于点双,那么这条边有删和不删俩种选择, ...

  3. HDU6736 2019CCPC秦皇岛赛区 F. Forest Program

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6736思路:dfs+栈 判环           设图中环的大小分别为 c1, c2, ..., ck,不属 ...

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

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

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

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

  6. HDU - 6736 F - Forest Program

    题意 给你n个点m条边,并且保证整个图是仙人掌. 仙人掌:每条边仅属于1条或者0条回路 且无重边和自环 让你删掉一些边使其变成一棵树(拥有点数-1条边) 注意一个点也是森林 图可能是不联通的 思路 考 ...

  7. 2019-ccpc秦皇岛现场赛

    https://www.cnblogs.com/31415926535x/p/11625462.html 昨天和队友模拟了下今年秦皇岛的区域赛,,,(我全程在演 题目链接 D - Decimal 签到 ...

  8. 2019CCPC秦皇岛赛区(重现赛)- F

    链接: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1006&cid=872 题意: Z 国近年来一直在考虑遏制国土沙 ...

  9. 2019CCPC秦皇岛 E题 Escape(网络流)

    Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

随机推荐

  1. docker showdoc安装

    自动脚本安装 前言 自动脚本脚本利用docker来安装运行环境,适用于linux服务器.如果你的服务器没有docker服务,脚本会尝试安装之.安装docker的过程可能有些慢.如果你已经安装过dock ...

  2. NioEventLoop的创建

    NioEventLoop的创建 NioEventLoop是netty及其重要的组成部件,它的首要职责就是为注册在它上的channels服务,发现这些channels上发生的新连接.读写等I/O事件,然 ...

  3. 力扣(LeetCode)种花问题 个人题解

    假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...

  4. React动画库

    npm i react-transition --save import {CSSTransition} from 'react-transition-group'

  5. vant-ui的van-area使用

    由于官方例子中并没有太多详情,因此记录之,方便以后使用. 1.配置 :area-list="areaList",以初始化全部省市区的数据,其中area.js文件在官方可以下载,放于 ...

  6. 【集合系列】- 深入浅出的分析IdentityHashMap

    一.摘要 在集合系列的第一章,咱们了解到,Map 的实现类有 HashMap.LinkedHashMap.TreeMap.IdentityHashMap.WeakHashMap.Hashtable.P ...

  7. 20191010-6 alpha week 1/2 Scrum立会报告+燃尽图 04

    此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/8749] 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 ...

  8. 【NHOI2018】扑克游戏

    [问题描述] 有一种别样“小猫钓鱼”扑克游戏.有 N 张牌,每张牌都有一个花色和点数.游戏的规则:扑克接龙时,若前面有同样花色的牌,你可以将这两张牌连同之间的牌都取走,得到的分值为取走牌点数之和.这里 ...

  9. Java通过 Scanner 类来获取用户的输入

    通过 Scanner 类来获取用户的输入. import java.util.Scanner; Scanner s = new Scanner(System.in);// 从键盘接收数据  Syste ...

  10. Kotlin实战案例:带你实现RecyclerView分页查询功能(仿照主流电商APP,可切换列表和网格效果)

    随着Kotlin的推广,一些国内公司的安卓项目开发,已经从Java完全切成Kotlin了.虽然Kotlin在各类编程语言中的排名比较靠后(据TIOBE发布了 19 年 8 月份的编程语言排行榜,Kot ...