답안 #578424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
578424 2022-06-16T18:55:32 Z AlperenT Min-max tree (BOI18_minmaxtree) C++17
100 / 100
292 ms 45744 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 7e4 + 5;

int n, k, a, b, z, ans[N], cnt[N], color[N];

char c;

vector<int> mn[N], mx[N], cycle, nodes;

map<int, int> mnmp[N], mxmp[N], cmprs, revcmprs;

vector<pair<int, int>> tree[N], graph[N];

pair<int, int> edges[N], edgerange[N], par[N];

struct Path{
	char c;
	int a, b, z;
};

Path paths[N];

void dfs(int v, int p){
	cnt[v] = 1;
	int maxn = -1;

	for(auto e : tree[v]){
		if(e.first != p){
			dfs(e.first, v);

			cnt[v] += cnt[e.first];

			if(maxn == -1 || cnt[e.first] > cnt[maxn]) maxn = e.first;

			a = b = -1;

			if(!mnmp[e.first].empty() && !mxmp[e.first].empty()){
				a = (*prev(mnmp[e.first].end())).first, b = (*mxmp[e.first].begin()).first;

				graph[a].push_back({b, e.second});
				graph[b].push_back({a, e.second});

				edgerange[e.second] = {a, b};
			}
			else if(!mnmp[e.first].empty() && mxmp[e.first].empty()){
				a = b = (*prev(mnmp[e.first].end())).first;

				graph[a].push_back({b, e.second});
				graph[b].push_back({a, e.second});

				edgerange[e.second] = {a, b};
			}
			else if(mnmp[e.first].empty() && !mxmp[e.first].empty()){
				a = b = (*mxmp[e.first].begin()).first;

				graph[a].push_back({b, e.second});
				graph[b].push_back({a, e.second});

				edgerange[e.second] = {a, b};
			}
			else edgerange[e.second] = {1, 1};

			if(a != -1) a = revcmprs[a];
			if(b != -1) b = revcmprs[b];

			// cout << v << " " << e.first << " " << e.second << " " << a << " " << b << "\n";
		}
	}

	if(maxn != -1) swap(mnmp[v], mnmp[maxn]), swap(mxmp[v], mxmp[maxn]);

	for(auto x : mn[v]) if(++mnmp[v][x] == 2) mnmp[v].erase(x);
	for(auto x : mx[v]) if(++mxmp[v][x] == 2) mxmp[v].erase(x);

	for(auto e : tree[v]){
		if(e.first != p && e.first != maxn){
			for(auto x : mnmp[e.first]) if((mnmp[v][x.first] += x.second) == 2) mnmp[v].erase(x.first);
			for(auto x : mxmp[e.first]) if((mxmp[v][x.first] += x.second) == 2) mxmp[v].erase(x.first);
		}
	}
}

void dfs2(int v, int p){
	color[v] = 1;

	nodes.push_back(v);

	for(auto e : graph[v]){
		if(e.second != p){
			if(color[e.first] == 0) par[e.first] = {v, e.second}, dfs2(e.first, e.second);
			else if(color[e.first] == 1 && cycle.empty()){
				int vv = v;

				while(vv != e.first){
					ans[par[vv].second] = vv;

					cycle.push_back(vv);

					vv = par[vv].first;
				}

				ans[e.second] = e.first;

				cycle.push_back(e.first);
			}
		}
	}

	color[v] = 2;
}

void dfs3(int v){
	color[v] = 2;

	for(auto e : graph[v]){
		if(color[e.first] == 0){
			ans[e.second] = e.first;
			dfs3(e.first);
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	
	cin >> n;

	for(int i = 1; i <= n - 1; i++){
		cin >> edges[i].first >> edges[i].second;

		tree[edges[i].first].push_back({edges[i].second, i});
		tree[edges[i].second].push_back({edges[i].first, i});
	}

	cin >> k;

	for(int i = 1; i <= k; i++){
		cin >> c >> a >> b >> z;

		paths[i] = {c, a, b, z};

		cmprs[z] = 1;
	}

	int cur = 0;

	for(auto &p : cmprs){
		p.second = ++cur;

		revcmprs[cur] = p.first;
	}

	for(int i = 1; i <= k; i++){
		paths[i].z = cmprs[paths[i].z];

		if(paths[i].c == 'M') mx[paths[i].a].push_back(paths[i].z), mx[paths[i].b].push_back(paths[i].z);
		else if(paths[i].c == 'm') mn[paths[i].a].push_back(paths[i].z), mn[paths[i].b].push_back(paths[i].z);
	}

	dfs(1, 0);

	for(int i = 1; i <= n; i++){
		if(!color[i] && !graph[i].empty()){
			cycle.clear(), nodes.clear();

			dfs2(i, 0);

			for(auto x : nodes) color[x] = 0;

			for(auto x : cycle) color[x] = 2;

			for(auto x : cycle){
				dfs3(x);
			}
		}
	}

	for(int i = 1; i <= n - 1; i++){
		if(ans[i] == 0) ans[i] = edgerange[i].first;
	}

	for(int i = 1; i <= n - 1; i++) ans[i] = revcmprs[ans[i]];

	for(int i = 1; i <= n - 1; i++) cout << edges[i].first << " " << edges[i].second << " " << ans[i] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13396 KB Output is correct
2 Correct 7 ms 13396 KB Output is correct
3 Correct 8 ms 13396 KB Output is correct
4 Correct 7 ms 13500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 240 ms 38200 KB Output is correct
2 Correct 260 ms 43128 KB Output is correct
3 Correct 218 ms 41036 KB Output is correct
4 Correct 232 ms 41292 KB Output is correct
5 Correct 211 ms 39344 KB Output is correct
6 Correct 233 ms 36660 KB Output is correct
7 Correct 219 ms 34656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 26440 KB Output is correct
2 Correct 129 ms 27872 KB Output is correct
3 Correct 109 ms 29908 KB Output is correct
4 Correct 110 ms 32460 KB Output is correct
5 Correct 137 ms 28184 KB Output is correct
6 Correct 133 ms 29300 KB Output is correct
7 Correct 134 ms 27596 KB Output is correct
8 Correct 146 ms 27424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13396 KB Output is correct
2 Correct 7 ms 13396 KB Output is correct
3 Correct 8 ms 13396 KB Output is correct
4 Correct 7 ms 13500 KB Output is correct
5 Correct 240 ms 38200 KB Output is correct
6 Correct 260 ms 43128 KB Output is correct
7 Correct 218 ms 41036 KB Output is correct
8 Correct 232 ms 41292 KB Output is correct
9 Correct 211 ms 39344 KB Output is correct
10 Correct 233 ms 36660 KB Output is correct
11 Correct 219 ms 34656 KB Output is correct
12 Correct 118 ms 26440 KB Output is correct
13 Correct 129 ms 27872 KB Output is correct
14 Correct 109 ms 29908 KB Output is correct
15 Correct 110 ms 32460 KB Output is correct
16 Correct 137 ms 28184 KB Output is correct
17 Correct 133 ms 29300 KB Output is correct
18 Correct 134 ms 27596 KB Output is correct
19 Correct 146 ms 27424 KB Output is correct
20 Correct 292 ms 45644 KB Output is correct
21 Correct 249 ms 45668 KB Output is correct
22 Correct 283 ms 45744 KB Output is correct
23 Correct 264 ms 44696 KB Output is correct
24 Correct 251 ms 41400 KB Output is correct
25 Correct 219 ms 45128 KB Output is correct
26 Correct 220 ms 43008 KB Output is correct
27 Correct 237 ms 41900 KB Output is correct
28 Correct 283 ms 39372 KB Output is correct
29 Correct 236 ms 40244 KB Output is correct
30 Correct 219 ms 38584 KB Output is correct
31 Correct 217 ms 38736 KB Output is correct
32 Correct 282 ms 40392 KB Output is correct
33 Correct 280 ms 43604 KB Output is correct
34 Correct 280 ms 43088 KB Output is correct
35 Correct 251 ms 39444 KB Output is correct