码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】

时间:2018-12-16 21:44:33      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:targe   map   code   review   .com   memory   main   one   int   

传送门: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 (1t3?1051≤t≤3?105) — the number of tests in the input.

The first line of each test contains two integers nn and mm (1n3?105,0m3?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 (1ui,vin;uivi1≤ui,vi≤n;ui≠vi) — indices of vertices connected by ii-th edge.

It is guaranteed that i=1tn3?105∑i=1tn≤3?105 and i=1tm3?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:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 #include <map>
 7 #define LL long long
 8 #define INF 0x3f3f3f3f
 9 using namespace std;
10 const LL MOD = 998244353;
11 int N, M, p, s;
12 LL ans;
13 const int MAXN = 3e5+10;
14 int book[MAXN];
15 LL pw[MAXN+1];
16 bool con;
17 //bool vis[MAXN];
18 struct EDGE
19 {
20     int v, nxt;
21 }edge[MAXN<<1];
22 int head[MAXN], cnt;
23 
24 void add(int u, int v)
25 {
26     edge[cnt].v = v;
27     edge[cnt].nxt = head[u];
28     head[u] = cnt++;
29 }
30 
31 void init()
32 {
33 //    memset(book, -1, sizeof(book));
34 //    memset(head, -1, sizeof(head));
35     cnt = 0;
36     con = true;
37     ans = 1LL;
38 }
39 
40 void dfs(int now, int flg)
41 {
42     if(flg == 1) p++;
43     else s++;
44 
45     book[now] = flg;
46     int v;
47     for(int i = head[now]; i != -1; i = edge[i].nxt){
48         v = edge[i].v;
49         if(book[v] == -1){
50             dfs(v, 1-flg);
51         }
52         else if(book[v] == flg) {con = 0; return;}
53     }
54 }
55 
56 int main()
57 {
58     int T_case, u, v;
59     scanf("%d", &T_case);
60     pw[0] = 1;
61     for(int i = 1; i < MAXN; i++){
62         pw[i] = (pw[i-1]*2)%MOD;
63     }
64     memset(head, -1, sizeof(head));
65     memset(book, -1, sizeof(book));
66     while(T_case--)
67     {
68         init();
69         scanf("%d%d", &N, &M);
70         for(int i = 1; i <= M; i++){
71             scanf("%d %d", &u, &v);
72             add(u, v);
73             add(v, u);
74         }
75         for(int st = 1; st <= N; st++){
76             if(book[st] != -1) continue;
77             else if(book[st] == -1){
78                 p = 0, s = 0;
79                 dfs(st, 1);
80                 ans = ans*(pw[p]+pw[s])%MOD;
81             }
82         }
83 
84         if(con) printf("%I64d\n", ans);
85         else printf("0\n");
86 
87         for(int i = 0; i <= N; i++){
88             head[i] = -1;
89             book[i] = -1;
90         }
91     }
92     return 0;
93 }

 

Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】

标签:targe   map   code   review   .com   memory   main   one   int   

原文地址:https://www.cnblogs.com/ymzjj/p/10127952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!