Educational Codeforces Round 56 D - Beautiful Graph
题目大意:
在给定的一个图中(可能不连通)
给每个点赋值1、2、3 使得一条边上的两个端点点权相加为奇数
求方案数
一条满足条件的路径上的点权必为一奇一偶交替
偶数只有2 奇数有1、3
若位于1、3、5、.... 的点有x1个 位于2、4、6、... 的点有x0个
那么一条路径的方案数为 2^x1+2^x0 (x1的点作为奇数点的方案+x0的点作为奇数点的方案)
当图不连通 存在多个子图 那么每个子图的方案数相乘 就是总的方案数
当图中存在环时 若环上的点数为偶数则同样满足上式 但为奇数则整条路径不可能有解
#include <bits/stdc++.h>
#define LL long long
#define mod 998244353
using namespace std;
int n,m;
const int N=3e5+;
vector <int> e[N];
bool vis[N], NO;
int col[N], m0, m1;
LL p[N];
void init() {
p[]=1LL;
for(int i=;i<N;i++)
p[i]=p[i-]*2LL%mod;
}
void dfs(int u,int c) {
if(NO) return;
col[u]=c;
if(col[u]) m1++;
else m0++;
for(int i=;i<e[u].size();i++) {
int v=e[u][i];
if(col[v]==-) dfs(v,c^);
else if(col[v]==col[u]) {
NO=; return; // 环上的点数为奇数个
}
}
}
int main()
{
init();
int t; scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
e[i].clear(), col[i]=-;
for(int i=;i<m;i++) {
int u,v; scanf("%d%d",&u,&v);
e[u].push_back(v), e[v].push_back(u);
}
LL ans=1LL; NO=;
for(int i=;i<=n;i++)
if(col[i]==-) {
m0=m1=; dfs(i,);
if(NO) break;
ans=(p[m0]+p[m1])%mod*ans%mod;
}
if(NO) printf("0\n");
else printf("%I64d\n",ans);
} return ;
}
Educational Codeforces Round 56 D - Beautiful Graph的更多相关文章
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- 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的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...
- Educational Codeforces Round 56 (Rated for Div. 2)
涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...
- Educational Codeforces Round 56 Solution
A. Dice Rolling 签到. #include <bits/stdc++.h> using namespace std; int t, n; int main() { scanf ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)
题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
随机推荐
- Android Studio3.3中Cannot resolve symbol ActivityTestRule
最近在看<Android编程权威指南>,在Windows10下安装了Android Studio3.3,边看编练习书中的例子程序,看到第21章""音频播放与单元测试&q ...
- nmon 定时任务 监控资源
nmon命令: # ./nmon –f -s 30 –c 100 说明:-f 以文件的形式输出,默认输出是机器名+日期.nmon的格式,也可以用-F指定输出的文件名,例如: # ./nmon_x8 ...
- 实用maven笔记四-打包&其他
通过使用maven的生命周期和丰富多样的插件,可以方便的将项目代码编译打包为自己需要的构件. maven默认项目主代码位置src/main/java目录,测试代码位置src/test/java目录.主 ...
- 微信小程序 使用wxParse解析html
微信小程序 加载 HTML 标签:https://blog.csdn.net/zclengendary/article/details/54312030 微信小程序 使用wxParse解析html:h ...
- Python判断一个字符串中是否存在多个子串中的一个
在使用python的开发过程中,常常需要判断,字符串中是否存在子串的问题, 但判断一个字符串中是否存在多个字串中的一个时,如if (a or b) in c或者if x contains a|b|c| ...
- Joda-Time 入门
Joda-Time 令时间和日期值变得易于管理.操作和理解.事实上,易于使用是 Joda 的主要设计目标.其他目标包括可扩展性.完整的特性集以及对多种日历系统的支持.并且 Joda 与 JDK 是百分 ...
- Hibernate4教程二:基本配置(3)
被映射的类必须定义对应数据库表主键字段.大多数类有一个JavaBeans风格的属性, 为每一个实例包含唯一的标识.<id> 元素定义了该属性到数据库表主键字段的映射. java代码: &l ...
- go中的string操作
strings 判断字符串s是否以prefix开头 strings.HasPrefix(s string,preffix string) bool: 判断字符串s是否以suffix结尾 stirngs ...
- check all tables rows
select TABLE_NAME,NUM_ROWS from all_tables where OWNER='xx' order by NUM_ROWS desc;
- mongodb的学习 (1)
1. 启动服务 (1)在D盘下新建mongodb 文件夹,在mongodb 文件夹里面新建db; (2)win+R 启动cmd ,输入 `mongod --dbpath "D:/mongod ...