#include <bits/stdc++.h>
#define pii pair<ll, ll>
#define mp make_pair
#define F first
#define S second
#define PB push_back
#define FOR(i, u, v) for (int i = u; i <= v; i++)
#define FORD(i, v, u) for (int i = v; i >= u; i--)
#define ll long long
#define N 300005
#define LN 20
#define maxc 1000000000000000007
using namespace std;
int n, m, h[N], high[N], p[N][LN], dd[N];
vector<int> a[N];
vector<pii> g[N];
void setup()
{
cin >> n >> m;
FOR(i, 2, n)
{
int u, v; cin >> u >> v;
a[u].PB(v);
a[v].PB(u);
}
}
void preDFS(int u)
{
for (auto v : a[u])
{
if (v == p[u][0]) continue;
p[v][0] = u;
h[v] = h[u] + 1;
FOR(i, 1, LN-1)
p[v][i] = p[p[v][i-1]][i-1];
preDFS(v);
}
}
int LCA(int u, int v)
{
if (h[u] > h[v]) swap(u, v);
int delta = h[v] - h[u];
FOR(i, 0, LN-1)
if ((delta >> i) & 1) v = p[v][i];
if (u == v) return u;
FORD(i, LN-1, 0)
if (p[u][i] != p[v][i])
{
u = p[u][i];
v = p[v][i];
}
return p[u][0];
}
void add_edge(int u, int v, int val)
{
g[u].PB(mp(v, val));
g[v].PB(mp(u, val));
}
int connect(int u)
{
for (auto v : a[u])
{
if (v == p[u][0]) continue;
int temp = connect(v);
high[u] = min(temp, high[u]);
if (temp < h[u])
add_edge(u, v, 0);
}
return high[u];
}
int DFS(int u, int val)
{
dd[u] = val;
bool flag = 1;
for (auto z : g[u])
{
int v = z.F;
int w = z.S;
if (dd[v] == -1) flag &= DFS(v, val^w);
else if (dd[v] != (val^w)) return 0;
}
return 1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("INP.TXT", "r", stdin);
setup();
preDFS(1);
FOR(i, 1, n) high[i] = h[i];
while (m--)
{
int u, v; cin >> u >> v;
int lca= LCA(u, v);
high[u] = min(high[u], h[lca]);
high[v] = min(high[v], h[lca]);
if (u != lca && v != lca)
add_edge(u, v, 1);
}
connect(1);
memset(dd, -1, sizeof dd);
int res = 1;
FOR(i, 2, n)
if (dd[i] == -1)
{
if (!DFS(i, 0))
res = 0;
else res = (res*2) % 1000000007;
}
cout <<res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
38648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
334 ms |
81780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
81780 KB |
Output is correct |
2 |
Incorrect |
17 ms |
81780 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
81780 KB |
Output is correct |
2 |
Incorrect |
19 ms |
81780 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
81780 KB |
Output is correct |
2 |
Correct |
19 ms |
81780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
81780 KB |
Output is correct |
2 |
Correct |
20 ms |
81780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
493 ms |
81780 KB |
Output is correct |
2 |
Correct |
530 ms |
86076 KB |
Output is correct |
3 |
Correct |
319 ms |
86076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
633 ms |
103080 KB |
Output is correct |
2 |
Correct |
610 ms |
110808 KB |
Output is correct |
3 |
Correct |
380 ms |
110808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
560 ms |
124744 KB |
Output is correct |
2 |
Correct |
485 ms |
124744 KB |
Output is correct |
3 |
Incorrect |
386 ms |
124744 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
607 ms |
145076 KB |
Output is correct |
2 |
Correct |
535 ms |
153796 KB |
Output is correct |
3 |
Correct |
348 ms |
153796 KB |
Output is correct |