Submission #851439

# Submission time Handle Problem Language Result Execution time Memory
851439 2023-09-19T18:42:22 Z serifefedartar Šarenlist (COCI22_sarenlist) C++17
10 / 110
1 ms 504 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 = 20;
const ll MAXN = 2e5 + 5;

vector<vector<pair<int,int>>> graph;
vector<vector<int>> edges;
bool found = false;
void dfs(int node, int parent, int target, int idx) {
	if (node == target) {
		found = true;
		return ;
	}

	if (found)
		return ;

	for (auto u : graph[node]) {
		if (found)
			return ;

		if (u.f != parent) {
			edges[idx].push_back(u.s);
			dfs(u.f, node, target, idx);
			if (!found)
				edges[idx].pop_back();
			else
				break;
		}
	}
}

ll expo(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * a) % MOD;
		a = (a * a) % MOD;
		b /= 2;
	}
	return res;
}

int par[65], sz[65];
int find(int node) {
	if (par[node] == node)
		return node;
	return par[node] = find(par[node]);
}

void unite(int a, int b) {
	a = find(a);
	b = find(b);
	if (a == b)
		return ;
	sz[a] += sz[b];
	par[b] = a; 
}

void dsu() {
	for (int i = 0; i <= 62; i++)
		par[i] = i, sz[i] = 1;
}

int main() {
	fast
	ll n, m, k, a, b;
	cin >> n >> m >> k;

	graph = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
	edges = vector<vector<int>>(m, vector<int>());
	for (int i = 1; i < n; i++) {
		cin >> a >> b;
		graph[a].push_back({b, i});
		graph[b].push_back({a, i});
	}

	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		found = false;
		dfs(a, a, b, i);
	}

	dsu();
	for (int i = 0; i < m; i++) {
		for (auto u : edges[i])
			unite(0, u);
	}

	ll ans = 0;
	const int mx_mask = (1<<m);
	for (int msk = 0; msk < mx_mask; msk++) {
		dsu();
		for (int j = 0; j < m; j++) {
			if (((1<<j) & msk) == 0)
				continue;
			for (auto u : edges[j])
				unite(0, u);
		}

		int s = sz[0] - 1;
		if (__builtin_popcount(msk) % 2 == 0)
			ans = (ans + expo(k, n-1-s) * (msk == 0 ? 1 : k)) % MOD;
		else {
			ans = (ans - expo(k, n-1-s) * (msk == 0 ? 1 : k) % MOD) % MOD;
			while (ans < 0)
				ans += MOD;
		}
	}
	cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 448 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Incorrect 1 ms 344 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 1 ms 504 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 448 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Incorrect 1 ms 344 KB Output isn't correct
13 Halted 0 ms 0 KB -