Submission #880833

# Submission time Handle Problem Language Result Execution time Memory
880833 2023-11-30T06:39:32 Z serifefedartar Usmjeri (COCI17_usmjeri) C++17
28 / 140
315 ms 88120 KB
#include <bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 21;
const ll MAXN = 3e5 + 10000;

vector<vector<int>> graph;
vector<vector<pair<int,int>>> G;
int up[LOGN][MAXN], par[MAXN], sz[MAXN], depth[MAXN], p[MAXN];
int find(int node) {
	if (node == par[node])
		return node;
	return par[node] = find(par[node]);
}

void unite(int a, int b) {
	a = find(a);
	b = find(b);
	if (sz[b] > sz[a]) 
		swap(a, b);
	sz[a] += sz[b];
	par[b] = a;
	int pa = p[a];
	int pb = p[b];
	if (depth[pa] > depth[pb])
		p[a] = pb;
	else
		p[a] = pa;
}

void dfs(int node, int parent) {
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		depth[u] = depth[node] + 1;
		up[0][u] = node;
		for (int i = 1; i < LOGN; i++)
			up[i][u] = up[i-1][up[i-1][u]];
		dfs(u, node);
	}
}

int lift(int node, int k) {
	for (int i = 0; i < LOGN; i++) {
		if ((1<<i) & k)
			node = up[i][node];
	}
	return node;
}

int lca(int a, int b) {
	if (depth[b] > depth[a])
		swap(a, b);
	a = lift(a, depth[a] - depth[b]);
	if (a == b)
		return a;

	for (int i = LOGN - 1; i >= 0; i--) {
		if (up[i][a] != up[i][b]) {
			a = up[i][a];
			b = up[i][b];
		}
	}
	return up[0][a];
}

void merge(int a, int b) {
	a = find(a);
	int lst = p[a];
	while (depth[lst] > depth[b]) {
		a = find(a); 
		lst = up[0][lst];
		if (depth[lst] > depth[b]) {
			G[p[a]].push_back({lst, 0});
			G[lst].push_back({p[a], 1});
			unite(lst, a);
			lst = p[find(lst)];
		} else
			break;
	}
}

int c[MAXN], vis[MAXN];
bool q; 
void check(int node, int parent, int color) {
	if (vis[node])
		return ;
	vis[node] = true; 

	c[node] = color;
	for (auto u : G[node]) {
		if (u.f == parent)
			continue;
		int need = c[node] ^ u.s;
		if (c[u.f] != -1 && c[u.f] != need)
			q = false;
		else
			check(u.f, node, need);
	}
}

int main() {
	fast
	memset(c, -1, sizeof(c));
	int N, M, a, b;
	cin >> N >> M;

	graph = vector<vector<int>>(N+1, vector<int>());
	G = vector<vector<pair<int,int>>>(N+1, vector<pair<int,int>>());
	for (int i = 1; i < N; i++) {
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for (int i = 0; i < LOGN; i++)
		up[i][1] = 1;
	dfs(1, 1);
	depth[0] = 1e8;

	for (int i = 1; i <= N; i++)
		sz[i] = 1, par[i] = p[i] = i;
	for (int i = 0; i < M; i++) {
		cin >> a >> b;
		int LCA = lca(a, b);
		merge(a, LCA);
		merge(b, LCA);

		if (a != LCA && b != LCA) {
			G[a].push_back({b, 1});
			G[b].push_back({a, 1});
		}
	}

	ll ans = 1;
	for (int i = 2; i <= N; i++) {
		if (!vis[i]) {
			q = true;
			check(i, i, 0);
			if (q)
				ans = (ans * 2) % MOD;
			else
				ans = 0;
		}
	}
	cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 79 ms 51792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 88120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 31324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 31324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 32088 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 31836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 232 ms 67668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 279 ms 71764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 315 ms 72244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 282 ms 72784 KB Output isn't correct
2 Halted 0 ms 0 KB -