답안 #709774

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709774 2023-03-14T11:56:28 Z surgutti Min-max tree (BOI18_minmaxtree) C++14
100 / 100
102 ms 20000 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100000 + 11;

int n, k;
vector<pair<int, int>> adj[N];
vector<pair<int, int>> edges;
int par[N], jmp[N], dep[N];

void dfs(int u, int f) {
	jmp[u] = f;

	for (auto [v, id] : adj[u]) if (v != f) {
		par[v] = id;
		dep[v] = dep[u] + 1;
		dfs(v, u);
	}
}

int uf[N];

int get(int u) {
	return u == uf[u] ? u : uf[u] = get(uf[u]);
}

int mn[N], mx[N];

vector<int> adj_mat[N];

void jazda(int u, int v, int id, int val) {
	u = get(u);
	v = get(v);

	while (u != v) {

		if (dep[u] < dep[v])
			swap(u, v);

		if (val > 0)
			mx[par[u]] = + val;
		else
			mn[par[u]] = - val;

		adj_mat[id].push_back(par[u]);

		uf[u] = get(jmp[u]);
		
		u = get(u);
	}
}

int mat[N], vis[N], col = 1;

bool match(int u) {
	if (vis[u] == col)
		return false;
	vis[u] = col;

	for (int v : adj_mat[u])
		if (mat[v] == -1)
			return mat[v] = u, true;
	
	for (int v : adj_mat[u])
		if (match(mat[v]))
			return mat[v] = u, true;

	return false;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);

	cin >> n;

	for (int i = 0; i < n - 1; i++) {
		int u, v; cin >> u >> v;

		adj[u].emplace_back(v, i);
		adj[v].emplace_back(u, i);

		edges.emplace_back(u, v);
	}

	par[1] = -1;
	dfs(1, 1);

	cin >> k;

	vector<tuple<int, int, int, int>> mini, maxi;
	vector<int> values;
	for (int i = 0; i < k; i++) {
		char c; int u, v, z; cin >> c >> u >> v >> z;

		values.push_back(z);

		if (c == 'm')
			mini.emplace_back(- z, u, v, i);
		else
			maxi.emplace_back(+ z, u, v, i);
	}

	for (int i = 0; i < n - 1; i++)
		mn[i] = mx[i] = 1;

	sort(mini.begin(), mini.end());
	sort(maxi.begin(), maxi.end());

	for (int i = 1; i <= n; i++)
		uf[i] = i;

	for (auto [z, u, v, i] : mini)
		jazda(u, v, i, z);

	for (int i = 1; i <= n; i++)
		uf[i] = i;

	for (auto [z, u, v, i] : maxi)
		jazda(u, v, i, z);

	for (int i = 0; i < n - 1; i++)
		mat[i] = -1;

	for (int i = 0; i < k; i++) {
		if (!match(i)) {
			col++;
			match(i);
		}
	}

	for (int i = 0; i < n - 1; i++) {
		auto [u, v] = edges[i];
		
		if (mat[i] == -1)
			cout << u << ' ' << v << ' ' << mn[i] << '\n';
		else
			cout << u << ' ' << v << ' ' << values[mat[i]] << '\n';
	}
}

Compilation message

minmaxtree.cpp: In function 'void dfs(int, int)':
minmaxtree.cpp:15:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   15 |  for (auto [v, id] : adj[u]) if (v != f) {
      |            ^
minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:113:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  113 |  for (auto [z, u, v, i] : mini)
      |            ^
minmaxtree.cpp:119:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  119 |  for (auto [z, u, v, i] : maxi)
      |            ^
minmaxtree.cpp:133:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  133 |   auto [u, v] = edges[i];
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 4 ms 5040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 18660 KB Output is correct
2 Correct 83 ms 16340 KB Output is correct
3 Correct 88 ms 16860 KB Output is correct
4 Correct 90 ms 19160 KB Output is correct
5 Correct 90 ms 16764 KB Output is correct
6 Correct 95 ms 16728 KB Output is correct
7 Correct 84 ms 16236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 13592 KB Output is correct
2 Correct 63 ms 13264 KB Output is correct
3 Correct 64 ms 15040 KB Output is correct
4 Correct 67 ms 16116 KB Output is correct
5 Correct 65 ms 13816 KB Output is correct
6 Correct 75 ms 14188 KB Output is correct
7 Correct 69 ms 13388 KB Output is correct
8 Correct 62 ms 13216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 4 ms 5040 KB Output is correct
5 Correct 97 ms 18660 KB Output is correct
6 Correct 83 ms 16340 KB Output is correct
7 Correct 88 ms 16860 KB Output is correct
8 Correct 90 ms 19160 KB Output is correct
9 Correct 90 ms 16764 KB Output is correct
10 Correct 95 ms 16728 KB Output is correct
11 Correct 84 ms 16236 KB Output is correct
12 Correct 58 ms 13592 KB Output is correct
13 Correct 63 ms 13264 KB Output is correct
14 Correct 64 ms 15040 KB Output is correct
15 Correct 67 ms 16116 KB Output is correct
16 Correct 65 ms 13816 KB Output is correct
17 Correct 75 ms 14188 KB Output is correct
18 Correct 69 ms 13388 KB Output is correct
19 Correct 62 ms 13216 KB Output is correct
20 Correct 95 ms 16128 KB Output is correct
21 Correct 87 ms 15224 KB Output is correct
22 Correct 77 ms 15164 KB Output is correct
23 Correct 85 ms 15308 KB Output is correct
24 Correct 102 ms 18592 KB Output is correct
25 Correct 100 ms 20000 KB Output is correct
26 Correct 101 ms 19244 KB Output is correct
27 Correct 96 ms 18128 KB Output is correct
28 Correct 97 ms 16408 KB Output is correct
29 Correct 90 ms 16576 KB Output is correct
30 Correct 95 ms 15480 KB Output is correct
31 Correct 83 ms 15456 KB Output is correct
32 Correct 100 ms 16068 KB Output is correct
33 Correct 94 ms 15528 KB Output is correct
34 Correct 85 ms 15436 KB Output is correct
35 Correct 102 ms 15264 KB Output is correct