Submission #937545

# Submission time Handle Problem Language Result Execution time Memory
937545 2024-03-04T08:18:13 Z IBory Training (IOI07_training) C++17
100 / 100
56 ms 9176 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;

const int MAX = 5005, BIT = 1 << 10;
vector<int> G[MAX], T[MAX];
pii E[MAX];
int P[MAX], D[MAX], C[MAX], R[MAX], idx[BIT][BIT], dp[BIT][BIT];
int in[MAX], out[MAX], dn;

void DFS(int cur, int prev) {
	if (prev) G[cur].erase(find(G[cur].begin(), G[cur].end(), prev));
	in[cur] = ++dn;
	D[cur] = D[prev] + 1;
	P[cur] = prev;
	for (int next : G[cur]) DFS(next, cur);
	out[cur] = dn;
}

int LCA(int a, int b) {
	if (D[a] > D[b]) swap(a, b);
	while (D[a] != D[b]) b = P[b];
	while (a != b) a = P[a], b = P[b];
	return a;
}

int Find(int cur, int child) {
	for (int i = 0; i < G[cur].size(); ++i) {
		int n = G[cur][i];
		if (in[n] <= in[child] && in[child] <= out[n]) return (1 << i);
	}
	return 0;
}

int Update(int p, int cur) {
	int c = 0, ret = 0;
	while (1) {
		ret += dp[cur][1023 - idx[cur][c]];
		c = cur;
		if (p == cur) return ret;
		cur = P[cur];
	}
}

void DFS2(int cur) {
	for (int next : G[cur]) DFS2(next);

	vector<pii> cost;
	for (int i = 0; i < G[cur].size(); ++i) {
		cost.emplace_back(1 << i, dp[G[cur][i]][1023]);
	}
	for (int i = G[cur].size(); i < 10; ++i) {
		cost.emplace_back(1 << i, 0);
	}

	for (int en : T[cur]) {
		auto [a, b] = E[en];
		int bit = 0, v = C[en];
		if (a != cur) bit |= Find(cur, a), v += Update(R[en], a);
		if (b != cur) bit |= Find(cur, b), v += Update(R[en], b);
		cost.emplace_back(bit, v);
	}

	for (int i = 0; i < BIT; ++i) {
		for (auto [b, n] : cost) {
			if (i & b) continue;
			dp[cur][i | b] = max(dp[cur][i | b], dp[cur][i] + n);
		}
	}

	// cout << cur << ' ' << dp[cur][1023] << '\n';
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, M, ans = 0, remain = 1;
	cin >> N >> M;
	for (int i = 1; i <= M; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		if (!c) {
			G[a].push_back(b);
			G[b].push_back(a);
		}
		else {
			E[remain] = {a, b};
			C[remain++] = c;
			ans += c;
		}
	}

	DFS(1, 0);
	for (int i = 1; i <= M - N + 1; ++i) {
		auto [a, b] = E[i];
		if (abs(D[a] - D[b]) & 1) continue;
		R[i] = LCA(a, b);
		T[R[i]].push_back(i);
	}
	for (int i = 1; i <= N; ++i) for (int j = 0; j < G[i].size(); ++j) idx[i][G[i][j]] = 1 << j;

	DFS2(1);
	ans -= dp[1][1023];
	cout << ans;
	return 0;
}

Compilation message

training.cpp: In function 'int Find(int, int)':
training.cpp:28:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  for (int i = 0; i < G[cur].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~~~
training.cpp: In function 'void DFS2(int)':
training.cpp:49:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |  for (int i = 0; i < G[cur].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~~~
training.cpp: In function 'int main()':
training.cpp:99:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |  for (int i = 1; i <= N; ++i) for (int j = 0; j < G[i].size(); ++j) idx[i][G[i][j]] = 1 << j;
      |                                               ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4712 KB Output is correct
2 Correct 2 ms 4704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7160 KB Output is correct
2 Correct 4 ms 7000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 9176 KB Output is correct
2 Correct 56 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4704 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4696 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7004 KB Output is correct
2 Correct 6 ms 7000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8024 KB Output is correct
2 Correct 15 ms 8060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 8668 KB Output is correct
2 Correct 26 ms 8800 KB Output is correct
3 Correct 25 ms 8828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 9052 KB Output is correct
2 Correct 50 ms 9136 KB Output is correct
3 Correct 52 ms 9136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 8800 KB Output is correct
2 Correct 28 ms 8716 KB Output is correct
3 Correct 55 ms 9048 KB Output is correct
4 Correct 28 ms 8800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 9048 KB Output is correct
2 Correct 56 ms 9060 KB Output is correct
3 Correct 52 ms 9052 KB Output is correct
4 Correct 53 ms 9040 KB Output is correct