Submission #19746

# Submission time Handle Problem Language Result Execution time Memory
19746 2016-02-25T05:14:20 Z xhae 트리 (kriii4_X) C++14
9 / 100
1000 ms 16264 KB
#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

const int mod = 1000000007;
map<int,int> groups;
map<int, int> groupsize;

int get_root(int a) {
	int t = a;
	while (a != groups[a])
		a = groups[a];
	while (t != groups[t])
	{
		int p = groups[t];
		groups[t] = a;
		t = p;
	}
	return a;
}

bool group_merge(int a, int b) {
	if (!groups.count(a)) {
		groups[a] = a;
		groupsize[a] = 1;
	}
	if (!groups.count(b)) {
		groups[b] = b;
		groupsize[b] = 1;
	}
	a = get_root(a);
	b = get_root(b);
	if (a == b)
		return false;
	if (groupsize[a] < groupsize[b]) {
		swap(a, b);
	}
	groups[b] = a;
	groupsize[a] += groupsize[b];
	return true;
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	long long ans = 1;
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		a--, b--;
		if (!group_merge(a, b)) {
			ans = 0;
		}
	}
	if (m >= n)
		ans = 0;
	int c = n - m;
	if (c >= 2) {
		for (int i = 0; i < c - 2; i++) {
			ans *= n;
			ans %= mod;
		}
		for (auto kv : groups) {
			if (kv.first == kv.second) {
				ans *= groupsize[kv.first];
				ans %= mod;
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1216 KB Output is correct
2 Correct 0 ms 1216 KB Output is correct
3 Correct 0 ms 1216 KB Output is correct
4 Correct 0 ms 1216 KB Output is correct
5 Correct 0 ms 1216 KB Output is correct
6 Correct 0 ms 1216 KB Output is correct
7 Correct 0 ms 1216 KB Output is correct
8 Correct 0 ms 1216 KB Output is correct
9 Correct 0 ms 1216 KB Output is correct
10 Correct 0 ms 1216 KB Output is correct
11 Correct 0 ms 1216 KB Output is correct
12 Correct 0 ms 1216 KB Output is correct
13 Correct 0 ms 1216 KB Output is correct
14 Correct 0 ms 1216 KB Output is correct
15 Correct 0 ms 1216 KB Output is correct
16 Correct 0 ms 1216 KB Output is correct
17 Correct 0 ms 1216 KB Output is correct
18 Correct 0 ms 1216 KB Output is correct
19 Correct 0 ms 1216 KB Output is correct
20 Correct 0 ms 1216 KB Output is correct
21 Correct 0 ms 1216 KB Output is correct
22 Correct 0 ms 1216 KB Output is correct
23 Correct 0 ms 1216 KB Output is correct
24 Correct 0 ms 1216 KB Output is correct
25 Correct 0 ms 1216 KB Output is correct
26 Correct 0 ms 1216 KB Output is correct
27 Correct 0 ms 1216 KB Output is correct
28 Correct 0 ms 1216 KB Output is correct
29 Correct 0 ms 1216 KB Output is correct
30 Correct 0 ms 1216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 2404 KB Output is correct
2 Correct 88 ms 3592 KB Output is correct
3 Correct 90 ms 3856 KB Output is correct
4 Correct 30 ms 2272 KB Output is correct
5 Correct 63 ms 3196 KB Output is correct
6 Correct 428 ms 15604 KB Output is correct
7 Correct 414 ms 15868 KB Output is correct
8 Correct 317 ms 7420 KB Output is correct
9 Correct 437 ms 16264 KB Output is correct
10 Correct 410 ms 14548 KB Output is correct
11 Correct 827 ms 10060 KB Output is correct
12 Correct 897 ms 9928 KB Output is correct
13 Execution timed out 1000 ms 10060 KB Program timed out
14 Halted 0 ms 0 KB -