ABC 226 Contest E - Just one 解题报告

两种做法,dsu并查集或者dfs深搜
关键是找环,细节看注释
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
//================================
#define debug(a) cout << #a": " << a << endl;
#define rep(i, ll,rr) for(int i = ll; i <= rr; ++i)
#define N 200010
//================================
typedef pair<int,int> pii;
#define x first
#define y second
typedef long long LL; typedef unsigned long long ULL; typedef long double LD;
inline LL read() { LL s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(LL x, int op = 10) { if (!x) { putchar('0'); if (op)  putchar(op); return; }  char F[40]; LL tmp = x > 0 ? x : -x; if (x < 0)putchar('-');  int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]);    if (op) putchar(op); }
LL fpower(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; } LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
//================================= 
/*
ABC F 解题报告
我们很容易发现,这道题就是在图中找环,每有一个环,方案总数就乘2,
即环上正向算一次,反向算一次,因此本题的关键就是找环
1.使用并查集找环(这个很简洁)
2.dfs找环(基环树/仙人掌 找环那种方式)
*/
const int mod = 998244353;
LL n,m;
//=================================并查集做法
int fa[N],edge[N],node[N]; 
int find(int x){return x == fa[x] ? fa[x] : fa[x] = find(fa[x]);}

void dsu_solve(){
	n = read() , m = read() ;
	rep(i,1,n) fa[i]=i,node[i]=1;
	rep(i,1,m){
		int a=read() ,b =read();
		int pa = find(a) , pb = find(b);
		if(pa == pb) edge[pa] ++;
		else{
			fa[pa] = pb;
			edge[pb] += edge[pa]+1;
			node[pb] += node[pa];
		}
	}
	bool flag = true;LL cnt = 0;
	rep(i,1,n) if(i == fa[i]) if(edge[i]!=node[i]) flag = false;
	else cnt++;
		
	if(!flag) {print(0);return ;}
	print(fpower(2ll,cnt,mod));
}
//=================================dfs做法
int e[N*2],ne[N*2],h[N],fu[N],idx=0;
int st[N],ins[N],flag=true;
LL ans = 0,cir;

void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;} 

void dfs(int u,int pre){
	if(!flag) return ;
	
	st[u] = 1 , ins[u] = 1;
	
	for(int i=h[u];~i;i=ne[i]){
		int j = e[i];
		if(j == pre) continue;
		
		if(!ins[j]) dfs(j,u);
		else if(ins[j] == 1){
			cir ++;
		}
	}
	ins[u] = 2;
}

void dfs_solve(){
	memset(h,-1,sizeof h);
	n = read() , m = read();
	rep(i,1,m){
		int a = read() , b = read();
		add(a,b) , add(b,a);
	}
	rep(i,1,n) if(!st[i]){ 
		cir = 0;
		ans ++;
		dfs(i,-1);
		if(cir != 1) flag = false;
	}
	if(!flag) {print(0);return ;}
	print(fpower(2ll,ans,mod));
}

//=================================
int main(){
	dsu_solve();
	// dfs_solve();  
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值