Submission #570441

# Submission time Handle Problem Language Result Execution time Memory
570441 2022-05-29T23:48:24 Z aryan12 Usmjeri (COCI17_usmjeri) C++17
28 / 140
913 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 3e5 + 5, M = 3e5 + 5, MOD = 1e9 + 7;
int depth[N], min_depth[N];
vector<array<int, 3> > special_pairs(M);
vector<int> g[N];
vector<array<int, 2> > g2[N];
int dp[19][N];
int tin[N], tout[N], tim = 0;
int parent[N], color[N];
bool vis[N];

int power(int a, int b)
{
	if(b == 0) return 1;
	if(b == 1) return a;
	int x = power(a, b / 2);
	x = (x * x) % MOD;
	if(b & 1)
	{
		x = (x * a) % MOD;
	}
	return x;
}

void dfs(int node, int par)
{
	tin[node] = ++tim;
	dp[0][node] = par;
	for(int to: g[node])
	{
		if(to == par) continue;
		depth[to] = depth[node] + 1;
		min_depth[to] = depth[to];
		dfs(to, node);
	}
	tout[node] = ++tim;
}

int Find(int x)
{
	if(x == parent[x]) return x;
	return parent[x] = Find(parent[x]);
}

void Unite(int x, int y)
{
	x = Find(x), y = Find(y);
	parent[x] = y;
}

int LCA(int x, int y)
{
	if(depth[x] > depth[y]) swap(x, y);
	int diff = depth[y] - depth[x];
	for(int i = 18; i >= 0; i--)
	{
		if(diff & (1 << i))
		{
			y = dp[i][y];
		}
	}
	if(x == y) return x;
	for(int i = 18; i >= 0; i--)
	{
		if(dp[i][x] != dp[i][y])
		{
			x = dp[i][x];
			y = dp[i][y];
		}
	}
	return dp[0][x];
}

void again_dfs(int node, int par)
{
	for(int to: g[node])
	{
		if(to == par) continue;
		again_dfs(to, node);
		min_depth[node] = min(min_depth[node], min_depth[to]);
		if(min_depth[to] < depth[node])
		{
			g2[to].push_back({node, 0});
			g2[node].push_back({to, 0});
		}
	}
}

bool dfs_check(int node, int par, int cur_color)
{
	if(color[node] == 0 || color[node] == cur_color)
	{
		color[node] = cur_color;
	}
	if(color[node] != cur_color) return false;
	for(auto [to, wt]: g2[node])
	{
		if(to == par) continue;
		int next_color = (wt == 0) ? cur_color : 3 - cur_color;
		if(!dfs_check(to, node, next_color)) return false;
	}
	return true;
}

void Solve() 
{
	int n, m;
	cin >> n >> m;
	for(int i = 1; i < n; i++)
	{
		parent[i] = i;
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	parent[n] = n;
	depth[1] = 0;
	min_depth[1] = 0;
	dfs(1, -1);
	for(int i = 1; i < 19; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			if(dp[i - 1][j] == -1)
			{
				dp[i][j] = -1;
			}
			else
			{
				dp[i][j] = dp[i - 1][dp[i - 1][j]];
			}
		}
	}
	for(int i = 1; i <= m; i++)
	{
		int u, v;
		cin >> u >> v;
		int lca = LCA(u, v);
		special_pairs[i] = {u, lca, v};
		if(lca != u && lca != v)
		{
			g2[u].push_back({v, 1});
			g2[v].push_back({u, 1});
		}
	}
	again_dfs(1, -1);
	for(int i = 1; i <= m; i++)
	{
		// cout << special_pairs[i][0] << " " << special_pairs[i][1] << " " << special_pairs[i][2] << "\n";
		int cur = special_pairs[i][0];
		cur = Find(cur);
		while(depth[cur] > depth[special_pairs[i][1]])
		{
			Unite(cur, dp[0][cur]);
			cur = Find(cur);
		}
		cur = special_pairs[i][2];
		cur = Find(cur);
		while(depth[cur] > depth[special_pairs[i][1]])
		{
			Unite(cur, dp[0][cur]);
			cur = Find(cur);
		}
	}
	int components = 0;
	for(int i = 1; i <= n; i++)
	{
		if(Find(i) == i)
		{
			components++;
		}
	}
	for(int i = 2; i <= n; i++)
	{
		if(!vis[i] && !dfs_check(i, -1, 1))
		{
			cout << "0\n";
			return;
		}
	}
	cout << power(2LL, components) << "\n";
}

int32_t main() 
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++) 
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 106 ms 56684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 120640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 21844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 21920 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 22988 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 22908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 444 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 913 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 803 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 731 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -