답안 #628741

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
628741 2022-08-13T16:16:29 Z vovamr Šarenlist (COCI22_sarenlist) C++17
110 / 110
19 ms 468 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

constexpr int md = 1e9 + 7; constexpr int iv2 = (md + 1) / 2;
inline int add(int a, int b) { a += b; if(a>=md){a-=md;} return a; }
inline int sub(int a, int b) { a -= b; if(a<0){a+=md;} return a; }
inline int mul(int a, int b) { return (ll)a * b % md; }
inline int bp(int a, int n) { int res = 1;
	for (; n; n >>= 1, a = mul(a, a)) {
		if (n & 1) res = mul(res, a);
	} return res;
}
inline int inv(int a) { return bp(a, md - 2); }

const int N = 65;
int p[N], rnk[N];
inline void build(int n) { for (int i = 0; i < n; ++i) p[i] = i, rnk[i] = 1; }
inline int par(int v) { if (p[v] == v) { return v; } return (p[v] = par(p[v])); }
inline bool uni(int a, int b) {
	a = par(a), b = par(b);
	if (a == b) return 0;
	if (rnk[a] > rnk[b]) swap(a, b);
	p[a] = b, rnk[b] += rnk[a]; return 1;
}

ve<int> gr[N];
int lca[N][N], h[N], ac[N];
inline void dfs(int v, int p) {
	for (auto &to : gr[v]) {
		if (to == p) continue;
		ac[to] = v, h[to] = h[v] + 1;
		dfs(to, v);
	}
}

inline void solve() {
	int n, m, k;
	cin >> n >> m >> k;
	for (int i = 1; i < n; ++i) {
		int v, u;
		cin >> v >> u, --v, --u;
		gr[v].pb(u), gr[u].pb(v);
	}

	ve<pii> a(m);
	for (auto &[v, u] : a) cin >> v >> u, --v, --u;

	dfs(0, 0);
	for (int v = 0; v < n; ++v) {
		for (int u = 0; u < n; ++u) {
			if (v == u) lca[v][u] = v;
			else {
				int x = v, y = u;
				while (x != y) {
					if (h[x] < h[y]) swap(x, y);
					x = ac[x];
				}
				lca[v][u] = x;
			}
		}
	}

//	for (int i = 0; i < n; ++i) {
//		for (int j = 0; j < n; ++j) {
//			cout << i + 1 << " " << j + 1 << " -> " << lca[i][j] + 1 << '\n';
//		}
//	}
//	cout << '\n';

	int ans = 0;

//	cout << '\n';
	for (int ma = 0; ma < (1 << m); ++ma) {
		build(n);
		for (int i = 0; i < m; ++i) {
			if (ma >> i & 1 ^ 1) continue;
			int x = a[i].fi, y = a[i].se;
			int l = lca[x][y];
			if (x != l && y != l) uni(x, y);

			while (x != l) {
				if (ac[x] != l) uni(x, ac[x]);
				x = ac[x];
			}
			while (y != l) {
				if (ac[y] != l) uni(y, ac[y]);
				y = ac[y];
			}
		}
		int comp = 0;
		for (int i = 1; i < n; ++i) comp += (par(i) == i);

//		cout << (bitset<3>(ma)) << " " << comp << '\n';
		
		if (__builtin_popcount(ma) & 1 ^ 1) ans = add(ans, bp(k, comp));
		else ans = sub(ans, bp(k, comp));
	}

	cout << ans;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:95:16: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   95 |    if (ma >> i & 1 ^ 1) continue;
      |        ~~~~~~~~^~~
Main.cpp:114:30: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  114 |   if (__builtin_popcount(ma) & 1 ^ 1) ans = add(ans, bp(k, comp));
      |       ~~~~~~~~~~~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 448 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 12 ms 352 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 9 ms 348 KB Output is correct
31 Correct 3 ms 352 KB Output is correct
32 Correct 2 ms 336 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 19 ms 340 KB Output is correct
37 Correct 9 ms 340 KB Output is correct