212762026-01-12 17:43:08algoproTrükkcpp17Accepted 60/6041ms1552 KiB
// UUID: f7fda52c-fa33-49b8-b8a6-d4975eacda50
#include<bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;

long long modPow(long long a, long long b, long long mod) {
    long long res = 1;
    while (b > 0) {
        if (b & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}

int solveOne() {
    int N, K;
    cin >> N >> K;
    
    vector<vector<int>> graph(N + 1);
    
    for (int i = 0; i < K; i++) {
        int a, b;
        cin >> a >> b;
        int u = a - 1;
        int v = b;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    
    vector<int> color(N + 1, -1); 
    int components = 0;
    
    for (int start = 0; start <= N; start++) {
        if (color[start] != -1) continue;
        
        components++;
        queue<int> q;
        q.push(start);
        
        color[start] = (start == 0) ? 0 : 0;
        
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            
            for (int v : graph[u]) {
                if (color[v] == -1) {
                    color[v] = color[u] ^ 1;
                    q.push(v);
                } else if (color[v] == color[u]) {
                    
                    return 0;
                }
            }
        }
    }
    
    
    int freeComponents = components - 1;
    return modPow(2, freeComponents, MOD);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int T;
    cin >> T;
    
    vector<int> results(T);
    for (int i = 0; i < T; i++) {
        results[i] = solveOne();
    }
    
    for (int i = 0; i < T; i++) {
        cout << results[i] << "\n";
    }
    
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/01ms508 KiB
2Accepted0/021ms1028 KiB
3Accepted3/31ms316 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted2/241ms1500 KiB
8Accepted2/237ms1364 KiB
9Accepted2/239ms1360 KiB
10Accepted2/241ms1368 KiB
11Accepted2/237ms1496 KiB
12Accepted2/237ms1384 KiB
13Accepted2/232ms1104 KiB
14Accepted2/228ms1100 KiB
15Accepted2/228ms1272 KiB
16Accepted2/234ms1384 KiB
17Accepted2/232ms1368 KiB
18Accepted2/229ms1388 KiB
19Accepted2/241ms1368 KiB
20Accepted2/239ms1532 KiB
21Accepted2/239ms1536 KiB
22Accepted2/237ms1444 KiB
23Accepted2/237ms1360 KiB
24Accepted2/237ms1364 KiB
25Accepted2/237ms1360 KiB
26Accepted2/229ms1552 KiB
27Accepted2/237ms1336 KiB
28Accepted2/234ms1376 KiB
29Accepted2/218ms1144 KiB
30Accepted2/214ms1104 KiB