67582023-12-18 19:51:26111Trükkcpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base60/60
1Elfogadva0/03ms1828 KiB
2Elfogadva0/064ms9008 KiB
3Elfogadva3/33ms2132 KiB
4Elfogadva3/33ms2204 KiB
5Elfogadva3/33ms2336 KiB
6Elfogadva3/33ms2592 KiB
7Elfogadva2/2128ms15344 KiB
8Elfogadva2/2128ms16832 KiB
9Elfogadva2/2130ms16816 KiB
10Elfogadva2/2126ms15492 KiB
11Elfogadva2/2123ms16380 KiB
12Elfogadva2/2123ms17412 KiB
13Elfogadva2/268ms13968 KiB
14Elfogadva2/268ms14372 KiB
15Elfogadva2/268ms15956 KiB
16Elfogadva2/272ms14240 KiB
17Elfogadva2/272ms14472 KiB
18Elfogadva2/275ms16108 KiB
19Elfogadva2/2136ms16732 KiB
20Elfogadva2/2136ms17872 KiB
21Elfogadva2/2136ms18120 KiB
22Elfogadva2/2123ms17636 KiB
23Elfogadva2/2130ms17776 KiB
24Elfogadva2/2122ms17656 KiB
25Elfogadva2/2123ms17676 KiB
26Elfogadva2/272ms14924 KiB
27Elfogadva2/2131ms18252 KiB
28Elfogadva2/294ms16280 KiB
29Elfogadva2/239ms12340 KiB
30Elfogadva2/239ms12432 KiB