답안 #19258

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
19258 2016-02-23T06:12:05 Z kaTkaHr 트리 (kriii4_X) C++14
100 / 100
199 ms 10300 KB
#include <stdio.h>
#include<vector>
#include <algorithm>
#include <map>

using namespace std;

typedef long long ll;

const int MX = 200005, MM = 1000000007;

ll pw(ll A, ll B){
    ll R = 1;
    while(B){
        if( B&1 ) R = R * A % MM;
        A = A * A % MM; B /= 2;
    }
    return R;
}

ll rv(ll A){ return pw(A, MM-2); }

struct frac{
    ll A, B;
		frac(ll A):A(A), B(1){}
    frac(ll a, ll b){
        A = (a%MM+MM) % MM;
        B = (b%MM+MM) % MM;
    }
    frac(){A = 0, B = 1;}
    frac operator+ (const frac &l)const{
        return frac((A * l.B + B * l.A) % MM, B * l.B % MM);
    }
    frac operator*(const frac &l)const{
        return frac(A*l.A % MM, B*l.B % MM);
    }
    frac operator/(const frac &l)const{
        return frac(A*l.B % MM, B*l.A % MM);
    }
    frac operator- (const frac &l)const{
        return frac((A*l.B - B*l.A%MM + MM) % MM, B*l.B % MM);
    }
    ll v(){ return A * rv(B) % MM; }
};

map<int, int> label;
struct UF{
	int p[MX], cnt[MX];
	UF(){ fill(cnt, cnt+MX, 1); }
	int find(int x){ return !p[x]? x : p[x] = find(p[x]); }
	int merge(int x, int y){
		x = find(x), y = find(y);
		if( x == y ) return false;
		p[x] = y;
		cnt[y] += cnt[x];
		return true;
	}
}uf;

int main()
{
	int N, M, cnt = 0;
	scanf("%d%d", &N, &M);
	for(int i = 1; i <= M; i++){
		int A, B;
		scanf("%d%d", &A, &B);
		if( label[A] == 0 ) label[A] = ++cnt;
		if( label[B] == 0 ) label[B] = ++cnt;
		A = label[A], B = label[B];
		if( !uf.merge(A, B) ) return !printf("0");
	}
	frac ans = 1, tot = 0;
	for(int i = 1; i <= cnt; i++){
		if( i == uf.find(i) ) ans = ans * uf.cnt[i];
	}
	ans = ans * pw(N, N-M-2);
	if(M+1 == N) return !printf("1");
	printf("%lld\n", ans.v());
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2776 KB Output is correct
2 Correct 0 ms 2776 KB Output is correct
3 Correct 0 ms 2776 KB Output is correct
4 Correct 0 ms 2776 KB Output is correct
5 Correct 0 ms 2776 KB Output is correct
6 Correct 0 ms 2776 KB Output is correct
7 Correct 0 ms 2776 KB Output is correct
8 Correct 0 ms 2776 KB Output is correct
9 Correct 0 ms 2776 KB Output is correct
10 Correct 0 ms 2776 KB Output is correct
11 Correct 0 ms 2776 KB Output is correct
12 Correct 0 ms 2776 KB Output is correct
13 Correct 0 ms 2776 KB Output is correct
14 Correct 0 ms 2776 KB Output is correct
15 Correct 0 ms 2776 KB Output is correct
16 Correct 0 ms 2776 KB Output is correct
17 Correct 0 ms 2776 KB Output is correct
18 Correct 0 ms 2776 KB Output is correct
19 Correct 0 ms 2776 KB Output is correct
20 Correct 0 ms 2776 KB Output is correct
21 Correct 0 ms 2776 KB Output is correct
22 Correct 0 ms 2776 KB Output is correct
23 Correct 0 ms 2776 KB Output is correct
24 Correct 0 ms 2776 KB Output is correct
25 Correct 0 ms 2776 KB Output is correct
26 Correct 0 ms 2776 KB Output is correct
27 Correct 0 ms 2776 KB Output is correct
28 Correct 0 ms 2776 KB Output is correct
29 Correct 0 ms 2776 KB Output is correct
30 Correct 0 ms 2776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3304 KB Output is correct
2 Correct 31 ms 3964 KB Output is correct
3 Correct 33 ms 4096 KB Output is correct
4 Correct 14 ms 3304 KB Output is correct
5 Correct 22 ms 3700 KB Output is correct
6 Correct 198 ms 9904 KB Output is correct
7 Correct 189 ms 10168 KB Output is correct
8 Correct 37 ms 4492 KB Output is correct
9 Correct 199 ms 10300 KB Output is correct
10 Correct 183 ms 9376 KB Output is correct
11 Correct 134 ms 7132 KB Output is correct
12 Correct 165 ms 7132 KB Output is correct
13 Correct 158 ms 7132 KB Output is correct
14 Correct 164 ms 7132 KB Output is correct
15 Correct 168 ms 7264 KB Output is correct
16 Correct 159 ms 7264 KB Output is correct
17 Correct 156 ms 7264 KB Output is correct
18 Correct 0 ms 2776 KB Output is correct
19 Correct 161 ms 7396 KB Output is correct
20 Correct 168 ms 7396 KB Output is correct
21 Correct 157 ms 7132 KB Output is correct
22 Correct 164 ms 7264 KB Output is correct
23 Correct 161 ms 7396 KB Output is correct
24 Correct 162 ms 7396 KB Output is correct
25 Correct 115 ms 7396 KB Output is correct
26 Correct 123 ms 7396 KB Output is correct
27 Correct 188 ms 9244 KB Output is correct
28 Correct 181 ms 9244 KB Output is correct
29 Correct 164 ms 7396 KB Output is correct
30 Correct 159 ms 7396 KB Output is correct