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;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll INFLL = numeric_limits<ll>::max() / 2;
template<class T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
constexpr int mod = 1e9 + 7;
struct BinaryJumps {
static constexpr int k = 20;
vector<vector<int>> up;
vector<int> depth;
BinaryJumps() {
}
BinaryJumps(int n) {
up.resize(k, vector<int>(n));
depth.resize(n);
}
void add_leaf(int v, int p) {
up[0][v] = p;
depth[v] = depth[p] + 1;
for (int i = 1; i < k; ++i) {
up[i][v] = up[i - 1][up[i - 1][v]];
}
}
int up_k(int v, int d) {
for (int i = k - 1; i >= 0; --i) {
if (d >= (1 << i)) {
v = up[i][v];
d -= (1 << i);
}
}
return v;
}
int LCA(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
u = up_k(u, depth[u] - depth[v]);
for (int i = k - 1; i >= 0; --i) {
if (up[i][u] != up[i][v]) {
u = up[i][u];
v = up[i][v];
}
}
if (u == v) {
return u;
}
return up[0][u];
}
};
vector<vector<int>> g;
vector<int> par;
BinaryJumps t;
void dfs(int v, int p) {
par[v] = p;
for (auto u : g[v]) {
if (u != p) {
t.add_leaf(u, v);
dfs(u, v);
}
}
}
vector<vector<pair<int, int>>> gr;
vector<int> color;
bool ok = 1;
void dfs2(int v) {
for (auto [u, c] : gr[v]) {
int cur = (c ? color[v] : color[v] ^ 1);
if (color[u] == -1) {
color[u] = cur;
dfs2(u);
} else {
if (color[u] != cur) {
ok = 0;
}
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
g.resize(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<pair<int, int>> v;
for (int i = 0; i < q; ++i) {
int a, b;
cin >> a >> b;
v.push_back({a, 1});
v.push_back({b, -1});
}
sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b) {
return a.first < b.first || (a.first == b.first && a.second > b.second);
});
int sum = 0, balance = 0, l = 0, r = 0, ans = 1;
for (int i = 0; i < (int) v.size(); ++i) {
balance += v[i].second;
if (i != 0 && balance == 0) {
sum += v[r].first - v[l].first;
l = r + 1;
++r;
ans = (ans * 2ll) % mod;
} else {
++r;
}
}
for (int i = 0; i < n - 1 - sum; ++i) {
ans = (ans * 2ll) % mod;
}
cout << ans << "\n";
// t = BinaryJumps(n);
// par.resize(n);
// dfs(0, -1);
// gr.resize(n);
// auto add_edge = [&](int u, int v, int c) {
// gr[u].push_back({v, c});
// gr[v].push_back({u, c});
// };
// while (q--) {
// int u, v;
// cin >> u >> v;
// --u;
// --v;
// if (t.depth[u] < t.depth[v]) {
// swap(u, v);
// }
// int l = t.LCA(u, v);
// if (l == v) {// verticals
// int curu = u;
// while (par[curu] != l) {
// add_edge(curu, par[curu], 1);// equal
// curu = par[curu];
// }
// } else {
// int curu = u;
// while (par[curu] != l) {
// add_edge(curu, par[curu], 1);// equal
// curu = par[curu];
// }
// int curv = u;
// while (par[curv] != l) {
// add_edge(curv, par[curv], 1);// equal
// curv = par[curv];
// }
// add_edge(curu, curv, 0);
// }
// }
// int ans = 1;
// color.resize(n, -1);
// for (int i = 1; i < n; ++i) {
// if (color[i] == -1) {
// color[i] = 0;
// dfs2(i);
// ans = (ans * 2ll) % mod;
// }
// }
// if (!ok) {
// cout << "0\n";
// } else {
// cout << ans << "\n";
// }
}
# | 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... |