Submission #19760

# Submission time Handle Problem Language Result Execution time Memory
19760 2016-02-25T05:21:02 Z xhae 트리 (kriii4_X) C++14
9 / 100
1000 ms 6316 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;
}

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);
	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) {
		for (int i = 0; i < c - 2; i++) {
			ans *= n;
			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 6 ms 2020 KB Output is correct
2 Correct 13 ms 2460 KB Output is correct
3 Correct 17 ms 2472 KB Output is correct
4 Correct 8 ms 2016 KB Output is correct
5 Correct 10 ms 2444 KB Output is correct
6 Correct 89 ms 6288 KB Output is correct
7 Correct 89 ms 6300 KB Output is correct
8 Correct 85 ms 4920 KB Output is correct
9 Correct 85 ms 6316 KB Output is correct
10 Correct 84 ms 6240 KB Output is correct
11 Correct 502 ms 5032 KB Output is correct
12 Correct 585 ms 5028 KB Output is correct
13 Correct 860 ms 5032 KB Output is correct
14 Correct 551 ms 5032 KB Output is correct
15 Correct 567 ms 5036 KB Output is correct
16 Correct 688 ms 5044 KB Output is correct
17 Correct 743 ms 5040 KB Output is correct
18 Execution timed out 1000 ms 1576 KB Program timed out
19 Halted 0 ms 0 KB -