This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//*
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<algorithm>
#include<vector>
#define all(A) (A).begin(), (A).end()
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MM = 200000;
const ll mod = 1000000007;
ll N;
int M;
int A[MM],B[MM];
int par[MM];
int cnt[MM];
int nN;
int getpar(int n){
if(par[n]==n)return n;
return par[n]=getpar(par[n]);
}
bool merge(int a,int b){
if(getpar(a)==getpar(b))return true;
par[getpar(a)]=getpar(b);
return false;
}
ll mpow(ll a,ll p){
ll r=1;
while(p>0){
if(p&1)r=r*a%mod;
a=a*a%mod;
p>>=1;
}
return r;
}
int main(){
scanf("%lld%d",&N,&M);
vector<int> nodes;
for(int i=0;i<M;i++){
scanf("%d%d",&A[i],&B[i]);
nodes.push_back(A[i]),nodes.push_back(B[i]);
}
sort(all(nodes));
nodes.resize(unique(all(nodes))-nodes.begin());
for(int i=0;i<M;i++){
A[i]=lower_bound(all(nodes),A[i])-nodes.begin();
B[i]=lower_bound(all(nodes),B[i])-nodes.begin();
}
nN=nodes.size();
for(int i=0;i<nN;i++)par[i]=i;
for(int i=0;i<M;i++){
if(merge(A[i],B[i]))return !printf("0\n");
}
for(int i=0;i<nN;i++){
cnt[getpar(i)]++;
}
ll ans=mpow(N,N-M-2);
for(int i=0;i<nN;i++)if(par[i]==i){
ans*=cnt[i];
ans%=mod;
}
printf("%lld\n",ans);
return 0;
}
//*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |