2019CCPC秦皇島 F-Forest Program(DFS)(快速冪)

題幹:

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

思路:

題目給了我們n個點,m條邊,樹的定義爲無向無環連通圖,要求去掉一部分邊,使圖中出現森林(一顆及以上的樹),輸入沒有自環,求有多少種可以構成森林的方式。
首先,如果已經是環的圖,去掉任意條邊都可以構成森林(除了全保留),也就是說如果一個環有三條邊則可以有2312^3-1種構造方法。
然後考慮圖中不構成環的散邊,因爲不構成環,即使保留這條邊也依然可以看作一棵樹,去掉的話作爲孤立點也是一顆樹,所以對於所有散邊都有保留和去掉兩種選擇,就有2y2^y種構造方法(y爲散邊數量)。
則答案爲 (2zix2^{zi^x} -1) * 2y2^y(x爲環的數量,zi爲第i個環中邊的數量,y爲散邊數量)
然後問題就轉換成了求在環中邊的數量,因爲已知總邊數,則散邊可以用總邊數-環的邊數求得。
可以使用較爲簡單的DFS去判環,對每個點打上一個時間戳vis,並且記錄該點的上一個點pre;如果某個點的下一個點已經被搜過了且時間戳早於當前點,則說明已經產生了環,則我們根據pre就可以找到這個環(因爲已經成環了,所以邊數和節點數是相同的)。
然後因爲牽扯到了指數和取模,所有記得用快速冪。

#include <bits/stdc++.h>
using namespace std;
int vis[300010],tim,pre[300010];
struct stu{
	int v;
};
vector<stu> e[300010];
vector<int> temp;
void add(int a,int b){
	stu t;
	t.v=b;
	e[a].push_back(t);
}
void dfs(int u){
	vis[u]=++tim;
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i].v;
		if(v==pre[u])	continue;
		if(!vis[v]){
			pre[v]=u;
			dfs(v);
		}
		else if(vis[v]>vis[u]){
			int t=v;
			int now=0;
			while(t!=u){
				now++;
				t=pre[t];
			}
			now++;
			temp.push_back(now);
		}
	}
}
long long pw(long long a,long long b){
	long long ans=1;
	while(b){
		if(b&1)
			ans=(ans*a)%998244353;
		a=(a*a)%998244353;
		b>>=1;
	}
	return ans%998244353;
}
int main()
{
	long long ans=1;
	int n,m,u,v;
	scanf("%d%d",&n,&m);
	ans=1;
	for(int i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		add(u,v);
		add(v,u);
	}
	for(int i=1;i<=n;i++){
		if(!vis[i])
			dfs(i);
	}
	for(int i=0;i<temp.size();i++){
		int v=temp[i];
		ans=ans*(pw(2,v)-1)%998244353;
		m-=v;
	}
	ans=ans*(long long)(pw(2,m)%998244353);
	printf("%lld\n",ans%998244353);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章