72382024-01-04 15:37:33anonTrükkcpp17Accepted 60/6048ms10352 KiB
#include <bits/stdc++.h>
#define MOD ((ll) (1e9 + 7))
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long ll;
using namespace std;
int main() {
    FastIO;
    ll i, j, T, ans;
    cin >> T;
    vector<ll> N(T);
    vector<ll> K(T);
    vector<vector<array<ll, 2>>> ranges(T);
    for(i = 0; i < T; i++) {
        cin >> N[i] >> K[i];
        ranges[i].resize(K[i]);
        for(j = 0; j < K[i]; j++) {
            cin >> ranges[i][j][0] >> ranges[i][j][1];
            ranges[i][j][0]--;
        }
    }
    for(i = 0; i < T; i++) {
        vector<vector<ll>> graph(N[i] + 1);
        for(j = 0; j < K[i]; j++) {
            graph[ranges[i][j][0]].push_back(ranges[i][j][1]);
            graph[ranges[i][j][1]].push_back(ranges[i][j][0]);
        }
        vector<ll> colors(N[i] + 1, 0);
        ans = 0;
        for(j = 0; j <= N[i]; j++) {
            if(colors[j])
                continue;
            stack<array<ll, 2>> st;
            st.push({ j, colors[j] = 1 });
            while(!st.empty()) {
                auto [box, color] = st.top();
                st.pop();
                for(const auto &x : graph[box]) {
                    if(!colors[x]) {
                        st.push({ x, colors[x] = 3 - color });
                    }
                    else if(colors[x] == color) {
                        cout << "0\n";
                        goto next;
                    }
                }
            }
            ans = ans ? ((ans * 2LL) % MOD) : 1;
        }
        cout << ans << '\n';
        next:;
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/03ms1832 KiB
2Accepted0/026ms5216 KiB
3Accepted3/33ms2264 KiB
4Accepted3/33ms2440 KiB
5Accepted3/33ms2664 KiB
6Accepted3/33ms3156 KiB
7Accepted2/248ms9020 KiB
8Accepted2/243ms9024 KiB
9Accepted2/243ms9188 KiB
10Accepted2/248ms9284 KiB
11Accepted2/243ms9404 KiB
12Accepted2/243ms9344 KiB
13Accepted2/239ms9292 KiB
14Accepted2/234ms9400 KiB
15Accepted2/234ms9412 KiB
16Accepted2/241ms9520 KiB
17Accepted2/237ms9628 KiB
18Accepted2/235ms9608 KiB
19Accepted2/248ms10024 KiB
20Accepted2/245ms10108 KiB
21Accepted2/246ms10064 KiB
22Accepted2/243ms10000 KiB
23Accepted2/243ms9960 KiB
24Accepted2/241ms9964 KiB
25Accepted2/241ms10352 KiB
26Accepted2/235ms9944 KiB
27Accepted2/243ms10040 KiB
28Accepted2/237ms10348 KiB
29Accepted2/224ms7640 KiB
30Accepted2/217ms7600 KiB