67572023-12-18 19:45:09111Trükkcpp17Accepted 60/60210ms43748 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);
}

void 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);
	for (int i : s) {
		if (v[i]) {
			continue;
		}
		h.emplace_back();
		scc_dfs(gt, v, h.back(), i);
	}
}

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<vector<int>> h;
		scc(g, h);
		bool ok = true;
		for (int i = 0; i < h.size(); i++) {
			set<int> s;
			for (int j : h[i]) {
				if (s.count((N + j)%(N*2)))ok=false;
				s.insert(j);
			}
		}
		cout << (!ok ? 0 : pow_mod(2, h.size() / 2 - 1)) << endl;
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/03ms1832 KiB
2Accepted0/096ms10488 KiB
3Accepted3/33ms2808 KiB
4Accepted3/33ms3000 KiB
5Accepted3/33ms3088 KiB
6Accepted3/33ms3224 KiB
7Accepted2/2196ms18536 KiB
8Accepted2/2202ms20704 KiB
9Accepted2/2197ms22108 KiB
10Accepted2/2185ms22308 KiB
11Accepted2/2189ms24728 KiB
12Accepted2/2193ms25472 KiB
13Accepted2/297ms27324 KiB
14Accepted2/2103ms28960 KiB
15Accepted2/2100ms30080 KiB
16Accepted2/2104ms30856 KiB
17Accepted2/2101ms32400 KiB
18Accepted2/2100ms33432 KiB
19Accepted2/2207ms32884 KiB
20Accepted2/2210ms35288 KiB
21Accepted2/2209ms36312 KiB
22Accepted2/2193ms37828 KiB
23Accepted2/2188ms38972 KiB
24Accepted2/2188ms40264 KiB
25Accepted2/2187ms41180 KiB
26Accepted2/298ms42804 KiB
27Accepted2/2209ms43504 KiB
28Accepted2/2151ms43540 KiB
29Accepted2/261ms43424 KiB
30Accepted2/261ms43748 KiB