답안 #254179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
254179 2020-07-29T13:07:30 Z shayan_p Min-max tree (BOI18_minmaxtree) C++14
100 / 100
114 ms 15792 KB
// And you curse yourself for things you never done

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn = 1e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10;

vector<int> v[maxn];

int dsu[maxn], up[maxn];
int h[maxn], pr[maxn];

void restart_dsu(){
    memset(dsu, -1, sizeof dsu);
    iota(up, up + maxn, 0);
}
int fnd(int u){
    return dsu[u] < 0 ? u : dsu[u] = fnd(dsu[u]);
}
void mrg(int a, int b){
    if( (a = fnd(a)) == (b = fnd(b)) )
	return;
    if(dsu[a] > dsu[b])
	swap(a, b);
    dsu[a]+= dsu[b];
    dsu[b] = a;
    if(h[ up[b] ] < h[ up[a] ])
	up[a] = up[b];
}

void prep(int u, int par = 0){
    h[u] = h[par] + 1;
    pr[u] = par;
    for(int y : v[u])
	if(y != par)
	    prep(y, u);
}

int L[maxn], R[maxn], idL[maxn], idR[maxn];

void MN(int id, int x, int id2){
    if(R[id] > x)
	R[id] = x, idR[id] = id2;
}
void MX(int id, int x, int id2){
    if(L[id] < x)
	L[id] = x, idL[id] = id2;
}

int tp[maxn], to[2 * maxn], nxt[2 * maxn], good[2 * maxn], C;

void add(int a, int b, int c){
    nxt[C] = tp[a], tp[a] = C, to[C] = b, good[C] = c, C++;
    nxt[C] = tp[b], tp[b] = C, to[C] = a, good[C] = c ^ 1, C++;
}

int mark[maxn], choose[maxn];

