This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author : pham van sam
* created : 22 June 2023 (Thursday)
**/
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)
template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
const int MAXN = 3e5 + 5;
int N, M, depth[MAXN], anc[MAXN][20], Min[MAXN];
vector <int> adj[MAXN];
void dfs(int u, int p) {
anc[u][0] = p;
depth[u] = Min[u] = depth[p] + 1;
FOR(i, 1, 19) anc[u][i] = anc[anc[u][i - 1]][i - 1];
for (auto v : adj[u]) if(v != p) {
dfs(v, u);
}
}
int lca(int u, int v) {
if(depth[u] < depth[v]) swap(u, v);
FORD(i, 19, 0) if(depth[anc[u][i]] >= depth[v]) {
u = anc[u][i];
}
FORD(i, 19, 0) if(anc[u][i] != anc[v][i]) {
u = anc[u][i];
v = anc[v][i];
}
while(u != v) {
u = anc[u][0];
v = anc[v][0];
// cout << u << " " << v << '\n';
}
return u;
}
vector <pair <int, int>> ke[MAXN];
void add_edges(int u, int v, int c) {
ke[u].emplace_back(v, c);
ke[v].emplace_back(u, c);
}
void DFS(int u, int p) {
for (auto v : adj[u]) if(v != p) {
DFS(v, u);
minimize(Min[u], Min[v]);
if(Min[v] < depth[u]) add_edges(u, v, 0);
}
}
int used[MAXN];
bool calc(int u, int c) {
if(used[u] != -1) return used[u] == c;
used[u] = c;
for (auto [v, x] : ke[u]) {
if(!calc(v, x ^ c)) return false;
}
return true;
}
void process(void) {
cin >> N >> M;
FOR(i, 2, N) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
while(M--) {
int u, v; cin >> u >> v;
int x = lca(u, v);
minimize(Min[u], depth[x]);
minimize(Min[v], depth[x]);
if(x != u && x != v) add_edges(u, v, 1);
}
DFS(1, 0);
memset(used, -1, sizeof used);
int res = 1, Mod = 1e9 + 7;
FOR(i, 2, N) if(used[i] == -1) {
if(!calc(i, 0)) return void(cout << 0);
(res *= 2) %= Mod;
}
cout << res;
}
signed main() {
#define TASK "usmjeri"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
auto start_time = steady_clock::now();
int test = 1;
// cin >> test;
for (int i = 1; i <= test; ++i) {
process();
// cout << '\n';
}
auto end_time = steady_clock::now();
cerr << "\nExecution time : " << duration_cast<milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
Compilation message (stderr)
usmjeri.cpp: In function 'int main()':
usmjeri.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
usmjeri.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |