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 ...
随机推荐
- Solr的学习使用之(七)Solr高级查询facet、facet.pivot简介
以下转载自:http://hongweiyi.com/2013/03/apache-solr-facet-introduction/ 1.什么是Faceted Search Facet['fæsɪt] ...
- R语言——ifelse函数
在数据处理中,经常会遇到要对判断结果做处理的情况.if函数是经常遇到的. ifelse(cond,statment1,statment2) 如果cond成立,执行statment1,否则执行statm ...
- 原生JS实现图片循环切换
<!-- <!DOCTYPE html> <html> <head> <title>原生JS实现图片循环切换 —— 方法一</title&g ...
- GetExtendedTcpTable
https://blog.csdn.net/sky101010ws/article/details/55511501 AdjustTokenPrivileges function Library Ad ...
- 4. jaxp----dom解析器(DocumentBuilderFactory、DocumentBuilder)
1.DocumentBuilderFactory--解析器工厂(抽象类 javax.xml.parsers.DocumentBuilderFactory) newInstance() 获取 Docu ...
- 【leetcode】1078. Occurrences After Bigram
题目如下: Given words first and second, consider occurrences in some text of the form "first second ...
- Spring动态数据源-AbstractRoutingDataSource
在分库分表的情况下,在执行SQL时选择连接不同的数据源(库)的思路:配置多个数据源加到动态数据源对象中,根据实际的情况动态切换到相应的数据源中. 如存放订单信息的有10个库,每个库中有100张表,根据 ...
- OSS重磅推出OSS Select——使用SQL选取文件的内容
对象存储OSS(Object Storage Service)具有海量.可靠.安全.高性能.低成本的特点.OSS提供标准.低频.归档类型,覆盖多种数据从热到冷的存储需求,单个文件的大小从1字节到48. ...
- 862C - Mahmoud and Ehab and the xor(构造)
原题链接:http://codeforces.com/contest/862/problem/C 题意:给出n,x,求n个不同的数,使这些数的异或和为x 思路:(官方题解)只有n==2&&am ...
- Codeforces 845D - Two TVs(贪心)
原题链接:http://codeforces.com/problemset/problem/845/D 题意:一个人在驾照考试中,路边有“限速XX”.“没有限速”.“可以超车”.“不能超车”路牌, 以 ...