死循环,感觉错误很离谱,但找不出来
查看原帖
死循环,感觉错误很离谱,但找不出来
637788
kimi0705楼主2024/7/11 11:34
#include <bits/stdc++.h>
#define fi first
#define se second
#define multi true
#define gc getchar
#define pc putchar
#define pb push_back
#define int long long
#define pii pair<int, int>
#define debug(x) cout << #x << " " << x << '\n'
#define For(i, x, y) for (int i = (x); i <= (y); i++)
#define Rof(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
int read(char ch = ' ', int x = 0, int w = 1) {
    while (!isdigit(ch)) {
        if (ch == '-') w = -1;
        ch = gc();
    }
    while (isdigit(ch)) { x = x * 10 + (ch - '0'), ch = gc(); }
    return x * w;
}
void read(int& x) { x = read(); }
void read(int n, int a[]) { For(i, 1, n) a[i] = read(); }
void read(int n, int a[], int b[]) { For(i, 1, n) a[i] = read(), b[i] = read(); }
void write(int x) {
    if (x < 0) x = -x, pc('-');
    if (x > 9) write(x / 10);
    pc(x % 10 + '0');
}
void write(int n, int a[]) { For(i, 1, n) write(a[i]), pc(' '); }
/*=============================================================================================================================================================================================*/
const int N = 3e5 + 10;
const int mod = 998244353;
int qpow(int x, int y) {
    if (y == 0) return 1;
    if (y == 1) return x;
    int result = qpow(x, y / 2);
    result = result * result % mod;
    if (y & 1) return result * x % mod;
    return result;
}
int t = 1, n, m, x, y, cnt1, ans;
int type[N];
vector<int> Edge[N];
queue<int> qu;
void solve() {
    while(qu.size()) qu.pop();
    read(n), read(m), ans = 1;
    For(i, 1, n) Edge[i].clear();
    For(i, 1, n) type[i] = 0;
    For(i, 1, m) x = read(), y = read(), Edge[x].pb(y), Edge[y].pb(x);
    For(i, 1, n) {
        if (type[i]) continue;
        type[i] = 1, qu.push(i);
        while (qu.size()) {
            int x = qu.front();
            qu.pop();
            for (int i : Edge[x]) {
                if (type[i] == 0)
                    type[i] = 3 - type[x], qu.push(i);
                else if (type[i] == type[x])
                    return puts("0"), void();
            }
        }
        cnt1 = 0;
        For(i, 1, n) cnt1 += (type[i] - 1);
        ans = ans * (qpow(2, cnt1) + qpow(2, n - cnt1) % mod) % mod;
    }
    write(ans);
    pc(10);
}
signed main() {
    if (multi) read(t);
    while (t--) solve();
    return 0;
}
12
8 7
2 3
3 4
4 5
5 2
6 7
7 8
8 6

1 0

2 1
1 2

3 3
1 2
2 3
1 3

3 2
2 3
3 1

4 4
1 2
2 3
3 4
4 1

4 4
1 2
2 3
3 1
4 1
6 9
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6

100000 0

1000 5
55 56
56 57
57 58
58 59
59 55
11 11
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 1
10 12
1 2
2 3
3 4
4 1
1 5
5 6
6 7
7 1
1 8
8 9
9 10
10 1
2024/7/11 11:34
加载中...