传送门: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. php操作Excel

    php操作Excel 1.new PHPExcel对象$objPHPExcel = new PHPExcel(); 2表的初始化设置$objPHPExcel->getProperties()-& ...

  2. css样式、js2种方式 控制字符个数,多余的字用省略号代替

    大家好,我是小菜 前端 ,技术不高,正在努力中充电!希望大家多多指教:css <div class="show">大家好,我是小菜 前端 ,技术不高,正在努力中充电!希 ...

  3. group by 语句

    user E_book go 这样的程序会出错,因为play没有使用sum,所以要分组. group by play 有函数的和没有函数的表一起使用要用 GROUP BY .AVG 求平均值,只能与数 ...

  4. Consul 遇到的坑

    均衡负载时调用的地址 spring.cloud.consul.discovery.service-name= 当A服务调用B服务时,可以转发到注册中心进行转发调用, 应该使用这个地址,这一点和eure ...

  5. asp.net core2.0 连接mysql和mssql

    转自:https://www.jianshu.com/p/15a557ac43d9 1.连接mysql 第一步,新建asp.net core项目   新建项目 本例程作简单演示两种数据库的连接,为简便 ...

  6. Java API 之 Annotation功能

    JDK1.5开始增加了Annotation功能,该功能可用于: 1.类: 2.构造方法: 3.成员变量: 4.方法 5.参数 等的声明: 该功能并不影响程序的运行,但是会对编译器警告等辅助工具产生影响 ...

  7. Be opinionated out of the box but get out of the way quickly as requirements start to diverge from

    Be opinionated out of the box but get out of the way quickly as requirements start to diverge from t ...

  8. 三 Buffer

    使用Buffer一般遵循以下四个步骤 写入数据到Buffer 调用flip() 从Buffer中读取数据 调用clear()或者compact()方法 当向buffer写入数据时,buffer会记录下 ...

  9. MVC 控制器中直接访问url 的方式

    public void ShowDetailsImg() { //生成MD5码 string path = @"D:\其他\Test\WebApplication2\WebApplicati ...

  10. lintcode题目记录4

    Russian Doll Envelopes    Largest Divisible Subset     Two Sum - Input array is sorted Russian Doll ...