67582023-12-18 19:51:26111Trükkcpp17Accepted 60/60136ms18252 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

#define MOD 1000000007

int pow_mod(int x, int p, int m = MOD) {
	int r = 1;
	while (p > 0) {
		if (p % 2 == 1) {
			r *= x;
			r %= m;
		}
		p /= 2;
		x *= x;
		x %= m;
	}
	return r;
}

void scc_dfs(const auto& g, auto& v, auto& s, int i) {
	if (v[i]) {
		return;
	}
	v[i] = true;
	for (int j : g[i]) {
		scc_dfs(g, v, s, j);
	}
	s.push_back(i);
}

int scc(const auto& g, auto& h) {
	int N = g.size();
	vector<int> v;
	vector<int> s;
	v.assign(N, false);
	for (int i = 0; i < N; i++) {
		scc_dfs(g, v, s, i);
	}
	vector<vector<int>> gt(N);
	for (int i = 0; i < N; i++) {
		for (int j : g[i]) {
			gt[j].push_back(i);
		}
	}
	reverse(s.begin(), s.end());
	v.assign(N, false);
	int c = 0;
	for (int i : s) {
		if (v[i]) {
			continue;
		}
		vector<int> z;
		scc_dfs(gt, v, z, i);
		for (int j : z) {
			h[j] = c;
		}
		c++;
	}
	return c;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int T;
	cin >> T;
	while (T--) {
		int N, K;
		cin >> N >> K;
		N++;
		vector<vector<int>> g(N * 2);
		for (int i = 0; i < K; i++) {
			int a, b;
			cin >> a >> b;
			g[a - 1].push_back(N + b);
			g[N + a - 1].push_back(b);
			g[b].push_back(N + a - 1);
			g[N + b].push_back(a - 1);
		}
		vector<int> h(N * 2);
		int c = scc(g, h);
		bool ok = true;
		for (int i = 0; i < N; i++) {
			ok &= h[i] != h[N + i];
		}
		cout << (!ok ? 0 : pow_mod(2, c / 2 - 1)) << endl;
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/03ms1828 KiB
2Accepted0/064ms9008 KiB
3Accepted3/33ms2132 KiB
4Accepted3/33ms2204 KiB
5Accepted3/33ms2336 KiB
6Accepted3/33ms2592 KiB
7Accepted2/2128ms15344 KiB
8Accepted2/2128ms16832 KiB
9Accepted2/2130ms16816 KiB
10Accepted2/2126ms15492 KiB
11Accepted2/2123ms16380 KiB
12Accepted2/2123ms17412 KiB
13Accepted2/268ms13968 KiB
14Accepted2/268ms14372 KiB
15Accepted2/268ms15956 KiB
16Accepted2/272ms14240 KiB
17Accepted2/272ms14472 KiB
18Accepted2/275ms16108 KiB
19Accepted2/2136ms16732 KiB
20Accepted2/2136ms17872 KiB
21Accepted2/2136ms18120 KiB
22Accepted2/2123ms17636 KiB
23Accepted2/2130ms17776 KiB
24Accepted2/2122ms17656 KiB
25Accepted2/2123ms17676 KiB
26Accepted2/272ms14924 KiB
27Accepted2/2131ms18252 KiB
28Accepted2/294ms16280 KiB
29Accepted2/239ms12340 KiB
30Accepted2/239ms12432 KiB