Submission #19864

# Submission time Handle Problem Language Result Execution time Memory
19864 2016-02-25T06:22:26 Z cki86201 트리 (kriii4_X) C++14
100 / 100
189 ms 11592 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pi;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()

const ll MOD = 1e9 + 7;

ll pw(ll x, ll y){
	ll res = 1;
	while(y){
		if(y & 1)res = res * x % MOD;
		x = x * x % MOD;
		y >>= 1;
	}
	return res;
}

map <int, int> M;
int cn;
int par[200020], w[200020];
int Find(int x){return par[x] == x ? x : par[x] = Find(par[x]);}
int q[100010][2];

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	rep(i, m){
		int x, y;
		scanf("%d%d", &x, &y);
		if(M.find(x) == M.end())M[x] = ++cn;
		if(M.find(y) == M.end())M[y] = ++cn;
		q[i][0] = M[x], q[i][1] = M[y];
	}
	for(int i=1;i<=cn;i++)par[i] = i, w[i] = 1;
	int ok = 1;
	for(int i=0;i<m;i++){
		int a = q[i][0], b = q[i][1];
		a = Find(a), b = Find(b);
		if(a == b){
			ok = 0;
			break;
		}
		par[a] = b;
		w[b] += w[a];
	}
	if(!ok){
		printf("0");
		return 0;
	}
	else{
		ll ans = 1, sum = n - cn;
		int cnt = n - cn;
		for(int i=1;i<=cn;i++){
			if(Find(i) == i){
				ans = ans * w[i] % MOD;
				sum = (sum + w[i]) % MOD;
				cnt++;
			}
		}
		if(cnt > 1)ans = ans * pw(sum, cnt-2) % MOD;
		else ans = 1;
		printf("%lld", ans);
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4068 KB Output is correct
2 Correct 0 ms 4068 KB Output is correct
3 Correct 0 ms 4068 KB Output is correct
4 Correct 0 ms 4068 KB Output is correct
5 Correct 0 ms 4068 KB Output is correct
6 Correct 0 ms 4068 KB Output is correct
7 Correct 0 ms 4068 KB Output is correct
8 Correct 0 ms 4068 KB Output is correct
9 Correct 0 ms 4068 KB Output is correct
10 Correct 0 ms 4068 KB Output is correct
11 Correct 0 ms 4068 KB Output is correct
12 Correct 0 ms 4068 KB Output is correct
13 Correct 0 ms 4068 KB Output is correct
14 Correct 0 ms 4068 KB Output is correct
15 Correct 0 ms 4068 KB Output is correct
16 Correct 0 ms 4068 KB Output is correct
17 Correct 0 ms 4068 KB Output is correct
18 Correct 0 ms 4068 KB Output is correct
19 Correct 0 ms 4068 KB Output is correct
20 Correct 0 ms 4068 KB Output is correct
21 Correct 0 ms 4068 KB Output is correct
22 Correct 0 ms 4068 KB Output is correct
23 Correct 0 ms 4068 KB Output is correct
24 Correct 0 ms 4068 KB Output is correct
25 Correct 0 ms 4068 KB Output is correct
26 Correct 0 ms 4068 KB Output is correct
27 Correct 0 ms 4068 KB Output is correct
28 Correct 0 ms 4068 KB Output is correct
29 Correct 0 ms 4068 KB Output is correct
30 Correct 0 ms 4068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 4596 KB Output is correct
2 Correct 36 ms 5256 KB Output is correct
3 Correct 35 ms 5388 KB Output is correct
4 Correct 12 ms 4596 KB Output is correct
5 Correct 26 ms 4992 KB Output is correct
6 Correct 179 ms 11196 KB Output is correct
7 Correct 182 ms 11460 KB Output is correct
8 Correct 126 ms 7104 KB Output is correct
9 Correct 189 ms 11592 KB Output is correct
10 Correct 175 ms 10668 KB Output is correct
11 Correct 152 ms 8424 KB Output is correct
12 Correct 158 ms 8424 KB Output is correct
13 Correct 151 ms 8424 KB Output is correct
14 Correct 152 ms 8424 KB Output is correct
15 Correct 165 ms 8556 KB Output is correct
16 Correct 159 ms 8556 KB Output is correct
17 Correct 163 ms 8556 KB Output is correct
18 Correct 0 ms 4068 KB Output is correct
19 Correct 159 ms 8688 KB Output is correct
20 Correct 149 ms 8688 KB Output is correct
21 Correct 146 ms 8424 KB Output is correct
22 Correct 143 ms 8556 KB Output is correct
23 Correct 150 ms 8688 KB Output is correct
24 Correct 156 ms 8688 KB Output is correct
25 Correct 124 ms 8688 KB Output is correct
26 Correct 120 ms 8688 KB Output is correct
27 Correct 182 ms 10536 KB Output is correct
28 Correct 169 ms 10536 KB Output is correct
29 Correct 165 ms 8688 KB Output is correct
30 Correct 158 ms 8688 KB Output is correct