This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
const int64_t MOD = 1000000007;
vector<int> grafo[MAXN];
int h[MAXN];
int parent[MAXN];
int in[MAXN];
int out[MAXN];
int dsu[MAXN];
int a,b;
int n,m;
int fast_read_int() {
int c, n = 0;
do c = getchar_unlocked();
while (c < '0' || c > '9' );
do {
n = (n << 3) + (n << 1) + (c - '0');
c = getchar_unlocked();
} while (c >= '0' && c <= '9');
return n;
}
inline int find(int x){
return dsu[x] = dsu[x] == x? x : find(dsu[x]);
}
inline void merge(int x,int y){
x = find(x);
y = find(y);
if(x == y) return ;
if(h[x] > h[y]) swap(x,y);
dsu[y] = x;
}
int cnt;
inline void get_h(int id,int prec,int k=0){
h[id] = k;
in[id] = cnt++;
parent[id] = prec;
for(auto el : grafo[id]){
if(el != prec){
get_h(el,id,k+1);
}
}
out[id] = cnt++;
}
inline bool is_ancestor(int a,int b){
return in[a] <= in[b] && out[b] <= out[a];
}
int main() {
n = fast_read_int();
m = fast_read_int();
for(int i=0;i<n-1;i++){
a = fast_read_int();
b = fast_read_int();
a--;
b--;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
get_h(0,-1);
//for(int i=0;i<n;i++) cout<<i<<": "<<h[i]<<endl;
for(int i=0;i<n;i++) dsu[i] = i;
for(int i=0;i<m;i++){
a = fast_read_int();
b = fast_read_int();
a--;b--;
if(find(a) == find(b)) continue;
//cout<<a<<" "<<b<<endl;
if(is_ancestor(a,b) || is_ancestor(b,a)){
int ra = a;
int rb = b;
a = find(a);
b = find(b);
while(a != b){
if(h[a] < h[b]) swap(a,b);
if(parent[a] == rb || parent[a] == ra) break;
merge(a,parent[a]);
a = find(a);
}
}else{
a = find(a);
b = find(b);
while(parent[b] != parent[a] && a != b){
if(h[a] < h[b]) swap(a,b);
merge(a,parent[a]);
a = find(a);
}
merge(a,b);
}
}
//for(int i=0;i<dsu.size();i++) cout<<i<<": "<<find(i)<<'\n';
int rs = 1;
for(int i=1;i<n;i++){
if(i == find(i)){
rs*=2,rs%=MOD;
}
}
printf("%d",rs);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |