답안 #61508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
61508 2018-07-26T06:11:28 Z 김세빈(#1779) Min-max tree (BOI18_minmaxtree) C++11
29 / 100
225 ms 15052 KB
#include <bits/stdc++.h>

using namespace std;

struct chain
{
	int s, e, v;
	chain() {}
	chain(int s, int e, int v) : s(s), e(e), v(v) {}
};

vector <chain> X, Y;
vector <int> V[70707], T;
int P[70707], D[70707];
int K1[70707], K2[70707], C1[70707], C2[70707];
vector <int> G[70707];
int M[70707], chk[70707];
int n, k, cnt;

void dfs(int p, int r)
{
	P[p] = K1[p] = K2[p] = r;
	D[p] = D[r] + 1;
	for(int t: V[p]){
		if(t != r) dfs(t, p);
	}
}

bool find_match(int p, int c)
{
	chk[p] = c;
	for(int t: G[p]){
		if(!M[t] || (chk[M[t]] != c && find_match(M[t], c))){
			M[t] = p;
			return 1;
		}
	}
	
	return 0;
}

int main()
{
	int i, a, b, c;
	char str[3];
	
	scanf("%d", &n);
	
	for(i=1; i<n; i++){
		scanf("%d%d", &a, &b);
		V[a].push_back(b);
		V[b].push_back(a);
	}
	
	for(i=1; i<=n; i++){
		C1[i] = C2[i] = -1;
	}
	
	dfs(1, 0);
	
	scanf("%d", &k);
	
	for(i=1; i<=k; i++){
		scanf("%s%d%d%d", str, &a, &b, &c);
		if(*str == 'M'){
			X.push_back(chain(a, b, c));
		}
		else{
			Y.push_back(chain(a, b, c));
		}
	}
	
	sort(X.begin(), X.end(), [=](chain ca, chain cb){
		return ca.v < cb.v;
	});
	
	for(i=0; i<X.size(); i++){
		a = X[i].s, b = X[i].e, c = X[i].v;
		T.clear();
		
		for(; a!=b; ){
			if(D[a] < D[b]) swap(a, b);
			if(C1[a] == -1){
				C1[a] = X[i].v;
				G[i].push_back(a);
			}
			T.push_back(a); a = K1[a];
		}
		
		for(int t: T) K1[t] = a;
	}
	
	sort(Y.begin(), Y.end(), [=](chain ca, chain cb){
		return ca.v > cb.v;
	});
	
	for(i=0; i<Y.size(); i++){
		a = Y[i].s, b = Y[i].e, c = Y[i].v;
		T.clear();
		
		for(; a!=b; ){
			if(D[a] < D[b]) swap(a, b);
			if(C2[a] == -1){
				C2[a] = Y[i].v;
				G[X.size() + i].push_back(a);
			}
			T.push_back(a); a = K2[a];
		}
		
		for(int t: T) K2[t] = a;
	}
	
	for(i=0; i<k; i++){
		find_match(i, ++cnt);
	}
	
	for(i=2; i<=n; i++){
		if(C2[i] != -1 && M[i] >= X.size()) printf("%d %d %d\n", i, P[i], C2[i]);
		else if(C1[i] != -1) printf("%d %d %d\n", i, P[i], C1[i]);
		else printf("%d %d %d\n", i, P[i], 12345);
	}
	
	return 0;
}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:77:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<X.size(); i++){
           ~^~~~~~~~~
minmaxtree.cpp:97:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<Y.size(); i++){
           ~^~~~~~~~~
minmaxtree.cpp:118:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(C2[i] != -1 && M[i] >= X.size()) printf("%d %d %d\n", i, P[i], C2[i]);
                     ~~~~~^~~~~~~~~~~
minmaxtree.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
minmaxtree.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
minmaxtree.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &k);
  ~~~~~^~~~~~~~~~
minmaxtree.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s%d%d%d", str, &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3704 KB Output is correct
2 Correct 7 ms 3768 KB Output is correct
3 Correct 6 ms 3780 KB Output is correct
4 Correct 5 ms 3828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 14616 KB Output is correct
2 Correct 126 ms 14616 KB Output is correct
3 Correct 144 ms 14616 KB Output is correct
4 Correct 225 ms 15052 KB Output is correct
5 Correct 143 ms 15052 KB Output is correct
6 Correct 195 ms 15052 KB Output is correct
7 Correct 166 ms 15052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 122 ms 15052 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3704 KB Output is correct
2 Correct 7 ms 3768 KB Output is correct
3 Correct 6 ms 3780 KB Output is correct
4 Correct 5 ms 3828 KB Output is correct
5 Correct 150 ms 14616 KB Output is correct
6 Correct 126 ms 14616 KB Output is correct
7 Correct 144 ms 14616 KB Output is correct
8 Correct 225 ms 15052 KB Output is correct
9 Correct 143 ms 15052 KB Output is correct
10 Correct 195 ms 15052 KB Output is correct
11 Correct 166 ms 15052 KB Output is correct
12 Incorrect 122 ms 15052 KB Output isn't correct
13 Halted 0 ms 0 KB -