Submission #19770

# Submission time Handle Problem Language Result Execution time Memory
19770 2016-02-25T05:25:39 Z xhae 트리 (kriii4_X) C++14
100 / 100
99 ms 5028 KB
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int mod = 1000000007;
vector<int> groups;
vector<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) {
	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;
}

long long modpow(long long a, long long p, long long m)
{
	long long r = 1;
	while (p) {
		if (p & 1)
		{
			r = (r*a) % m;
		}
		a = (a*a) % m;
		p >>= 1;
	}
	return r;
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	long long ans = 1;

	vector<pair<int, int>> dat;
	vector<int> vertices;
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		a--, b--;
		dat.emplace_back(a, b);
		vertices.push_back(a);
		vertices.push_back(b);
	}
	sort(vertices.begin(), vertices.end());
	vertices.erase(unique(vertices.begin(), vertices.end()), vertices.end());
	groupsize.assign(vertices.size(), 1);
	groups.reserve(vertices.size());
	for (int i = 0; i < vertices.size(); i++) {
		groups.push_back(i);
	}
	for (int i = 0; i < m; i++) {
		int a = dat[i].first;
		int b = dat[i].second;
		a = lower_bound(vertices.begin(), vertices.end(), a) - vertices.begin();
		b = lower_bound(vertices.begin(), vertices.end(), b) - vertices.begin();
		if (!group_merge(a, b)) {
			ans = 0;
		}
	}
	if (m >= n)
		ans = 0;
	int c = n - m;
	if (c >= 2) {
		ans *= modpow(n, c-2, mod);
		ans %= mod;
		for (int i = 0; i < vertices.size(); i++) {
			if (i == groups[i]) {
				ans *= groupsize[i];
				ans %= mod;
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1580 KB Output is correct
2 Correct 0 ms 1580 KB Output is correct
3 Correct 0 ms 1580 KB Output is correct
4 Correct 0 ms 1580 KB Output is correct
5 Correct 0 ms 1580 KB Output is correct
6 Correct 0 ms 1580 KB Output is correct
7 Correct 0 ms 1580 KB Output is correct
8 Correct 0 ms 1580 KB Output is correct
9 Correct 0 ms 1580 KB Output is correct
10 Correct 0 ms 1580 KB Output is correct
11 Correct 0 ms 1580 KB Output is correct
12 Correct 0 ms 1580 KB Output is correct
13 Correct 0 ms 1580 KB Output is correct
14 Correct 0 ms 1580 KB Output is correct
15 Correct 0 ms 1580 KB Output is correct
16 Correct 0 ms 1580 KB Output is correct
17 Correct 0 ms 1580 KB Output is correct
18 Correct 0 ms 1580 KB Output is correct
19 Correct 0 ms 1580 KB Output is correct
20 Correct 0 ms 1580 KB Output is correct
21 Correct 0 ms 1580 KB Output is correct
22 Correct 0 ms 1580 KB Output is correct
23 Correct 0 ms 1580 KB Output is correct
24 Correct 0 ms 1580 KB Output is correct
25 Correct 0 ms 1580 KB Output is correct
26 Correct 0 ms 1580 KB Output is correct
27 Correct 0 ms 1580 KB Output is correct
28 Correct 0 ms 1580 KB Output is correct
29 Correct 0 ms 1580 KB Output is correct
30 Correct 0 ms 1580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1860 KB Output is correct
2 Correct 21 ms 2228 KB Output is correct
3 Correct 17 ms 2228 KB Output is correct
4 Correct 9 ms 1860 KB Output is correct
5 Correct 15 ms 2228 KB Output is correct
6 Correct 91 ms 4972 KB Output is correct
7 Correct 92 ms 4996 KB Output is correct
8 Correct 79 ms 4276 KB Output is correct
9 Correct 87 ms 5028 KB Output is correct
10 Correct 79 ms 4876 KB Output is correct
11 Correct 87 ms 4380 KB Output is correct
12 Correct 86 ms 4372 KB Output is correct
13 Correct 86 ms 4376 KB Output is correct
14 Correct 86 ms 4376 KB Output is correct
15 Correct 95 ms 4388 KB Output is correct
16 Correct 86 ms 4400 KB Output is correct
17 Correct 59 ms 4400 KB Output is correct
18 Correct 0 ms 1580 KB Output is correct
19 Correct 99 ms 4416 KB Output is correct
20 Correct 92 ms 4416 KB Output is correct
21 Correct 88 ms 4372 KB Output is correct
22 Correct 86 ms 4392 KB Output is correct
23 Correct 59 ms 4420 KB Output is correct
24 Correct 76 ms 4416 KB Output is correct
25 Correct 59 ms 4420 KB Output is correct
26 Correct 63 ms 4420 KB Output is correct
27 Correct 89 ms 4860 KB Output is correct
28 Correct 89 ms 4860 KB Output is correct
29 Correct 97 ms 4424 KB Output is correct
30 Correct 98 ms 4424 KB Output is correct