Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】
传送门:http://codeforces.com/contest/1093/problem/D
D. Beautiful Graph
2 seconds
256 megabytes
standard input
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.
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.
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.
2
2 1
1 2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
4
0
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.
题意概括:
给一个无向图,要求在每个结点填入 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】的更多相关文章
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)
题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...
- Educational Codeforces Round 56 (Rated for Div. 2) ABCD
题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D
给你一个无向图 以及点的个数和边 每个节点只能用1 2 3 三个数字 求相邻 两个节点和为奇数 能否构成以及有多少种构成方法 #include<bits/stdc++.h> usin ...
- Educational Codeforces Round 56 (Rated for Div. 2)
涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...
- 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 ], 这样 ...
- 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了这道题,但是感觉就算比赛再次遇到类似 ...
- Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array
题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array (简单DP)
题目:https://codeforces.com/contest/1155/problem/D 题意:给你n,x,一个n个数的序列,你可以选择一段区间,区间的数都乘以x,然后求出最大字段和 思路: ...
随机推荐
- go install runtime/cgo: open /usr/local/go/pkg/darwin_amd64/runtime/cgo.a: permission denied
在做更新时,收到下面提示: go get github.com/astaxie/beego go install runtime/cgo: open /usr/local/go/pkg/darwin ...
- vcfc之zk+postsql+keystore(cassandra)框架分析
vcfc框架总结: 1 一. bus和keystore是如何协调处理的,什么样的问题是处理不了的? 1. 如果在备重启的过程中,主处理了某个时间1,备机如果同步数据呢? 二 .数据可靠性和一致性分析 ...
- java 文件的上传和下载
主要介绍使用 smartupload.jar 包中的方法对文件的上传和下载.上传时文件是存放在服务器中,我用的是tamcat. 首先建立一个servlet 类,对文件的操作 package com.d ...
- JavaScript中按键事件的e.keyCode || e.which || e.charCode
1.浏览器的按键事件 浏览器有3种按键事件——keydown,keypress和keyup,分别对应onkeydown.onkeypress和onkeyup3个事件句柄. 一个典型的按键会产生所有这三 ...
- css text-shadow
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 配置centos7 网卡
进入root模式,输入 cd /etc/sysconfig/network-scripts/ 按Tab键查看网卡配置文件名称,然后进入编辑: 如: cd /etc/sysconfig/network- ...
- html高度塌陷问题解决
高度塌陷的问题: 当开启元素的BFC以后,元素将会有如下的特性 1 父元素的垂直外边距不会和子元素重叠 开启BFC的元素不会被浮动元素所覆盖 开启BFC的元素可以包含浮动的子元素 如何开启 ...
- Python基础-简介二
一.变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据类型,这些 ...
- linux多线程编程——读者优先、写者优先问题
读者优先描述 如果读者来: 1) 无读者.写着,新读者可以读: 2) 无写者等待,但有其他读者正在读,新读者可以读: 3) 有写者等待,但有其他读者正在读,新读者可以读: 4) 有写者写,新读者等 如 ...
- SharePoint 2013 - REST API about Security
1. 获取当前用户信息(current user): var currentUserInfo = "{0}/_api/Web/CurrentUser"; // {0} -> ...