Codeforces 1093D(染色+组合数学)
题面
题目大意:给出一个无向图,每个节点可以填1,2,3三个数中的一个
问有多少种填数方案,使两个相邻节点的数之和为奇数
分析
如果图中有奇环,一定无解
我们对图黑白染色,由于图可能不联通,记第i个连通分量的黑点数量为\(b_i\),白点数量为\(w_i\)
观察发现每一条边的连接的两个节点,一个是2,另一个是1或3
显然要不黑点全部填2,要不白点全部填2
若黑点填2,则剩下的白点有\(2^{w_i}\) 种填法
若白点填2,则剩下的黑点有\(2^{b_i}\) 种填法
总答案为:
\]
有两个小坑:
1.多组样例,邻接表记得清空
2.记录颜色数组用for循环初始化,不要用memset,因为数组中非0的数可能很少。memset会访问整个数组,导致TLE
代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define maxn 300005
#define mod 998244353
using namespace std;
inline long long fast_pow(long long x,long long k){
long long ans=1;
while(k){
if(k&1) ans=ans*x%mod;
x=x*x%mod;
k>>=1;
}
return ans;
}
int t,n,m;
vector<int>E[maxn];
int color[maxn];
int cnt0,cnt1;
bool flag=true;
void dfs(int x,int c){
if(c==1) cnt0++;
if(c==2) cnt1++;
color[x]=c;
for(auto y : E[x]){
if(color[y]==0) dfs(y,3-c);
else if(color[y]==c){
flag=false;
return;
}
}
}
void ini(){
for(int i=1;i<=n;i++) E[i].clear();
// memset(color,0,sizeof(color));
for(int i=1;i<=n;i++) color[i]=0;
}
int main(){
int u,v;
scanf("%d",&t);
for(int k=1;k<=t;k++){
scanf("%d %d",&n,&m);
ini();
for(int i=1;i<=m;i++){
scanf("%d %d",&u,&v);
E[u].push_back(v);
E[v].push_back(u);
}
flag=true;
long long ans=1;
for(int i=1;i<=n;i++){
if(!color[i]){
cnt0=cnt1=0;
dfs(i,1);
ans=ans*(fast_pow(2,cnt0)%mod+fast_pow(2,cnt1)%mod)%mod;
if(flag==false) break;
}
}
if(flag==false){
printf("0\n");
}else{
printf("%I64d\n",ans);
}
}
}
Codeforces 1093D(染色+组合数学)的更多相关文章
- D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...
- Codeforces 1093D Beautiful Graph(二分图染色+计数)
题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...
- Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】
<题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共 ...
- Colorful Bricks CodeForces - 1081C ( 组合数学 或 DP )
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in t ...
- Codeforces 15E Triangles - 组合数学
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby fores ...
- CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)
题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数. 思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色. 这里用的并查集(还可以带权并查集优化一下,或 ...
- Mysterious Crime CodeForces - 1043D (思维+组合数学)
Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody ...
- codeforces 630H (组合数学)
H - Benches Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- Educational Codeforces Round 32 Almost Identity Permutations CodeForces - 888D (组合数学)
A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in thi ...
随机推荐
- Linux基于Hadoop2.8.0集群安装配置Hive2.1.1及基础操作
前言 安装Apache Hive前提是要先安装hadoop集群,并且hive只需要在hadoop的namenode节点集群里安装即可,安装前需保证Hadoop已启(动文中用到了hadoop的hdfs命 ...
- 省电的iPhone定位
1.Getting the User’s Current Location 获取用户当前位置. 获取位置的方式有三种:GPS, cell tower triangulation(蜂窝站点), 和 Wi ...
- 一、C++类库与C#类库相互调用
1.C++调用C#类库 1.准备C#类库(dll文件) 1.1.创建C#类库: 右击项目类库生成即可, 出现.dll(类库)与.pdb(pdb文件包含了编译后程序指向源代码的位置信息, 用于调试的时候 ...
- JSON.stringify常见用法
转摘于其他博客 var data =[ { name: "金",sex:"1",age:26 }, { name: "才",sex:&quo ...
- php内置函数分析之strtoupper()、strtolower()
strtoupper(): PHP_FUNCTION(strtoupper) { zend_string *str; ZEND_PARSE_PARAMETERS_START(, ) Z_PARAM_S ...
- tpcc-mysql测试mysql5.6 (EXT4文件系统)
操作系统版本:CentOS release 6.5 (Final) 2.6.32-431.el6.x86_64 #1 内存:32G CPU:Intel(R) Xeon(R) CPU E5-2450 ...
- python tkinter坐标转换
tkinter中坐标原点在左上角,横坐标向右,纵坐标向下,画图需要将坐标转换成右下角的某个点来符合我们的常用坐标 坐标原点设为(x0,y0),横坐标向右,纵坐标向上,: 转换:想实现坐标点(x,y)的 ...
- Oracle 11g 详细安装步骤
一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...
- Minor GC、Major GC、Full GC 区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11488036.html Minor GC 清理年轻代 Minor GC指新生代GC,即发生在新生代(包 ...
- 【leetcode】1089. Duplicate Zeros
题目如下: Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the re ...