답안 #61484

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
61484 2018-07-26T05:13:03 Z koosaga(#1775) Min-max tree (BOI18_minmaxtree) C++11
100 / 100
650 ms 24984 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 70005;

int n, m;
vector<int> gph[MAXN];
map<int, int> mp;
int dep[MAXN], par[17][MAXN];
int emax[MAXN], emin[MAXN], cost[MAXN];
int s[MAXN], e[MAXN];

void dfs(int x, int p){
	for(auto &i : gph[x]){
		if(i != p){
			dep[i] = dep[x] + 1;
			par[0][i] = x;
			dfs(i, x);
		}
	}
}

int lca(int s, int e){
	if(dep[s] > dep[e]) swap(s, e);
	int dx = dep[e] - dep[s];
	for(int i=0; i<17; i++){
		if((dx >> i) & 1) e = par[i][e];
	}
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

struct edg{
	int s, e, x;
};

int pa[MAXN];

int paint(int s, int l, int v, int *ans){
	if(s != pa[s]) return pa[s] = paint(pa[s], l, v, ans);
	if(dep[s] <= dep[l]) return s;
	else{
		ans[s] = v;
		return pa[s] = paint(par[0][s], l, v, ans);
	}
}

struct bipartite_matching{ // unnecessary, but I'm too lazy
	vector<int> gph[MAXN];
	bitset<MAXN> vis;
	int l[MAXN], r[MAXN];
	int dis[MAXN];
	void init(){
		memset(l, -1, sizeof(l));
		memset(r, -1, sizeof(r));
	}
	void add_edge(int s, int e){
		gph[s].push_back(e);
	}
	bool bfs(int k){
		queue<int> que;
		memset(dis, 0, sizeof(dis));
		for(int i=0; i<k; i++){
			if(l[i] == -1){
				dis[i] = 1;
				que.push(i);
			}
		}
		int ok = 0;
		while(!que.empty()){
			auto x = que.front();
			que.pop();
			for(auto &i : gph[x]){
				if(r[i] == -1) ok = 1;
				else if(!dis[r[i]]){
					dis[r[i]] = dis[x] + 1;
					que.push(r[i]);
				}
			}
		}
		return ok;
	}
	bool dfs(int x){
		if(vis[x]) return 0;
		vis[x] = 1;
		for(auto &i : gph[x]){
			if(r[i] == -1 || dfs(r[i])){
				r[i] = x;
				l[x] = i;
				return 1;
			}
		}
		return 0;
	}
	void match(int k){
		while(bfs(k)){
			vis = 0;
			for(int i=0; i<k; i++){
				if(l[i] == -1){
					dfs(i);
				}
			}
		}
	}
}bpm;

int main(){
	scanf("%d",&n);
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d",&s,&e);
		gph[s].push_back(e);
		gph[e].push_back(s);
	}
	dfs(1, -1);
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	bpm.init();
	memset(emin, -1, sizeof(emin));
	memset(emax, 0x3f, sizeof(emax));
	vector<edg> evmax, evmin;
	scanf("%d",&m);
	for(int i=0; i<m; i++){
		char buf[5];
		int s, e, x;
		scanf("%s %d %d %d",buf,&s,&e,&x);
		cost[i] = x;
		mp[x] = i;
		int l = lca(s, e);
		if(*buf == 'M'){
			evmax.push_back({s, l, x});
			evmax.push_back({e, l, x});
		}
		else{
			evmin.push_back({s, l, x});
			evmin.push_back({e, l, x});
		}
	}
	sort(evmax.begin(), evmax.end(), [&](const edg &a, const edg &b){
		return a.x < b.x;
	});
	sort(evmin.begin(), evmin.end(), [&](const edg &a, const edg &b){
		return a.x > b.x;
	});
	iota(pa, pa + n + 1, 0);
	for(auto &i : evmax){
		paint(i.s, i.e, i.x, emax);
	}
	iota(pa, pa + n + 1, 0);
	for(auto &i : evmin){
		paint(i.s, i.e, i.x, emin);
	}
	for(int i=2; i<=n; i++){
		int l = -1, r = -1;
		if(mp.find(emin[i]) != mp.end()) l = mp[emin[i]];
		if(mp.find(emax[i]) != mp.end()) r = mp[emax[i]];
		if(~l) bpm.add_edge(l, i-2);
		if(~r) bpm.add_edge(r, i-2);
	}
	bpm.match(m);
	// l : left -> right fun
	// r : right -> left fun
	for(int i=2; i<=n; i++){
		printf("%d %d ", par[0][i], i);
		if(bpm.r[i-2] == -1) printf("%d\n", emin[i]);
		else printf("%d\n", cost[bpm.r[i-2]]);
	}
}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
minmaxtree.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&s,&e);
   ~~~~~^~~~~~~~~~~~~~~
minmaxtree.cpp:130:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&m);
  ~~~~~^~~~~~~~~
minmaxtree.cpp:134:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s %d %d %d",buf,&s,&e,&x);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5168 KB Output is correct
4 Correct 7 ms 5296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 636 ms 23552 KB Output is correct
2 Correct 552 ms 23552 KB Output is correct
3 Correct 400 ms 23552 KB Output is correct
4 Correct 545 ms 24232 KB Output is correct
5 Correct 613 ms 24232 KB Output is correct
6 Correct 627 ms 24232 KB Output is correct
7 Correct 487 ms 24232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 272 ms 24232 KB Output is correct
2 Correct 263 ms 24232 KB Output is correct
3 Correct 265 ms 24232 KB Output is correct
4 Correct 247 ms 24232 KB Output is correct
5 Correct 309 ms 24232 KB Output is correct
6 Correct 304 ms 24232 KB Output is correct
7 Correct 302 ms 24232 KB Output is correct
8 Correct 255 ms 24232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5168 KB Output is correct
4 Correct 7 ms 5296 KB Output is correct
5 Correct 636 ms 23552 KB Output is correct
6 Correct 552 ms 23552 KB Output is correct
7 Correct 400 ms 23552 KB Output is correct
8 Correct 545 ms 24232 KB Output is correct
9 Correct 613 ms 24232 KB Output is correct
10 Correct 627 ms 24232 KB Output is correct
11 Correct 487 ms 24232 KB Output is correct
12 Correct 272 ms 24232 KB Output is correct
13 Correct 263 ms 24232 KB Output is correct
14 Correct 265 ms 24232 KB Output is correct
15 Correct 247 ms 24232 KB Output is correct
16 Correct 309 ms 24232 KB Output is correct
17 Correct 304 ms 24232 KB Output is correct
18 Correct 302 ms 24232 KB Output is correct
19 Correct 255 ms 24232 KB Output is correct
20 Correct 552 ms 24232 KB Output is correct
21 Correct 419 ms 24232 KB Output is correct
22 Correct 370 ms 24232 KB Output is correct
23 Correct 460 ms 24232 KB Output is correct
24 Correct 588 ms 24232 KB Output is correct
25 Correct 650 ms 24984 KB Output is correct
26 Correct 595 ms 24984 KB Output is correct
27 Correct 618 ms 24984 KB Output is correct
28 Correct 564 ms 24984 KB Output is correct
29 Correct 647 ms 24984 KB Output is correct
30 Correct 446 ms 24984 KB Output is correct
31 Correct 565 ms 24984 KB Output is correct
32 Correct 509 ms 24984 KB Output is correct
33 Correct 459 ms 24984 KB Output is correct
34 Correct 529 ms 24984 KB Output is correct
35 Correct 472 ms 24984 KB Output is correct