#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, sz;
BinaryJumps t;
void dfs(int v, int p) {
par[v] = p;
sz[v] = 1;
// if (v == 6879) {
// cout << "here\n";
// }
for (auto u : g[v]) {
if (u != p) {
t.add_leaf(u, v);
dfs(u, v);
sz[v] += sz[u];
}
}
if (p != -1) {
g[v].erase(find(g[v].begin(), g[v].end(), p));
}
sort(g[v].begin(), g[v].end(), [&](int a, int b) {
return sz[a] > sz[b];
});
}
vector<int> tour, tin, tout, head;
void dfs_hld(int v) {
tin[v] = tour.size();
tour.push_back(v);
for (auto u : g[v]) {
if (u == g[v][0]) {
head[u] = head[v];
} else {
head[u] = u;
}
dfs_hld(u);
}
tout[v] = tour.size();
}
bool is_anc(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
vector<vector<pair<int, bool>>> gr;
int n;
void add_edge(int u, int v, bool c) {
gr[u].push_back({v, c});
gr[v].push_back({u, c});
}
void build1(int v, int l, int r) {
if (l + 1 == r) {
gr[v + n].push_back({tour[l], 1});
return;
}
gr[v + n].push_back({2 * v + 1 + n, 1});
gr[v + n].push_back({2 * v + 2 + n, 1});
int m = (l + r) / 2;
build1(2 * v + 1, l, m);
build1(2 * v + 2, m, r);
}
void add_edges1(int v, int l, int r, int li, int ri, int i) {
if (li >= r || l >= ri) {
return;
}
if (li <= l && r <= ri) {
gr[i].push_back({v + n, 1});
return;
}
int m = (l + r) / 2;
add_edges1(2 * v + 1, l, m, li, ri, i);
add_edges1(2 * v + 2, m, r, li, ri, i);
}
void build2(int v, int l, int r) {
if (l + 1 == r) {
gr[tour[l]].push_back({v + 5 * n, 1});
return;
}
gr[2 * v + 1 + 5 * n].push_back({v + 5 * n, 1});
gr[2 * v + 2 + 5 * n].push_back({v + 5 * n, 1});
int m = (l + r) / 2;
build2(2 * v + 1, l, m);
build2(2 * v + 2, m, r);
}
void add_edges2(int v, int l, int r, int li, int ri, int i) {
if (li >= r || l >= ri) {
return;
}
if (li <= l && r <= ri) {
gr[v + 5 * n].push_back({i, 1});
return;
}
int m = (l + r) / 2;
add_edges2(2 * v + 1, l, m, li, ri, i);
add_edges2(2 * v + 2, m, r, li, ri, i);
}
void add_edges_stupid(int l, int r, int v) {// [l; r)
for (int i = l; i < r; ++i) {
int u = tour[i];
add_edge(u, v, 1);
}
}
void add_edges(int li, int ri, int i) {
add_edges1(0, 0, n, li, ri, i);
add_edges2(0, 0, n, li, ri, i);
// add_edges_stupid(li, ri, i);
}
vector<int> color;
bool ok = 1;
void dfs2(int v) {
if (!ok) {
return;
}
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 (u < n && color[u] != cur) {
ok = 0;
return;
}
}
}
}
struct DSU {
vector<int> p;
DSU(int n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
int get(int v) {
if (p[v] == v) {
return v;
}
return p[v] = get(p[v]);
}
void unite(int u, int v) {
u = get(u);
v = get(v);
if (u == v) {
return;
}
p[v] = u;
add_edge(u, v, 1);
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int q;
cin >> n >> q;
g.resize(n);
for (int i = 0; i < n - 1; ++i) {
int u = i, v = i + 1;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
t = BinaryJumps(n);
par.resize(n);
sz.resize(n);
dfs(0, -1);
head.resize(n);
tin.resize(n);
tout.resize(n);
tour.reserve(n);
dfs_hld(0);
gr.resize(n);
// build1(0, 0, n);
// build2(0, 0, n);
DSU d(n);
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];
// }
int uu = t.up_k(u, t.depth[u] - t.depth[v] - 1);
// assert(head[u] == 0);
{
// int cur = u;
// while (!is_anc(head[cur], uu)) {
// add_edges(tin[head[cur]], tin[cur] + 1, u);
// cur = par[head[cur]];
// }
// add_edges(tin[uu], tin[cur] + 1, u);
int cur = u;
while (!is_anc(cur, uu)) {
int pp = d.p[cur];
if (is_anc(pp, uu)) {
break;
}
pp = par[pp];
d.unite(pp, cur);
cur = pp;
}
}
} else {
// int curu = u;
// while (par[curu] != l) {
// add_edge(curu, par[curu], 1);// equal
// curu = par[curu];
// }
// int curv = v;
// while (par[curv] != l) {
// add_edge(curv, par[curv], 1);// equal
// curv = par[curv];
// }
// add_edge(curu, curv, 0);
int uu = t.up_k(u, t.depth[u] - t.depth[l] - 1);
{
// int cur = u;
// while (!is_anc(head[cur], uu)) {
// add_edges(tin[head[cur]], tin[cur] + 1, u);
// cur = par[head[cur]];
// }
// add_edges(tin[uu], tin[cur] + 1, u);
int cur = u;
while (!is_anc(cur, uu)) {
int pp = d.p[cur];
if (is_anc(pp, uu)) {
break;
}
pp = par[pp];
d.unite(pp, cur);
cur = pp;
}
}
int vv = t.up_k(v, t.depth[v] - t.depth[l] - 1);
{
// int cur = v;
// while (!is_anc(head[cur], vv)) {
// add_edges(tin[head[cur]], tin[cur] + 1, v);
// cur = par[head[cur]];
// }
// add_edges(tin[vv], tin[cur] + 1, v);
int cur = v;
while (!is_anc(cur, vv)) {
int pp = d.p[cur];
if (is_anc(pp, vv)) {
break;
}
pp = par[pp];
d.unite(pp, cur);
cur = pp;
}
}
add_edge(uu, vv, 0);
}
}
// int ed = 0;
// for (int i = 0; i < 9 * n; ++i) {
// ed += gr[i].size();
// }
// cout << ed << " ";
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 |
1 |
Correct |
145 ms |
28368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
83716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1364 KB |
Output is correct |
2 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1368 KB |
Output is correct |
2 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
397 ms |
63484 KB |
Output is correct |
2 |
Correct |
468 ms |
70312 KB |
Output is correct |
3 |
Correct |
357 ms |
45928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
756 ms |
66144 KB |
Output is correct |
2 |
Correct |
608 ms |
72520 KB |
Output is correct |
3 |
Correct |
374 ms |
49032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
610 ms |
65872 KB |
Output is correct |
2 |
Correct |
405 ms |
69500 KB |
Output is correct |
3 |
Correct |
443 ms |
49140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
673 ms |
67516 KB |
Output is correct |
2 |
Correct |
436 ms |
73260 KB |
Output is correct |
3 |
Correct |
313 ms |
46920 KB |
Output is correct |