답안 #170456

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
170456 2019-12-25T10:33:52 Z Retro3014 Min-max tree (BOI18_minmaxtree) C++17
100 / 100
271 ms 29020 KB
#include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()
#define sortv(v) sort(all(v))
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define pb push_back
#define FI first
#define SE second
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define test 1
#define TEST if(test)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

const int MOD = 1000000007; // 998244353
const int INF = 2e9;
const ll INFLL = 1e18;
const int MAX_N = 70000;
const int MAX_K = 20;

int N, K;
string str;
vector<int> gp[MAX_N+1];
int p[MAX_N+1], up[MAX_N+1][MAX_K+1], lv[MAX_N+1];
bool vst[MAX_N+1];
struct Query{
	int idx, x, y;
	int d;
	bool operator <(const Query &a)const{
		return d<a.d;
	}
};
vector<Query> qmx, qmn;

int lca(int x, int y){
	for(int i=MAX_K-1; i>=0; i--){
		if(lv[up[x][i]]>=lv[y])	x = up[x][i];
		if(lv[up[y][i]]>=lv[x])	y = up[y][i];
	}
	if(x==y)	return x;
	for(int i=MAX_K-1; i>=0; i--){
		if(up[x][i]!=up[y][i]){
			x = up[x][i];
			y = up[y][i];
		}
	}
	return up[x][0];
}

void dfs(int x){
	up[x][0] = p[x];
	for(int i=1; i<MAX_K; i++){
		up[x][i] = up[up[x][i-1]][i-1];
	}
	vst[x] = true;
	for(int i : gp[x]){
		if(!vst[i]){
			p[i] = x; lv[i] = lv[x]+1;
			dfs(i);
		}
	}
}
int mx[MAX_N+1], mn[MAX_N+1];

int g[MAX_N+1];
void ig(){
	for(int i=1; i<=N; i++)	g[i] = i;
}
int fg(int x){
	return (x==g[x])?x:g[x] = fg(g[x]);
}
void ug(int x, int y){
	x = fg(x); y = fg(y);
	g[y] = x;
}

bool chk2[MAX_N+1];
bool chk[MAX_N+1];
vector<int> cost;
vector<pii> gp2[MAX_N+1];
set<int> st;
struct S{
	int a, b, c;
};
vector<S> ans;
vector<int> vt;
int use[MAX_N+1];
int deg[MAX_N+1];

int main(){
	scanf("%d", &N);
	for(int i=1; i<N; i++){
		int a, b; scanf("%d%d", &a, &b);
		gp[a].pb(b); gp[b].pb(a);
	}
	lv[1] = 1; dfs(1);
	scanf("%d", &K);
	for(int i=0; i<K; i++){
		cin>>str;
		int a, b, c; scanf("%d%d%d", &a, &b, &c);
		if(str[0]=='M'){
			qmx.pb({i, a, b, c});
		}else{
			qmn.pb({i, a, b, -c});
		}
		cost.pb(c);
		st.insert(i);
	}
	sort(qmx.begin(), qmx.end()); sort(qmn.begin(), qmn.end());
	ig();
	for(int i=2; i<=N; i++)	mx[i] = mn[i] = -1;
	for(int i=0; i<qmx.size(); i++){
		Query q = qmx[i];
		int l = lca(q.x, q.y);
		int nxt;
		while(1){
			q.x = fg(q.x);
			if(lv[p[q.x]]>=lv[l]){
				mx[q.x] = q.idx;
			}else{
				break;
			}
			ug(l, q.x);
			q.x = p[q.x];
		}
		while(1){
			q.y = fg(q.y);
			if(lv[p[q.y]]>=lv[l]){
				mx[q.y] = q.idx;
			}else{
				break;
			}
			ug(l, q.y);
			q.y = p[q.y];
		}
	}
	ig();
	for(int i=0; i<qmn.size(); i++){
		Query q = qmn[i];
		int l = lca(q.x, q.y);
		int nxt;
		while(1){
			q.x = fg(q.x);
			if(lv[p[q.x]]>=lv[l]){
				mn[q.x] = q.idx;
			}else{
				break;
			}
			ug(l, q.x);
			q.x = p[q.x];
		}
		while(1){
			q.y = fg(q.y);
			if(lv[p[q.y]]>=lv[l]){
				mn[q.y] = q.idx;
			}else{
				break;
			}
			ug(l, q.y);
			q.y = p[q.y];
		}
	}
	for(int i=2; i<=N; i++){
		//cout<<mx[i]<<" "<<mn[i]<<endl;
		if(mx[i]!=-1){
			if(mn[i]!=-1){
				gp2[mx[i]].pb({mn[i], i});
				gp2[mn[i]].pb({mx[i], i});
				deg[mx[i]]++; deg[mn[i]]++;
			}else{
				if(!chk[mx[i]]){
					chk[mx[i]] = true;
					vt.pb(mx[i]);
					chk2[i] = true;
					ans.pb({i, p[i], cost[mx[i]]});
					use[mx[i]] = i;
				}
			}
		}else{
			if(mn[i]!=-1){
				if(!chk[mn[i]]){
					chk[mn[i]] = true;
					vt.pb({mn[i]});
					chk2[i] = true;
					ans.pb({i, p[i], cost[mn[i]]});
					use[mn[i]] = i;
				}
			}
		}
	}
	for(int i=0; i<K; i++){
		if(!chk[i] && gp2[i].size()==1){
			pii pp = gp2[i][0];
			chk[i] = true;
			ans.pb({pp.second, p[pp.second], cost[i]});
			chk2[pp.second] = true;
			vt.pb(i);
			use[i] = pp.second;
			deg[pp.first]--;
		}
	}
	while(!st.empty()){
		int t, k=-1;
		if(vt.empty()){
			t = (*st.begin());
			for(pii i : gp2[t]){
				if(!chk2[i.second]){
					k = i.second;
					deg[i.first]--;
					break;
				}
			}
			chk[t] = true;
			ans.pb({k, p[k], cost[t]});
			chk2[k] = true;
		}else{
			t = vt.back(); vt.pop_back();
		}
		st.erase(t);
		for(pii pr : gp2[t]){
			if(!chk2[pr.second] && !chk[pr.first]){
				chk[pr.first] = true;
				vt.pb(pr.first);
				ans.pb({pr.second, p[pr.second], cost[pr.first]});
				chk2[pr.second] = true;
			}else if(chk2[pr.second] && !chk[pr.first]){
				if(deg[pr.first]==1){
					int tmp;
					for(pii i : gp2[pr.first]){
						if(!chk2[i.second]){
							deg[i.first]--;
							tmp = i.second; break;
						}
					}
					chk[pr.first] = true;
					ans.pb({tmp, p[tmp], cost[pr.first]});
					chk2[tmp] = true;
					vt.pb(pr.first);
				}
			}
		}
	}
	for(int i=2; i<=N; i++){
		if(!chk2[i]){
			if(mn[i]!=-1){
				ans.pb({i, p[i], cost[mn[i]]});
			}else if(mx[i]!=-1){
				ans.pb({i, p[i], cost[mx[i]]});
			}else{
				ans.pb({i ,p[i], 0});
			}
		}
	}
	for(int i=0; i<ans.size(); i++){
		printf("%d %d %d\n", ans[i].a, ans[i].b, ans[i].c);
	}
	return 0;
}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:119:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<qmx.size(); i++){
               ~^~~~~~~~~~~
minmaxtree.cpp:122:7: warning: unused variable 'nxt' [-Wunused-variable]
   int nxt;
       ^~~
minmaxtree.cpp:145:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<qmn.size(); i++){
               ~^~~~~~~~~~~
minmaxtree.cpp:148:7: warning: unused variable 'nxt' [-Wunused-variable]
   int nxt;
       ^~~
minmaxtree.cpp:261:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<ans.size(); i++){
               ~^~~~~~~~~~~
minmaxtree.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
minmaxtree.cpp:100:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d%d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~
minmaxtree.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &K);
  ~~~~~^~~~~~~~~~
