27742023-01-23 17:09:53CattTrükkcpp17Accepted 60/6083ms6976 KiB
#include <bits/stdc++.h>
using namespace std;
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using ll = long long;

const int maxN = 2e5 + 5;
const int MOD = 1e9 + 7;


ll power(ll a, ll b) {
    ll res = 1;
    while(b > 0) {
        if(b % 2) res = (res * a) % MOD;
        a = (a*a) % MOD;
        b /= 2;
    }
    return res;
}


vector<vector<int> > g;
vector<int> c;

bool dfs(int x) {
    bool ret = 0;
    for(int sz : g[x]) {
        if(c[sz] == -1) {
            c[sz] = 1 - c[x];
            ret |= dfs(sz);
        }
        else if(c[sz] == c[x]) ret = 1;
    }

    return ret;
}

int main() {
    int t;
    cin >> t;
    while(t--)
    {
        int n,m;
        cin >> n >> m;

        g.clear();
        g.resize(n+1);
        c.assign(n+1, -1);

        for(int i = 0; i < m; i++) {
            int x,y;
            cin >> x >> y;
            x--;
            g[x].push_back(y);
            g[y].push_back(x);
        }

        int comp = 0;
        bool b = 0;
        for(int i = 0; i <= n; i++) {
            if(c[i] == -1) {
                c[i] = 0;
                b |= dfs(i);
                comp++;
            }
        }

        if(b) cout <<"0\n";
        else {
            cout << power(2, comp-1) << "\n";
        }
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/03ms1876 KiB
2Accepted0/041ms3464 KiB
3Accepted3/32ms2324 KiB
4Accepted3/32ms2348 KiB
5Accepted3/32ms2472 KiB
6Accepted3/32ms2724 KiB
7Accepted2/283ms5444 KiB
8Accepted2/282ms5388 KiB
9Accepted2/282ms5348 KiB
10Accepted2/282ms5344 KiB
11Accepted2/282ms5344 KiB
12Accepted2/283ms5628 KiB
13Accepted2/274ms4904 KiB
14Accepted2/274ms4904 KiB
15Accepted2/272ms5032 KiB
16Accepted2/272ms5620 KiB
17Accepted2/272ms5456 KiB
18Accepted2/272ms5480 KiB
19Accepted2/282ms6500 KiB
20Accepted2/282ms6828 KiB
21Accepted2/282ms6908 KiB
22Accepted2/282ms6732 KiB
23Accepted2/282ms6520 KiB
24Accepted2/283ms6908 KiB
25Accepted2/282ms6976 KiB
26Accepted2/272ms6204 KiB
27Accepted2/283ms6848 KiB
28Accepted2/279ms6576 KiB
29Accepted2/235ms5888 KiB
30Accepted2/237ms5884 KiB