标签:一个 amp cto == class 题目 back scan names
思路:
题目倒是没啥好说的,就是注意memset的效率问题。如果循环多次调用memset去初始化一个比较大的数组,那就会很费时间。就是因为这个被hack了。:(
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 const int MOD = 998244353; 6 const int MAXN = 300005; 7 8 vector<int> G[MAXN]; 9 int col[MAXN]; 10 int n, m; 11 ll c1 = 0, c2 = 0, bin[MAXN]; 12 13 bool dfs(int v, int c) 14 { 15 col[v] = c; 16 if (c == 1) c1++; 17 else c2++; 18 for (int i = 0; i < G[v].size(); i++) 19 { 20 if (col[G[v][i]] == c) return false; 21 if (col[G[v][i]] == 0 && !dfs(G[v][i], 3 - c)) return false; 22 } 23 return true; 24 } 25 26 int main() 27 { 28 bin[0] = 1; 29 for (int i = 1; i <= 300000; i++) bin[i] = bin[i - 1] * 2 % MOD; 30 int t, v, u; 31 scanf("%d", &t); 32 while (t--) 33 { 34 scanf("%d%d", &n, &m); 35 for (int i = 1; i <= n; i++) { G[i].clear(); col[i] = 0; } 36 for (int i = 0; i < m; i++) 37 { 38 scanf("%d%d", &u, &v); 39 G[u].push_back(v); 40 G[v].push_back(u); 41 } 42 bool flg = true; 43 ll ans = 1; 44 for (int i = 1; i <= n; i++) 45 { 46 if (col[i]) continue; 47 ll tmp = 0; c1 = c2 = 0; 48 if (!dfs(i, 1)) { flg = false; break; } 49 tmp = (bin[c1] + bin[c2]) % MOD; 50 ans = ans * tmp % MOD; 51 } 52 if (!flg) puts("0"); 53 else printf("%lld\n", ans); 54 } 55 return 0; 56 }
标签:一个 amp cto == class 题目 back scan names
原文地址:https://www.cnblogs.com/wangyiming/p/10127419.html