题面

传送门

题目大意:给出一个无向图,每个节点可以填1,2,3三个数中的一个

问有多少种填数方案,使两个相邻节点的数之和为奇数

分析

如果图中有奇环,一定无解

我们对图黑白染色,由于图可能不联通,记第i个连通分量的黑点数量为\(b_i\),白点数量为\(w_i\)

观察发现每一条边的连接的两个节点,一个是2,另一个是1或3

显然要不黑点全部填2,要不白点全部填2

若黑点填2,则剩下的白点有\(2^{w_i}\) 种填法

若白点填2,则剩下的黑点有\(2^{b_i}\) 种填法

总答案为:

\[\Pi (2^{w_i}+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(染色+组合数学)的更多相关文章

  1. D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)

    D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...

  2. Codeforces 1093D Beautiful Graph(二分图染色+计数)

    题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...

  3. Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】

    <题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共 ...

  4. 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 ...

  5. Codeforces 15E Triangles - 组合数学

    Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby fores ...

  6. CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)

    题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数. 思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色. 这里用的并查集(还可以带权并查集优化一下,或 ...

  7. Mysterious Crime CodeForces - 1043D (思维+组合数学)

    Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody ...

  8. codeforces 630H (组合数学)

    H - Benches Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  9. 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 ...

随机推荐

  1. Vue动态注册异步组件(非同一个工程的组件)

    前言:最近在掘金逛的时候,无意中看到前滴滴前端架构黄轶大佬,看到了大佬分享的一篇博客滴滴 webapp 5.0 Vue 2.0 重构经验分享 ,对于其中第5个问题(异步加载的业务线组件,如何动态注册? ...

  2. hdu4417 Super Mario (树状数组/分块/主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...

  3. Qt 样式对于QPushbutton 增加 hover press release效果

    按钮的三种状态,未被选中,选中(划过),点击时候的效果 使用setStyleSheet即QSS样式实现. QPushButton *MyBtn = new QPushButton(this); MyB ...

  4. idhttp.get返回403错误解决办法

    在GET之前,先指定UserAgent参数IdHTTP1.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Ma ...

  5. P2627 修剪草坪 (单调队列优化$dp$)

    题目链接 Solution 70分很简单的DP,复杂度 O(NK). 方程如下: \[f[i][1]=max(f[j][0]+sum[i]-sum[j])\]\[f[i][0]=max(f[i-1][ ...

  6. UX168办公本地环境维护记录

    上班到公司 1.打考勤靠.或处理考勤异常 2.钉钉 3.邮件. 4.禅道系统 5.开启nginx.node.monogo服务 5.1.开启nginx服务 /etc/init.d/apache2 sto ...

  7. RabbitMQ核心概念和AMQP协议(二)

    RabbitMQ是什么? RabbitMQ是一个开源的消息代理和队列服务器,用来通过普通协议,在完全不同的应用之间共享数据,RabbirMQ是使用Erlang语言来编写的,并且RabbitMQ是基于A ...

  8. 微信小程序填坑之路其一:wx.request发送与服务端接受

    一.序言 应公司要求要求,要用小程序开发一个信息录入系统.没办法只能听话来填坑. 先介绍一下环境:客户端——小程序:服务端——java:数据库——mysql:服务器——centos7 需求:客户端输入 ...

  9. PHP 最全的正则表达式

    一.校验数字的表达式  1 数字:^[0-9]*$2 n位的数字:^\d{n}$3 至少n位的数字:^\d{n,}$4 m-n位的数字:^\d{m,n}$5 零和非零开头的数字:^(0|[1-9][0 ...

  10. 28 August

    单调队列复习. 投资 (invest) 给定一带符号整数数列,求长度为 \([s, e]\) 的子区间的和的最大值.(最大子段和) 降二维为一维,for循环枚举区间右端点.预处理前缀和,问题转化为找到 ...