题面

传送门

题目大意:给出一个无向图,每个节点可以填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. 按字节读取txt文件缓存区大小设置多少比较好?

    读取 txt 文件常规写法有逐行读取和按照字节缓存读取,那么按照字节缓存读取时,设置缓存区多大比较好呢?百度了一下,没发现有说这个问题的,自测了一把,以事实说话. 常规读取方法如下: // 字节流读取 ...

  2. Docker 基础学习(一)

    Docker官网:https://docker.com/ 中文翻译非常好的学习地址:http://dockerpool.com/static/books/docker_practice/index.h ...

  3. web框架之初识Django

    目录 一.web框架 1.1什么是web框架 1.2自制的简易web框架 1.3三大主流web框架简介 Django Flask Tornado 1.4动态网页与静态网页 二.初识Django框架 2 ...

  4. Callable使用示例

    之前工作中也有使用过Callable,但是却没有使用Thread对象去操作过,今晚 小组培训,涨知识了,现特意记录一下,以免忘记. 先看一下Thread的构造器 可以看到,Thread类并没有提供参数 ...

  5. // 62.是否有利润奖--lrj private boolean isProfitsAward; // 63.利润奖--lrj_price private String profitsAward;

    // 62.是否有利润奖--lrj private boolean isProfitsAward; // 63.利润奖--lrj_price private String profitsAward; ...

  6. Python3解leetcode Min Cost Climbing Stairs

    问题描述: On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you ...

  7. 做网站用php还是python

    单纯说做网站,显然是php更适合,php是专为web而生,而Python只是可以做web.php也比python更简单,更容易学,对于新手更友好. 从权威技术网站w3techs.com2017年7月2 ...

  8. [CF1167D]Bicolored RBS题解

    模拟两个颜色的扩号层数,贪心,如果是左括号,哪边的层数浅就放那边:如果是右括号,哪边的层数深就放那边. 至于层数的维护,两个int就做掉了 放个代码: #include <cstdio> ...

  9. How to: Create a Windows Communication Foundation Client

    How to: Create a Windows Communication Foundation Client To create a Windows Communication Foundatio ...

  10. ruby file

    E:/AutoTHCN/lib/report/generate_report/web14/20190516/LoanTool.636936123857869205_190516_140514.xlsx ...