#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MOD = 1E9 + 7;
const int MAXN = 3E5 + 5;
const int LOG = 25;
int p[LOG][MAXN];
int depth[MAXN], tin[MAXN], tout[MAXN];
int get(int a, int b) {
for(int i = LOG -1; i >= 0; i--) {
if(b & (1 << i)) {
a = p[i][a];
}
}
return a;
}
struct DSU {
int n, *par;
DSU(int n) : n(n) {
// 23 orz
par = new int[n + 5];
iota(par, par + n + 5, 0);
}
int get(int x) {
if(par[x] == x) {
return x;
}
return par[x] = get(par[x]);
}
bool same(int a, int b) {
return get(a) == get(b);
}
bool unite(int u, int v) { // u -> v
if(same(u, v)) {
return false;
}
u = get(u);
v = get(v);
par[u] = v;
return true;
}
};
int lca(int a, int b) {
if(depth[a] > depth[b]) {
swap(a, b);
}
b = get(b, depth[b] - depth[a]);
if(a == b) {
return a;
}
for(int lg = LOG -1; lg >= 0; lg--) {
if(p[lg][a] != p[lg][b]) {
a = p[lg][a];
b = p[lg][b];
}
}
return p[0][a];
}
vector <pair <int, int>> adj[MAXN];
vector <pair <int, int>> bdj[MAXN];
map <pair <int, int>, int> edges;
map <int, pair <int, int>> redges;
pair <int, int> mp(int a, int b) {
return {min(a, b), max(a, b)};
}
#define ONLINE_JUDGE
void solve() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n -1; i++) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
edges[mp(u, v)] = i;
redges[i] = mp(u, v);
}
int timer = 0;
function <void(int, int)> dfs = [&](int node, int par) -> void {
tin[node] = timer++;
p[0][node] = par;
depth[node] = depth[par] +1;
for(auto &[child, col] : adj[node]) {
if(child != par) {
dfs(child, node);
}
}
tout[node] = timer++;
};
dfs(1, 0);
for(int lg = 1; lg < LOG; lg++) {
for(int i = 1; i <= n; i++) {
p[lg][i] = p[lg -1][p[lg -1][i]];
}
}
DSU dsu(n +1), zipzip(n +1);
function <void(int, int)> merge = [&](int node, int par) -> void {
vector <int> vec;
vec.emplace_back(edges[mp(node, p[0][node])]);
node = zipzip.get(node);
while(depth[node] > depth[par]) {
vec.emplace_back(edges[mp(node, p[0][node])]);
if(p[0][node] != par) {
vec.emplace_back(edges[mp(p[0][node], p[1][node])]);
}
node = p[0][node];
node = zipzip.get(node);
}
for(int i = 0; i +1 < int(vec.size()); i++) {
dsu.unite(vec[i], vec[i +1]);
bdj[vec[i]].emplace_back(vec[i +1], 0);
bdj[vec[i +1]].emplace_back(vec[i], 0);
}
for(int i = 2; i + 2 < int(vec.size()); i++) {
auto [a, b] = redges[vec[i]];
if(depth[a] < depth[b]) {
swap(a, b);
}
zipzip.unite(a, b);
}
return;
};
vector <array <int, 3>> queries(m +1);
for(int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
int c = lca(a, b);
queries[i] = {a, b, c};
if(a != c) {
merge(a, c);
}
if(b != c) {
merge(b, c);
}
if(a != c && b != c) {
bdj[edges[mp(a, p[0][a])]].emplace_back(edges[mp(b, p[0][b])], 1);
bdj[edges[mp(b, p[0][b])]].emplace_back(edges[mp(a, p[0][a])], 1);
}
}
vector <int> colors(n +1, -1);
function <void(int, int, int)> paint = [&](int node, int par, int col) -> void {
if(colors[node] != -1) {
if(colors[node] != col) {
cerr << node << " " << par << "\n";
cout << 0;
exit(0);
}
return;
}
colors[node] = col;
for(auto [child, c] : bdj[node]) {
if(child != par) {
dsu.unite(child, node);
paint(child, node, col ^ c);
}
}
return;
};
for(int i = 1; i <= n -1; i++) {
if(colors[i] == -1) {
paint(i, 0, 0);
}
}
for(int i = 1; i <= n -1; i++) {
for(auto [child, c] : bdj[i]) {
if(colors[i] != colors[child] ^ c) {
cerr << i << " " << child << " -> " << colors[i] << " " << colors[child] << " :: " << c << "\n";
cout << 0;
exit(0);
}
}
}
int ans = 1;
for(int i = 1; i <= n -1; i++) {
if(dsu.get(i) == i) {
ans = (ans * 2) % MOD;
}
}
cout << ans;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
Compilation message
usmjeri.cpp: In function 'void solve()':
usmjeri.cpp:210:26: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
210 | if(colors[i] != colors[child] ^ c) {
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
74648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
356 ms |
131160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
43864 KB |
Output is correct |
2 |
Correct |
9 ms |
44124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
43888 KB |
Output is correct |
2 |
Correct |
10 ms |
44124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
44892 KB |
Output is correct |
2 |
Correct |
12 ms |
44636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
44852 KB |
Output is correct |
2 |
Correct |
11 ms |
44632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
757 ms |
115920 KB |
Output is correct |
2 |
Correct |
828 ms |
118020 KB |
Output is correct |
3 |
Correct |
537 ms |
90944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1056 ms |
127688 KB |
Output is correct |
2 |
Correct |
988 ms |
129296 KB |
Output is correct |
3 |
Correct |
747 ms |
101376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
951 ms |
130144 KB |
Output is correct |
2 |
Correct |
830 ms |
115380 KB |
Output is correct |
3 |
Correct |
713 ms |
98432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
919 ms |
122184 KB |
Output is correct |
2 |
Correct |
951 ms |
127248 KB |
Output is correct |
3 |
Correct |
553 ms |
89684 KB |
Output is correct |