1. 程式人生 > >Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【規律 && DFS】

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

傳送門: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 

n">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 

1">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 

t">tt (1t31051≤t≤3⋅105) — the number of tests in the input.

The first line of each test contains two integers nn and mm (1n3105,0m31051≤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=1tn3105∑i=1tn≤3⋅105 and i=1tm3105∑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 規律 &amp;&amp; DFS

傳送門:http://codeforces.com/contest/1093/problem/D D. Beautiful Graph time limit per test 2 seconds memory limit per test 256

Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph(二分圖判定+計數)

題意 給一個圖,圖上的點可以被染成權值為1,2,3 令邊權=兩個點的點權和,求令邊權為奇數的所有方案數%998244353 思路來源 自己寫的 題解 首先二分圖判定一下,分奇偶層; 奇層染奇數,偶層染偶數; 或奇層染偶數,偶層染奇數。 對於每個連通分量

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

-- sum rate names ans 無向圖 != || space 給你一個無向圖 以及點的個數和邊 每個節點只能用1 2 3 三個數字 求相鄰 兩個節點和為奇數 能否構成以及有多少種構成方法 #include<bits/stdc++.

Multidimensional Queries(二進位制列舉+線段樹+Educational Codeforces Round 56 (Rated for Div. 2))

題目連結:   https://codeforces.com/contest/1093/problem/G 題目: 題意:   在k維空間中有n個點,每次給你兩種操作,一種是將某一個點的座標改為另一個座標,一種操作是查詢[l,r]中曼哈頓距離最大的兩個點的最大曼哈頓距離。 思路:   對於曼哈

Educational Codeforces Round 56 (Rated for Div. 2)

漲rating啦。。 不過話說為什麼有這麼多資料結構題啊,難道是中國人出的? A - Dice Rolling 傻逼題,可以用一個三加一堆二或者用一堆二,那就直接。。 #include<cstdio> #include<cstring> #include<algorithm

Educational Codeforces Round 56 (Rated for Div. 2) CodeForces - 1093C

題意: 有一個序列a ,有n個數 給出一個序列b,且滿足 bi=ai+an−i+1     求序列a,答案不唯一,滿足就行 #include<stdio.h> #include<iostream> #include<algo

Educational Codeforces Round 56 (Rated for Div. 2) CodeForces - 1093B

題意: 給你一個字串,任意變化,使它變成不是迴文串,如果不行,就輸出-1 我的做法是,如果這個串只有同一個字母,那麼直接輸出-1 否則,sort一下,輸出就行了,即可保證不是迴文串 #include<stdio.h> #include<iostream> #i

Educational Codeforces Round 56 (Rated for Div. 2) CodeForces - 1093A

題意大概就是,現在有一個骰子,面上的數字為2~7,然後給你一個x,問多少個總和為這個數字 答案不唯一,所以就讓每個骰子都為2,答案就是x/2 #include<stdio.h> #include<iostream> #include<algorithm>

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

題目連結:https://codeforces.com/contest/1093 A. Dice Rolling 題意: 有一個號數為2-7的骰子,現在有一個人他想扔到幾就能扔到幾,現在問需要扔多少次,能使扔出的總和等於xi。   題解: 由於是special judge,模擬一下搞搞就

codeforces Educational Codeforces Round 56 (Rated for Div. 2) 部分題解

A. Dice Rolling 題目: 傳送門A 題意: 就是求任意需要篩幾次達到目標值 程式碼如下:   #include <bits/stdc++.h> using namespace std; int t; int x; int main() {

Educational Codeforces Round 56 (Rated for Div. 2) E. Intersection of Permutations(分塊 + 樹狀陣列)

題目連結:https://codeforces.com/contest/1093/problem/E 題目大意:給出兩個1~n的排列 a 和 b;對這兩個排列進行如下兩種操作: 1 la ra lb rb:查詢排列 a 的區間 [la,ra]

Educational Codeforces Round 56 (Rated for Div. 2) A. Dice Rolling 簽到題(思維題)

Mishka got a six-faced dice. It has integer numbers from 22 to 77 written on its faces (all numbers on faces are different, so t

Educational Codeforces Round 56 (Rated for Div. 2) C - Mishka and the Last Exam(貪心/差分約束)

題意 給一個n,一個序列b[], bi=ai+a(n+i-1), 求不降序的a序列[] 思路來源 組裡各神犇&&自己 題解 Solution1: 顯然a1=0,an=b1的時候,區間長度最長 區間裡面內建區間的時候如果內區間能左對齊,顯

Educational Codeforces Round 38 (Rated for Div. 2) ----D

pan inf force mes hid get struct include push D. Buy a Ticket 問題轉換為對於每一個點x,求出一個點y,使得xy的最短路2倍+在y舉辦的費用最小。 考慮建一個超級源點,向每一個點連一條費用為其舉辦所需費用

Educational Codeforces Round 36 (Rated for Div. 2) ---d

continue void 枚舉 nal HR spl %d 大小 log D. Almost Acyclic Graph 首先判環可以用拓撲來實現。 暴力解法自然是枚舉每一條邊,刪除,判斷是否存在環。 解法一: 對於指向同一個點的邊,在拓撲排序中看刪除他們事

#分組背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

p s eterm queue erl 學習 () logs https 情況 2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 second

Educational Codeforces Round 42 (Rated for Div. 2) D - Merge Equals

cond ons 直接 can end 開始 cstring type Education 這道題我可以直接模擬 理由是一個數*2的過程中最多30次左右 2^31 = 2e9 所以我可以從小的書開始模擬這個過程 #include <iostream> #incl

Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair

題意:一個人  有T塊錢 有一圈商店 分別出售 不同價格的東西  每次經過商店只能買一個  並且如果錢夠就必須買  這個人一定是從1號店開始的!(比賽的時候讀錯了題,以為隨意起點。。。)問可以買多少個 思路:這個人有T塊錢  走一圈之後可以買num個 花了