답안 #19928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
19928 2016-02-25T07:20:56 Z Qwaz 트리 (kriii4_X) C++
100 / 100
554 ms 28136 KB
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef long long ll;

const ll mod = 1000000007LL;

map< int, vector<int> > neigh;
map< int, bool > vis;
vector<int> special_vertices;

int times_mod(int a, int b, ll mod) {
	return ll(a) * ll(b) % mod;
}

// solves a*x + b*y = d = gcd(a, b)
// if d == 1, x is a modular inverse of a mod b
void extended_gcd(ll a, ll b, ll &d, ll &x, ll &y)
{
    if(a < 0) {extended_gcd(-a, b, d, x, y); x *= -1; return;}
    if(b < 0) {extended_gcd(a, -b, d, x, y); y *= -1; return;}
	x = 0, y = 1;
	ll lx = 1, ly = 0, frac, tmp;
	while(b)
	{
		frac = a / b; 
		tmp = a; a = b; b = tmp % b;
		tmp = x; x = lx - frac * x; lx = tmp;
		tmp = y; y = ly - frac * y; ly = tmp;
	}
	x = lx; y = ly; d = a;
}

// calculates x^-1 mod a
ll inv_mod(ll x, ll a)
{
    ll res, y, d;
    extended_gcd(x, a, d, res, y);
    return ((res % a) + a) % a;
}

ll pow_mod(ll a, ll b, ll n)
{
    if(b < 0) return inv_mod(pow_mod(a, -b, n), n);
	ll t, result = 1;
	for(t = a % n; b; t = times_mod(t, t, n), b >>= 1)
		if(b & 1) result = times_mod(result, t, n);

	return result;
}

void dfs(int v, int &num_vertex, int &sum_deg) {
	vis[v] = true;
	num_vertex++;
	vector<int> &vneigh = neigh[v];
	for (int i = 0; i < int(vneigh.size()); i++) {
		int u = vneigh[i];
		if(!vis[u])
			dfs(u, num_vertex, sum_deg);
		sum_deg++;
	}
}

int n, m;

int main() {
	cin >> n >> m;
	while(m--) {
		int a, b;
		cin >> a >> b;
		neigh[a].push_back(b);
		neigh[b].push_back(a);
		special_vertices.push_back(a);
		special_vertices.push_back(b);
	}

	int c = n;
	vector<int> e;
	for (int i = 0; i < int(special_vertices.size()); i++) {
		int v = special_vertices[i];
		if (!vis[v]) {
			int num_vertex = 0, sum_deg = 0;
			dfs(v, num_vertex, sum_deg);
			if (2 * (num_vertex - 1) != sum_deg) {
				cout << 0 << endl;
				return 0;
			}

			c -= (num_vertex - 1);
			e.push_back(num_vertex);
		}
	}

	/*
	cout << c << endl;
	for (int i = 0; i < int(e.size()); i++)
		cout << e[i] << " ";
	cout << endl;
	*/

	if (c == 1)
		cout << 1 << endl;
	else {
		int ans = pow_mod(n, c - 2, mod);
		for (int i = 0; i < int(e.size()); i++)
			ans = times_mod(ans, e[i], mod);
		cout << ans << endl;
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1724 KB Output is correct
2 Correct 0 ms 1724 KB Output is correct
3 Correct 0 ms 1724 KB Output is correct
4 Correct 0 ms 1724 KB Output is correct
5 Correct 0 ms 1724 KB Output is correct
6 Correct 0 ms 1724 KB Output is correct
7 Correct 0 ms 1724 KB Output is correct
8 Correct 0 ms 1724 KB Output is correct
9 Correct 0 ms 1724 KB Output is correct
10 Correct 0 ms 1724 KB Output is correct
11 Correct 0 ms 1724 KB Output is correct
12 Correct 0 ms 1724 KB Output is correct
13 Correct 0 ms 1724 KB Output is correct
14 Correct 0 ms 1724 KB Output is correct
15 Correct 0 ms 1724 KB Output is correct
16 Correct 0 ms 1724 KB Output is correct
17 Correct 0 ms 1724 KB Output is correct
18 Correct 0 ms 1724 KB Output is correct
19 Correct 0 ms 1724 KB Output is correct
20 Correct 0 ms 1724 KB Output is correct
21 Correct 0 ms 1724 KB Output is correct
22 Correct 0 ms 1724 KB Output is correct
23 Correct 0 ms 1724 KB Output is correct
24 Correct 0 ms 1724 KB Output is correct
25 Correct 0 ms 1724 KB Output is correct
26 Correct 0 ms 1724 KB Output is correct
27 Correct 0 ms 1724 KB Output is correct
28 Correct 0 ms 1724 KB Output is correct
29 Correct 0 ms 1724 KB Output is correct
30 Correct 0 ms 1724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 3848 KB Output is correct
2 Correct 88 ms 6076 KB Output is correct
3 Correct 96 ms 6472 KB Output is correct
4 Correct 30 ms 3736 KB Output is correct
5 Correct 59 ms 5416 KB Output is correct
6 Correct 536 ms 27112 KB Output is correct
7 Correct 515 ms 27528 KB Output is correct
8 Correct 317 ms 16620 KB Output is correct
9 Correct 554 ms 28136 KB Output is correct
10 Correct 501 ms 25204 KB Output is correct
11 Correct 418 ms 17576 KB Output is correct
12 Correct 423 ms 17444 KB Output is correct
13 Correct 437 ms 17444 KB Output is correct
14 Correct 428 ms 17444 KB Output is correct
15 Correct 453 ms 17708 KB Output is correct
16 Correct 340 ms 22008 KB Output is correct
17 Correct 293 ms 21076 KB Output is correct
18 Correct 0 ms 1724 KB Output is correct
19 Correct 470 ms 18368 KB Output is correct
20 Correct 445 ms 18368 KB Output is correct
21 Correct 434 ms 17448 KB Output is correct
22 Correct 439 ms 17840 KB Output is correct
23 Correct 441 ms 18372 KB Output is correct
24 Correct 461 ms 18372 KB Output is correct
25 Correct 335 ms 18416 KB Output is correct
26 Correct 358 ms 18408 KB Output is correct
27 Correct 536 ms 24800 KB Output is correct
28 Correct 529 ms 24800 KB Output is correct
29 Correct 429 ms 18372 KB Output is correct
30 Correct 463 ms 18504 KB Output is correct