Submission #21416

# Submission time Handle Problem Language Result Execution time Memory
21416 2017-04-13T19:26:29 Z gs14004 트리 (kriii4_X) C++11
100 / 100
116 ms 5988 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;

lint ipow(lint x, lint p){
	lint ret = 1, piv = x % mod;
	while(p){
		if(p&1) ret *= piv;
		piv *= piv;
		ret %= mod;
		piv %= mod;
		p >>= 1;
	}
	return ret % mod;
}

const int MAXN = 200005;
struct disj{
	int pa[MAXN], sz[MAXN];
	void init(int n){
		for(int i=0; i<=n; i++){
			pa[i] = i;
			sz[i] = 1;
		}
	}
	int find(int x){
		return pa[x] = (pa[x] == x ? x : find(pa[x]));
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		pa[q] = p;
		sz[p] += sz[q];
		return 1;
	}
}disj;

int n, m;
pi e[100005];
vector<int> v;

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0; i<m; i++){
		scanf("%d %d",&e[i].first, &e[i].second);
		v.push_back(e[i].first);
		v.push_back(e[i].second);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	disj.init(v.size());
	for(int i=0; i<m; i++){
		e[i].first = lower_bound(v.begin(), v.end(), e[i].first) - v.begin();
		e[i].second = lower_bound(v.begin(), v.end(), e[i].second) - v.begin();
		if(!disj.uni(e[i].first, e[i].second)){
			puts("0");
			return 0;
		}
	}
	if(n - 1 == m){
		puts("1");
		return 0;
	}
	lint ans = ipow(n, n - m - 2);
	for(int i=0; i<v.size(); i++){
		if(disj.find(i) == i){
			ans *= disj.sz[i];
			ans %= mod;
		}
	}
	cout << ans;
}

Compilation message

X.cpp: In function 'int main()':
X.cpp:68:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<v.size(); i++){
                ^
X.cpp:46:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
X.cpp:48:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&e[i].first, &e[i].second);
                                           ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4368 KB Output is correct
2 Correct 0 ms 4368 KB Output is correct
3 Correct 0 ms 4368 KB Output is correct
4 Correct 0 ms 4368 KB Output is correct
5 Correct 0 ms 4368 KB Output is correct
6 Correct 0 ms 4368 KB Output is correct
7 Correct 0 ms 4368 KB Output is correct
8 Correct 0 ms 4368 KB Output is correct
9 Correct 0 ms 4368 KB Output is correct
10 Correct 0 ms 4368 KB Output is correct
11 Correct 0 ms 4368 KB Output is correct
12 Correct 0 ms 4368 KB Output is correct
13 Correct 0 ms 4368 KB Output is correct
14 Correct 0 ms 4368 KB Output is correct
15 Correct 0 ms 4368 KB Output is correct
16 Correct 0 ms 4368 KB Output is correct
17 Correct 0 ms 4368 KB Output is correct
18 Correct 0 ms 4368 KB Output is correct
19 Correct 0 ms 4368 KB Output is correct
20 Correct 0 ms 4368 KB Output is correct
21 Correct 0 ms 4368 KB Output is correct
22 Correct 0 ms 4368 KB Output is correct
23 Correct 0 ms 4368 KB Output is correct
24 Correct 0 ms 4368 KB Output is correct
25 Correct 0 ms 4368 KB Output is correct
26 Correct 0 ms 4368 KB Output is correct
27 Correct 0 ms 4368 KB Output is correct
28 Correct 0 ms 4368 KB Output is correct
29 Correct 0 ms 4368 KB Output is correct
30 Correct 0 ms 4368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4640 KB Output is correct
2 Correct 19 ms 4836 KB Output is correct
3 Correct 19 ms 4836 KB Output is correct
4 Correct 0 ms 4640 KB Output is correct
5 Correct 13 ms 4836 KB Output is correct
6 Correct 79 ms 5988 KB Output is correct
7 Correct 86 ms 5988 KB Output is correct
8 Correct 66 ms 5988 KB Output is correct
9 Correct 79 ms 5988 KB Output is correct
10 Correct 79 ms 5988 KB Output is correct
11 Correct 83 ms 5988 KB Output is correct
12 Correct 99 ms 5988 KB Output is correct
13 Correct 79 ms 5988 KB Output is correct
14 Correct 86 ms 5988 KB Output is correct
15 Correct 76 ms 5988 KB Output is correct
16 Correct 83 ms 5988 KB Output is correct
17 Correct 83 ms 5988 KB Output is correct
18 Correct 0 ms 4368 KB Output is correct
19 Correct 86 ms 5988 KB Output is correct
20 Correct 99 ms 5988 KB Output is correct
21 Correct 76 ms 5988 KB Output is correct
22 Correct 86 ms 5988 KB Output is correct
23 Correct 83 ms 5988 KB Output is correct
24 Correct 86 ms 5988 KB Output is correct
25 Correct 63 ms 5988 KB Output is correct
26 Correct 59 ms 5988 KB Output is correct
27 Correct 89 ms 5988 KB Output is correct
28 Correct 116 ms 5988 KB Output is correct
29 Correct 86 ms 5988 KB Output is correct
30 Correct 89 ms 5988 KB Output is correct