HDU - 6736 F - Forest Program
題意
給你n個點m條邊,並且保證整個圖是仙人掌。
仙人掌:每條邊僅屬於1條或者0條迴路
且無重邊和自環
讓你刪掉一些邊使其變成一棵樹(擁有點數-1條邊)
注意一個點也是森林
圖可能是不聯通的
思路
考慮環,顯然一個環可以隨便去掉幾條邊但是至少一條(也就是說不能是 C n 0 C_n^0 Cn0) 2 x 2^{x} 2x-1,然後考慮非環那麼共有m-(所有環的邊數),然後可以隨便去除邊共 2 m − c n t 2^{m-cnt} 2m−cnt
在找環時,求環的邊數見 d f s dfs dfs
#include<iostream>
#include<algorithm>
using namespace std;
#define int long long
const int maxn=3e5+10;
int dep[maxn],cnt;
vector<int>edge[maxn];
const int mod=998244353;
int ans;
int qp(int a,int b) {
int res=1;
while(b) {
if(b&1)res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
void dfs(int u,int fa) {
dep[u]=dep[fa]+1;
for(int i=0; i<edge[u].size(); ++i) {
int v=edge[u][i];
if(v==fa)continue;
if(!dep[v])dfs(v,u);
else if(dep[u]>dep[v]){
ans=(ans%mod*(qp(2,dep[u]-dep[v]+1)-1)%mod)%mod;
cnt+=dep[u]-dep[v]+1;
}
}
}
signed main() {
int n,m;
while(~scanf("%lld%lld",&n,&m)) {
cnt=0;
for(int i=1; i<=n; ++i)dep[i]=0,edge[i].clear();
for(int i=1; i<=m; ++i) {
int u,v;
scanf("%lld%lld",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
ans=1;
for(int i=1; i<=n; ++i) {
if(!dep[i])dfs(i,0);
}
ans=(ans%mod*(qp(2,m-cnt))%mod)%mod;
printf("%lld\n",ans);
}
}
相關文章
- HDU4941Magical Forest(map)REST
- HDU 2582 f(n) (組合數的gcd)GC
- 【ABAP】FTP ProgramFTP
- 2013成都站F題||hdu4786 並查集 生成樹並查集
- Machine Learning(13)- Random ForestMacrandomREST
- YT14-HDU-三分查詢求F(x)的最小值
- Spark Driver Program剖析Spark
- Registering a program on the GatewayGateway
- Run a program as a service (daemon)-GOGo
- Program Units and Referenced Objects (244)Object
- Overview of the Program Global Areas (97)View
- Program Interface Structure (135)Struct
- pwn.college-Program Misuse
- 1118. Birds in Forest (25)REST
- SAP APP: Automatic Payment ProgramAPP
- Program Execution 程式的執行
- Program perl 資料結構資料結構
- Individual Project - Word frequency programProject
- oracle concurrent program session and sqlOracleSessionSQL
- Oracle Scheduler(5)job呼叫programOracle
- The Network Program Log Two (Scapy)
- Profiling an Assembly Program
- pwn.college Fundementals Program interaction
- 第九篇:隨機森林(Random Forest)隨機森林randomREST
- 物件啟用用得到的Program物件
- Could not find the main class. Program will exitAI
- Record for Individual Project ( Word frequency program )Project
- the program list about oracle database in SAP systemOracleDatabase
- Segmentation of retinal OCT images using a random forest classifierSegmentationrandomREST
- CF1092E Minimal Diameter ForestREST
- Quartz.net-控制檯例項-Programquartz
- Apple Developer Program全球電話支援APPDeveloper
- Autodesk Cloud Accelerator Program 開始報名Cloud
- Oracle Scheduler(4)job呼叫program和scheduleOracle
- Compilation of Views and PL/SQL Program Units (242)ViewSQL
- PL/SQL Program Units and the Shared Pool (89)SQL
- 白話異常檢測演算法Isolation Forest演算法REST
- ML《決策樹(四)Bagging 和 Random Forest》randomREST