int cyc(int u, int par = -1){
    mark[u] = 1;
    for(int id = tp[u]; id != -1; id = nxt[id]){
	if(id != par){
	    if(mark[ to[id] ] == 1)
		return u;
	    int num = cyc(to[id], id ^ 1);
	    if(num != -1)
		return num;
	}
    }
    return -1;
}
void go(int u, int par = -1){
    mark[u] = 2;
    for(int id = tp[u]; id != -1; id = nxt[id]){
	if(id != par){
	    if(mark[ to[id] ] == 2)
		choose[good[id] >> 1] = good[id] & 1;
	    else if(mark[ to[id] ] != 3)
		choose[good[id] >> 1] = good[id] & 1, go(to[id], id ^ 1);
	}
    }
    mark[u] = 3;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();

    memset(tp, -1, sizeof tp);
    memset(idL, -1, sizeof idL);
    memset(idR, -1, sizeof idR);
    
    int n;
    cin >> n;
    for(int i = 0; i < n-1; i++){
	int a, b;
	cin >> a >> b;
	v[a].PB(b);
	v[b].PB(a);	    
    }
    prep(1);
    for(int i = 2; i <= n; i++){
	L[i] = -inf, R[i] = inf;
    }
    int k;
    cin >> k;
    vector< pair<int, pii> > MXS, MNS;    
    for(int i = 0; i < k; i++){
	char c;
	cin >> c;
	int a, b, x;
	cin >> a >> b >> x;
	if(c == 'M')
	    MNS.PB({x, {a, b}});
	else
	    MXS.PB({x, {a, b}});
    }
    sort(MNS.begin(), MNS.end());
    sort(MXS.begin(), MXS.end());
    reverse(MXS.begin(), MXS.end());

    int counter = 0;
    restart_dsu();
    for(auto x : MNS){
	int a = x.S.F, b = x.S.S, X = x.F;
	while(fnd(a) != fnd(b)){
	    a = up[fnd(a)], b = up[fnd(b)];
	    if(h[a] < h[b])
		swap(a, b);
	    MN(a, X, counter);
	    mrg(a, pr[a]);
	}
	counter++;
    }
    restart_dsu();
    for(auto x : MXS){
	int a = x.S.F, b = x.S.S, X = x.F;
	while(fnd(a) != fnd(b)){
	    a = up[fnd(a)], b = up[fnd(b)];
	    if(h[a] < h[b])
		swap(a, b);
	    MX(a, X, counter);
	    mrg(a, pr[a]);
	}
	counter++;
    }
    
    for(int i = 2; i <= n; i++){
	assert(L[i] <= R[i]);
	if(idL[i] == -1)
	    L[i] = R[i];
	if(idR[i] == -1)
	    R[i] = L[i];
	if(idL[i] == -1 && idR[i] != -1)
	    add(idR[i], idR[i], 2 * i + 1);
	if(idL[i] != -1 && idR[i] == -1)
	    add(idL[i], idL[i], 2 * i);
	if(L[i] == R[i]){
	    if(idL[i] != -1 && idR[i] != -1)
		add(idL[i], idL[i], 2 * i), add(idR[i], idR[i], 2 * i + 1);
	}
	else{
	    assert(idL[i] != -1 && idR[i] != -1);
	    add(idL[i], idR[i], 2 * i + 1);
	}
    }

    for(int i = 0; i < k; i++){
	if(mark[i] == 0){
	    int u = cyc(i);
	    assert(u != -1);
	    go(u);
	}
    }
    for(int i = 2; i <= n; i++){
	cout << i << " " << pr[i] << " " << (choose[i] ? R[i] : L[i]) << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4736 KB Output is correct
2 Correct 3 ms 4736 KB Output is correct
3 Correct 3 ms 4736 KB Output is correct
4 Correct 3 ms 4736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 14444 KB Output is correct
2 Correct 88 ms 12776 KB Output is correct
3 Correct 85 ms 13680 KB Output is correct
4 Correct 97 ms 14956 KB Output is correct
5 Correct 93 ms 13424 KB Output is correct
6 Correct 100 ms 13088 KB Output is correct
7 Correct 95 ms 12524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 11600 KB Output is correct
2 Correct 86 ms 11896 KB Output is correct
3 Correct 80 ms 13272 KB Output is correct
4 Correct 68 ms 14048 KB Output is correct
5 Correct 86 ms 12080 KB Output is correct
6 Correct 74 ms 12400 KB Output is correct
7 Correct 78 ms 11896 KB Output is correct
8 Correct 82 ms 11796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4736 KB Output is correct
2 Correct 3 ms 4736 KB Output is correct
3 Correct 3 ms 4736 KB Output is correct
4 Correct 3 ms 4736 KB Output is correct
5 Correct 97 ms 14444 KB Output is correct
6 Correct 88 ms 12776 KB Output is correct
7 Correct 85 ms 13680 KB Output is correct
8 Correct 97 ms 14956 KB Output is correct
9 Correct 93 ms 13424 KB Output is correct
10 Correct 100 ms 13088 KB Output is correct
11 Correct 95 ms 12524 KB Output is correct
12 Correct 76 ms 11600 KB Output is correct
13 Correct 86 ms 11896 KB Output is correct
14 Correct 80 ms 13272 KB Output is correct
15 Correct 68 ms 14048 KB Output is correct
16 Correct 86 ms 12080 KB Output is correct
17 Correct 74 ms 12400 KB Output is correct
18 Correct 78 ms 11896 KB Output is correct
19 Correct 82 ms 11796 KB Output is correct
20 Correct 113 ms 12888 KB Output is correct
21 Correct 100 ms 12464 KB Output is correct
22 Correct 90 ms 12336 KB Output is correct
23 Correct 88 ms 12396 KB Output is correct
24 Correct 112 ms 14560 KB Output is correct
25 Correct 110 ms 15792 KB Output is correct
26 Correct 114 ms 15788 KB Output is correct
27 Correct 102 ms 14512 KB Output is correct
28 Correct 98 ms 12900 KB Output is correct
29 Correct 95 ms 13108 KB Output is correct
30 Correct 102 ms 12860 KB Output is correct
31 Correct 97 ms 12528 KB Output is correct
32 Correct 103 ms 12616 KB Output is correct
33 Correct 100 ms 12716 KB Output is correct
34 Correct 97 ms 12532 KB Output is correct
35 Correct 98 ms 12592 KB Output is correct