D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph
You are given an undirected unweighted graph consisting of nn vertices and mm edges.
You have to write a number on each vertex of the graph. Each number should be 11, 22or 33. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd.
Calculate the number of possible ways to write numbers 11, 22 and 33 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353998244353.
Note that you have to write exactly one number on each vertex.
The graph does not have any self-loops or multiple edges.
Input
The first line contains one integer tt (1≤t≤3⋅1051≤t≤3⋅105) — the number of tests in the input.
The first line of each test contains two integers nn and mm (1≤n≤3⋅105,0≤m≤3⋅1051≤n≤3⋅105,0≤m≤3⋅105) — the number of vertices and the number of edges, respectively. Next mm lines describe edges: ii-th line contains two integers uiui, vivi (1≤ui,vi≤n;ui≠vi1≤ui,vi≤n;ui≠vi) — indices of vertices connected by ii-th edge.
It is guaranteed that ∑i=1tn≤3⋅105∑i=1tn≤3⋅105 and ∑i=1tm≤3⋅105∑i=1tm≤3⋅105.
Output
For each test print one line, containing one integer — the number of possible ways to write numbers 11, 22, 33 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353998244353.
Example
Input
2
2 1
1 2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output
4
0
Note
Possible ways to distribute numbers in the first test:
- the vertex 11 should contain 11, and 22 should contain 22;
- the vertex 11 should contain 33, and 22 should contain 22;
- the vertex 11 should contain 22, and 22 should contain 11;
- the vertex 11 should contain 22, and 22 should contain 33.
In the second test there is no way to distribute numbers.
题意:
给你n个节点,m个边的无向图。
每人一个节点可以填1,2,3 中的任意一个。
问你有多少种填数字的方案,使每一条边连接的两个节点填的数字相加为奇数。
思路:
因为一个奇数+一个偶数=奇数。
所以如果有合法的填充方案就是有合法的黑白染色方案。
那么我们先用经典的“黑白染色-判断法”判断是否能有合法方案。
如果没有直接输出0
如果可以成功黑白染色,再计算方案数。
我们通过可以推出
每一个连通块中的方案数是 2(填奇数的节点个数)+2(填偶数的节点个数)
填奇数的节点个数和偶数的个数可以通过dfs得出。
如果一个连通块中只有一个节点,那么方案数应该是3.
每一个联通的答案的乘积就是总方案数。
记得取模即可。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int *p);
const int maxn = 300010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int t;
std::vector<int> e[maxn];
queue<pii> q;
int col[maxn];
const ll mod = 998244353;
ll ans = 0ll;
int vis[maxn];
int tot;
ll base1;
ll base2;
void dfs(int x, int c)
{
vis[x] = 1;
tot++;
if (c & 1) {
base1++;
} else {
base2++;
}
for (auto z : e[x]) {
if (vis[z]) { continue; }
dfs(z, c == 1 ? 2 : 1);
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
du1(t);
while (t--) {
int n, m;
du2(n, m);
if (n == 1) {
n = 1;
}
repd(i, 1, n) {
e[i].clear();
col[i] = 0;
vis[i] = 0;
}
while (!q.empty()) {
q.pop();
}
int x, y;
repd(i, 1, m) {
du2(x, y);
e[x].pb(y);
e[y].pb(x);
}
int isok = 1;
// 二分图判断部分
repd(i, 1, n) {
if (col[i] != 0) {
continue;
}
q.push(mp(i, 1));
while (!q.empty()) {
pii temp = q.front();
// cout<<temp.fi<<" "<<temp.second<<endl;
q.pop();
if (col[temp.fi] != 0 && col[temp.fi] != temp.se) {
isok = 0;
break;
}
if (col[temp.fi] == 0) {
col[temp.fi] = temp.se;
} else {
continue;
}
for (auto Z : e[temp.fi]) {
q.push(mp(Z, temp.se == 1 ? 2 : 1));
}
}
}
if (isok) {
ans = 1ll;
repd(i, 1, n) {
// 对于每一个连通块,求方案数。乘法原理计算答案。
if (vis[i]) { continue; }
tot = 0;
base1 = 0ll;
base2 = 0ll;
dfs(i, 1);
if (tot == 1) {// 连通块内只有一个节点时 答案是3
ans = ans * 3ll % mod;
} else {
ans = ans * ((powmod(2ll, base1, mod) % mod + powmod(2ll, base2, mod) % mod) % mod) % mod;
}
}
printf("%lld\n", ans );
} else {
printf("0\n");
}
// cout << isok << endl;
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)的更多相关文章
- Edge coloring of bipartite graph CodeForces - 600F (二分图染色)
大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0 ...
- CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)
题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数. 思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色. 这里用的并查集(还可以带权并查集优化一下,或 ...
- Codeforces 57C (1-n递增方案数,组合数取模,lucas)
这个题相当于求从1-n的递增方案数,为C(2*n-1,n); 取模要用lucas定理,附上代码: #include<bits/stdc++.h> using namespace std; ...
- AIM Tech Round (Div. 2) C. Graph and String 二分图染色
C. Graph and String 题目连接: http://codeforces.com/contest/624/problem/C Description One day student Va ...
- Codeforces 1093D Beautiful Graph(二分图染色+计数)
题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...
- Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】
<题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)
题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...
- Codeforces 664D Graph Coloring 二分图染色
题意: 一个无向图的每条边为红色或蓝色,有这样一种操作:每次选一个点,使与其相邻的所有边的颜色翻转. 求解是否可以经过一系列操作使所有的边颜色相同,并输出最少操作次数和相应的点. 分析: 每个点要么选 ...
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)
题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...
随机推荐
- 【并行计算-CUDA开发】浅谈GPU并行计算新趋势
随着GPU的可编程性不断增强,GPU的应用能力已经远远超出了图形渲染任务,利用GPU完成通用计算的研究逐渐活跃起来,将GPU用于图形渲染以外领域的计算成为GPGPU(General Purpose c ...
- eclipse 如何从Gitee.com克隆工程到本地,并运行
1.再项目资源管理器里选择导入,导入 2.在导入向导中选择Git,选择来自Git的项目 3.选克隆URL 4.输入URL 和用户密码,点击下一步 4.下一步 5.选择保存路径 6.选择下一步,自动导入 ...
- day22 subprocess、configeparser、表格操作模块
今日内容: 1.configparser模块的使用 2.subprocess模块的使用 3.表格处理模块 xlrd模块 xlwt模块 1.configparser模块 configparser模块是用 ...
- mysql 插入数据后返回自增 ID 的七种方法
参考地址:https://blog.csdn.net/qq_30715329/article/details/80868411 其中使用函数方式.存储过程方式.注解方式.xml属性方式设置都可. 常用 ...
- [Nowcoder113E]弹球弹弹弹_线段树
弹球弹弹弹 题目大意:有n个位置,标号为1到n的整数,m次操作,第i次操作放置一个弹球在b[i] xor c[i-1]处,并询问b[i] xor c[i-1]处弹球个数c[i]每次操作后,在x处的弹球 ...
- 【转帖】Istio是啥?一文带你彻底了解!
Istio是啥?一文带你彻底了解! http://www.sohu.com/a/270131876_463994 原始位置来源: https://cizixs.com 如果你比较关注新兴技术的话,那么 ...
- 查找担保圈-step6-对被包含过的组进行清理,只保留未被包含过的组
USE [test] GO /****** Object: StoredProcedure [dbo].[p04_get_groupno_cleared] Script Date: 2019/7/8 ...
- vue 简易学习
好记性不如烂笔头 最近公司新出一个框架,采用的是前后端分离的开发方式,后端用的是springboot+mybatis(还有额外的zk.缓存.日志等待),前端采用的是vue+es6,由于以前对vue只知 ...
- SQLite进阶-9.别名
我们可以暂时给表或者列重命名为另一个名字,称为别名,重命名只是临时运行时改变,不会改变数据库中的实际的名字. -- 语法 SELECT column_name AS column_alias_name ...
- 03 Python基础
1.输出和输入 (1)print打印 Python 提供print方法来打印信息 输入: print ("hello python") 调用print方法,用户双引号(" ...