# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
19749 |
2016-02-25T05:16:15 Z |
xhae |
트리 (kriii4_X) |
C++14 |
|
1000 ms |
15672 KB |
#include <cstdio>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
const int mod = 1000000007;
unordered_map<int, int> groups;
unordered_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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1584 KB |
Output is correct |
2 |
Correct |
0 ms |
1584 KB |
Output is correct |
3 |
Correct |
0 ms |
1584 KB |
Output is correct |
4 |
Correct |
0 ms |
1584 KB |
Output is correct |
5 |
Correct |
0 ms |
1584 KB |
Output is correct |
6 |
Correct |
0 ms |
1584 KB |
Output is correct |
7 |
Correct |
0 ms |
1584 KB |
Output is correct |
8 |
Correct |
0 ms |
1584 KB |
Output is correct |
9 |
Correct |
0 ms |
1584 KB |
Output is correct |
10 |
Correct |
0 ms |
1584 KB |
Output is correct |
11 |
Correct |
0 ms |
1584 KB |
Output is correct |
12 |
Correct |
0 ms |
1584 KB |
Output is correct |
13 |
Correct |
0 ms |
1584 KB |
Output is correct |
14 |
Correct |
0 ms |
1584 KB |
Output is correct |
15 |
Correct |
0 ms |
1584 KB |
Output is correct |
16 |
Correct |
0 ms |
1584 KB |
Output is correct |
17 |
Correct |
0 ms |
1584 KB |
Output is correct |
18 |
Correct |
0 ms |
1584 KB |
Output is correct |
19 |
Correct |
0 ms |
1584 KB |
Output is correct |
20 |
Correct |
0 ms |
1584 KB |
Output is correct |
21 |
Correct |
0 ms |
1584 KB |
Output is correct |
22 |
Correct |
0 ms |
1584 KB |
Output is correct |
23 |
Correct |
0 ms |
1584 KB |
Output is correct |
24 |
Correct |
0 ms |
1584 KB |
Output is correct |
25 |
Correct |
0 ms |
1584 KB |
Output is correct |
26 |
Correct |
0 ms |
1584 KB |
Output is correct |
27 |
Correct |
0 ms |
1584 KB |
Output is correct |
28 |
Correct |
0 ms |
1584 KB |
Output is correct |
29 |
Correct |
0 ms |
1584 KB |
Output is correct |
30 |
Correct |
0 ms |
1584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
2556 KB |
Output is correct |
2 |
Correct |
24 ms |
3704 KB |
Output is correct |
3 |
Correct |
23 ms |
3836 KB |
Output is correct |
4 |
Correct |
11 ms |
2556 KB |
Output is correct |
5 |
Correct |
21 ms |
3440 KB |
Output is correct |
6 |
Correct |
163 ms |
15144 KB |
Output is correct |
7 |
Correct |
160 ms |
15408 KB |
Output is correct |
8 |
Correct |
95 ms |
7920 KB |
Output is correct |
9 |
Correct |
163 ms |
15672 KB |
Output is correct |
10 |
Correct |
147 ms |
14484 KB |
Output is correct |
11 |
Correct |
571 ms |
9416 KB |
Output is correct |
12 |
Correct |
627 ms |
9416 KB |
Output is correct |
13 |
Correct |
901 ms |
9416 KB |
Output is correct |
14 |
Correct |
605 ms |
9416 KB |
Output is correct |
15 |
Correct |
639 ms |
9548 KB |
Output is correct |
16 |
Correct |
749 ms |
9548 KB |
Output is correct |
17 |
Correct |
804 ms |
9548 KB |
Output is correct |
18 |
Execution timed out |
1000 ms |
1580 KB |
Program timed out |
19 |
Halted |
0 ms |
0 KB |
- |