Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)

题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模.
题解:要使得每个点所连的奇偶不同,很明显是二分图染色,那么对于某一个联通块,我们可以对左边的点赋\(2\),右边的点赋\({1,3}\),那么左边的点没有选择,只有一种情况,而右边的点每个点可以有两种情况,如果右边的点数是\(k_2\),那么方案数就是\(2^{k_2}\),如果左边赋\({1,3}\)且点数为\(k_1\),右边赋\(2\),方案数就是\(2^{k_1}\),所以一个联通块的方案数就是\(2^{k_1}+2^{k_2}\).我们可以不考虑点权,直接dfs二分图染色,记录二分图两边的点数,再直接贡献给答案即可.
代码:
int t;
int n,m;
vector<int> v[N];
int color[N];
int g[N];
int cnt[N];
int ans; bool dfs(int x,int c){
color[x]=c; for(auto w:v[x]){
if(!color[w]){
if(!dfs(w,3-c)) return false;
}
else{
if(color[w]==c) return false;
}
}
cnt[c]++;
return true;
} int main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
scanf("%d",&t);
g[0]=1; for(int i=1;i<=300010;++i){
g[i]=g[i-1]*2%mod;
} while(t--){
scanf("%d %d",&n,&m); ans=1; for(int i=1;i<=n;++i){
color[i]=0;
} for(int i=1;i<=m;++i){
int a,b;
scanf("%d %d",&a,&b);
v[a].pb(b);
v[b].pb(a);
} bool ok=true; for(int i=1;i<=n;++i){
if(!color[i]){
cnt[1]=cnt[2]=0;
if(!dfs(i,1)){
ok=false;
break;
}
else{
ans=(ll)ans*(g[cnt[1]]+g[cnt[2]])%mod;
}
}
} if(ok) printf("%d\n",ans);
else printf("0\n"); for(int i=1;i<=n;++i) v[i].clear(); } return 0;
}
Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)的更多相关文章
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】
传送门:http://codeforces.com/contest/1093/problem/D D. Beautiful Graph time limit per test 2 seconds me ...
- Educational Codeforces Round 56 (Rated for Div. 2) ABCD
题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D
给你一个无向图 以及点的个数和边 每个节点只能用1 2 3 三个数字 求相邻 两个节点和为奇数 能否构成以及有多少种构成方法 #include<bits/stdc++.h> usin ...
- Educational Codeforces Round 56 (Rated for Div. 2)
涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...
- Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题
F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...
- Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)
题意和分析在之前的链接中有:https://www.cnblogs.com/pkgunboat/p/10160741.html 之前补题用三维偏序的cdq的分治A了这道题,但是感觉就算比赛再次遇到类似 ...
- Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array
题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array (简单DP)
题目:https://codeforces.com/contest/1155/problem/D 题意:给你n,x,一个n个数的序列,你可以选择一段区间,区间的数都乘以x,然后求出最大字段和 思路: ...
随机推荐
- selenium爬虫 | 爬取疫情实时动态
import csvimport selenium.webdriverfrom selenium.webdriver.chrome.options import Optionsclass spider ...
- 文件监控性能问题【BUG】
文件监控性能问题[BUG] 背景:JAVA写了一个文件夹目录监控的程序,使用的是org.apache.commons.io.monitor 包,项目稳定运行了一个月,现场反馈,文件夹数据处理越来越慢, ...
- Sentry(v20.12.1) K8S 云原生架构探索,JavaScript 性能监控之管理 Transactions
系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...
- 【Android】报错 Please ensure Hyper-V is disabled in Windows Features, or refer to the Intel HAXM 的解决方案
参考文章 实测华为锐龙本(adm yes)安装Android avd虚拟机教程 环境 Android Studio 3.6; Windows 1909; AMD Ryzen 4800U with Ra ...
- FLask之视图
视图 1 FBV def index(): return render_template('index.html') app.add_url_rule('/index', 'index', index ...
- C++ unordered_map/unordered_set 自定义键类型
1. unordered_map 和 unordered_set template < class Key, // unordered_map::key_type class T, // uno ...
- 源码 redis 分布式锁
https://github.com/SPSCommerce/redlock-py/tree/master/redlock
- vue-router实现路由懒加载( 动态加载路由 )
三种方式第一种:vue异步组件技术 ==== 异步加载,vue-router配置路由 , 使用vue的异步组件技术 , 可以实现按需加载 .但是,这种情况下一个组件生成一个js文件.第二种:路由懒加载 ...
- vue组件中data为什么必须是一个函数?
因为JavaScript的特性所导致,在component中,data必须以函数的形式存在,不可以是对象. 组建中的data写成一个函数,数据以函数返回值的形式定义,这样每次复用组件的时候,都会返回一 ...
- JetBrains(IEDA、CLion、Pycharm) 学生获得免费使用资格
JetBrains针对学生推出了免费使用资格,但是很多同学却不知道或者说不知道怎样获得免费资格,只能千辛万苦的去寻找破解密钥,但现在JetBrains针对盗版的封锁越来越严密,很多密钥过一段时间就会失 ...