minmaxtree.cpp:107:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b, c; scanf("%d%d%d", &a, &b, &c);
                ~~~~~^~~~~~~~~~~~~~~~~~~~~~
minmaxtree.cpp:243:24: warning: 'tmp' may be used uninitialized in this function [-Wmaybe-uninitialized]
      ans.pb({tmp, p[tmp], cost[pr.first]});
                   ~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3704 KB Output is correct
2 Correct 5 ms 3704 KB Output is correct
3 Correct 5 ms 3704 KB Output is correct
4 Correct 5 ms 3704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 227 ms 24184 KB Output is correct
2 Correct 202 ms 21912 KB Output is correct
3 Correct 197 ms 20840 KB Output is correct
4 Correct 219 ms 24788 KB Output is correct
5 Correct 198 ms 20712 KB Output is correct
6 Correct 215 ms 24292 KB Output is correct
7 Correct 207 ms 23652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 17188 KB Output is correct
2 Correct 115 ms 18852 KB Output is correct
3 Correct 116 ms 20008 KB Output is correct
4 Correct 120 ms 20748 KB Output is correct
5 Correct 131 ms 19368 KB Output is correct
6 Correct 138 ms 19744 KB Output is correct
7 Correct 138 ms 19236 KB Output is correct
8 Correct 131 ms 19108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3704 KB Output is correct
2 Correct 5 ms 3704 KB Output is correct
3 Correct 5 ms 3704 KB Output is correct
4 Correct 5 ms 3704 KB Output is correct
5 Correct 227 ms 24184 KB Output is correct
6 Correct 202 ms 21912 KB Output is correct
7 Correct 197 ms 20840 KB Output is correct
8 Correct 219 ms 24788 KB Output is correct
9 Correct 198 ms 20712 KB Output is correct
10 Correct 215 ms 24292 KB Output is correct
11 Correct 207 ms 23652 KB Output is correct
12 Correct 114 ms 17188 KB Output is correct
13 Correct 115 ms 18852 KB Output is correct
14 Correct 116 ms 20008 KB Output is correct
15 Correct 120 ms 20748 KB Output is correct
16 Correct 131 ms 19368 KB Output is correct
17 Correct 138 ms 19744 KB Output is correct
18 Correct 138 ms 19236 KB Output is correct
19 Correct 131 ms 19108 KB Output is correct
20 Correct 216 ms 24756 KB Output is correct
21 Correct 184 ms 22880 KB Output is correct
22 Correct 183 ms 22880 KB Output is correct
23 Correct 184 ms 23264 KB Output is correct
24 Correct 234 ms 28500 KB Output is correct
25 Correct 243 ms 29020 KB Output is correct
26 Correct 223 ms 28392 KB Output is correct
27 Correct 238 ms 27992 KB Output is correct
28 Correct 271 ms 25584 KB Output is correct
29 Correct 220 ms 25564 KB Output is correct
30 Correct 240 ms 23952 KB Output is correct
31 Correct 219 ms 24236 KB Output is correct
32 Correct 233 ms 25300 KB Output is correct
33 Correct 210 ms 24412 KB Output is correct
34 Correct 213 ms 24416 KB Output is correct
35 Correct 201 ms 23872 KB Output is correct