67572023-12-18 19:45:09111Trükkcpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base60/60
1Elfogadva0/03ms1832 KiB
2Elfogadva0/096ms10488 KiB
3Elfogadva3/33ms2808 KiB
4Elfogadva3/33ms3000 KiB
5Elfogadva3/33ms3088 KiB
6Elfogadva3/33ms3224 KiB
7Elfogadva2/2196ms18536 KiB
8Elfogadva2/2202ms20704 KiB
9Elfogadva2/2197ms22108 KiB
10Elfogadva2/2185ms22308 KiB
11Elfogadva2/2189ms24728 KiB
12Elfogadva2/2193ms25472 KiB
13Elfogadva2/297ms27324 KiB
14Elfogadva2/2103ms28960 KiB
15Elfogadva2/2100ms30080 KiB
16Elfogadva2/2104ms30856 KiB
17Elfogadva2/2101ms32400 KiB
18Elfogadva2/2100ms33432 KiB
19Elfogadva2/2207ms32884 KiB
20Elfogadva2/2210ms35288 KiB
21Elfogadva2/2209ms36312 KiB
22Elfogadva2/2193ms37828 KiB
23Elfogadva2/2188ms38972 KiB
24Elfogadva2/2188ms40264 KiB
25Elfogadva2/2187ms41180 KiB
26Elfogadva2/298ms42804 KiB
27Elfogadva2/2209ms43504 KiB
28Elfogadva2/2151ms43540 KiB
29Elfogadva2/261ms43424 KiB
30Elfogadva2/261ms43748 KiB