传送门:http://codeforces.com/contest/1093/problem/D

D. Beautiful Graph

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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, 22 or 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

Copy
2
2 1
1 2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output

Copy
4
0
Note

Possible ways to distribute numbers in the first test:

  1. the vertex 11 should contain 11, and 22 should contain 22;
  2. the vertex 11 should contain 33, and 22 should contain 22;
  3. the vertex 11 should contain 22, and 22 should contain 11;
  4. the vertex 11 should contain 22, and 22 should contain 33.

In the second test there is no way to distribute numbers.

题意概括:

给一个无向图,要求在每个结点填入 1,2,3 三个数的其中一个,如果每条边相连的两个结点的权值之和为奇数,则当前的填数方案是满足条件的;

询问有多少种填数方案,如果不存在则输出0;

解题思路:

因为只有 1, 2, 3 三个数,其中有两个奇数, 一个偶数。为了满足题目的条件,我们的填入规则肯定是在一条路径上奇偶奇偶...这样填入数字,保证相连两点的和为奇数。

那么就有该路径是先填奇数还是先填偶数之分了:

如果是在该路径上的第一点填入奇数,那么由这个点出发的路径的偶数点肯定是要填偶数,那么我们统计填入奇数的点(即奇数点)的个数 p ,每个点可填两种数,方案有 2 的 p 次方种。

如果在该路径的第一点填入偶数,那么偶数点要填奇数(1或者3),统计填入奇数的点(即偶数点)的个数 x,每个点有两种选择,方案数为 2 的 x 次方。

那么总的方案数就是上面两种情况的方案数之和。

由此,我们发现统计路径的奇数点和偶数点,分别以他们求2次幂之和,就是以当前点出发所能经过路径的方案数了。枚举一遍起点,求出总的方案数就是答案。

那么无解的情况呢,就是存在起点和终点奇偶性相同的环(DFS过程中判断一下即可)。

注意点:

一、2次幂可先预处理

二、一开始敲得静态邻接表版本要注意初始化得方式 for循环可过,效率客观(比vector版本快些)。memset初始化超时。

三、vector版本的for循环初始化即可。

AC code:

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const LL MOD = ;
int N, M, p, s;
LL ans;
const int MAXN = 3e5+;
int book[MAXN];
LL pw[MAXN+];
bool con;
//bool vis[MAXN];
struct EDGE
{
int v, nxt;
}edge[MAXN<<];
int head[MAXN], cnt; void add(int u, int v)
{
edge[cnt].v = v;
edge[cnt].nxt = head[u];
head[u] = cnt++;
} void init()
{
// memset(book, -1, sizeof(book));
// memset(head, -1, sizeof(head));
cnt = ;
con = true;
ans = 1LL;
} void dfs(int now, int flg)
{
if(flg == ) p++;
else s++; book[now] = flg;
int v;
for(int i = head[now]; i != -; i = edge[i].nxt){
v = edge[i].v;
if(book[v] == -){
dfs(v, -flg);
}
else if(book[v] == flg) {con = ; return;}
}
} int main()
{
int T_case, u, v;
scanf("%d", &T_case);
pw[] = ;
for(int i = ; i < MAXN; i++){
pw[i] = (pw[i-]*)%MOD;
}
memset(head, -, sizeof(head));
memset(book, -, sizeof(book));
while(T_case--)
{
init();
scanf("%d%d", &N, &M);
for(int i = ; i <= M; i++){
scanf("%d %d", &u, &v);
add(u, v);
add(v, u);
}
for(int st = ; st <= N; st++){
if(book[st] != -) continue;
else if(book[st] == -){
p = , s = ;
dfs(st, );
ans = ans*(pw[p]+pw[s])%MOD;
}
} if(con) printf("%I64d\n", ans);
else printf("0\n"); for(int i = ; i <= N; i++){
head[i] = -;
book[i] = -;
}
}
return ;
}

Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】的更多相关文章

  1. Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)

    题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...

  2. Educational Codeforces Round 56 (Rated for Div. 2) ABCD

    题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...

  3. Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))

    题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...

  4. Educational Codeforces Round 56 (Rated for Div. 2) D

    给你一个无向图 以及点的个数和边  每个节点只能用1 2 3 三个数字 求相邻 两个节点和为奇数   能否构成以及有多少种构成方法 #include<bits/stdc++.h> usin ...

  5. Educational Codeforces Round 56 (Rated for Div. 2)

    涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...

  6. Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题

    F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...

  7. Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)

    题意和分析在之前的链接中有:https://www.cnblogs.com/pkgunboat/p/10160741.html 之前补题用三维偏序的cdq的分治A了这道题,但是感觉就算比赛再次遇到类似 ...

  8. Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array

    题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...

  9. Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array (简单DP)

    题目:https://codeforces.com/contest/1155/problem/D 题意:给你n,x,一个n个数的序列,你可以选择一段区间,区间的数都乘以x,然后求出最大字段和 思路: ...

随机推荐

  1. Linux常用命令语法+示例

    原文出自:https://blog.csdn.net/seesun2012 Linux常用命令:Linux查看日志命令总结:Tomcat相关:Linux配置网卡,连接外网:Linux下安装JDK:Li ...

  2. Exists 和 Not Exists

    只注重子查询是否有返回行,如有返回结果为真,否则为假,并不适用子查询的结果,仅用于测试子查询是否有返回结果. 语法: if exists (子查询) begin 语句块 end 例子: if exis ...

  3. C#导入Excel|读取Excel方法

    OleDbConnection读取 /// <summary>       /// 返回Excel数据源       /// </summary>       /// < ...

  4. C# 多线程系列之Semaphore使用

    Semaphore,即信号量的意思.是操作系统原始提供的内核同步对象. Semaphore semaphoreAcceptedClients = , 3,"Semaphore1") ...

  5. zookeeper【2】集群管理

    Zookeeper 的核心是广播,这个机制保证了各个Server之间的同步.实现这个机制的协议叫做Zab协议. Zab协议有两种模式,它们分别是恢复模式(选主)和广播 模式(同步).当服务启动或者在领 ...

  6. php foreach遍历

    foreach($facility_list['data'] as $facility){ //处理语句} 第一种格式遍历给定的 array_expression_r_r 数组.每次循环中,当前单元的 ...

  7. Git更新或提交出错的解决办法

    一.舍弃本地代码,用远端版本覆盖本地版本. 当自己本地修改很少,更新代码出现冲突时,“error: Your local changes to the following files would be ...

  8. 批量删除微博的js代码

    清空微博,网上找了一段js代码,试了下,还行. var fileref=document.createElement('script') fileref.setAttribute("type ...

  9. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    viewport:移动设备上用来显示网页的那部分区域,但其大小并不局限于那部分可视区域 width设置layout viewport  的宽度,为一个正整数,或字符串"device-widt ...

  10. 基于bootstrap的单选(radio)或者多选(checkbox)的选择框组

    完成的效果如下图所示: html代码如下: <!-- 两行组 --> <div class="box"> <ul class="list-g ...