Submission #60028

# Submission time Handle Problem Language Result Execution time Memory
60028 2018-07-23T13:18:16 Z egorlifar Min-max tree (BOI18_minmaxtree) C++17
100 / 100
237 ms 17752 KB
/*
ЗАПУСКАЕМ 
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░ 
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
    
     
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } 
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define next next228
#define rank rank228
#define prev prev228
#define y1 y1228                                                         
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
const string FILENAME = "input";
const int MAXN = 100228;


int n;
vector <int> g[MAXN];
int pr[MAXN], deep[MAXN];


void dfs(int u) {
	for (auto h: g[u]) {
		if (h != pr[u]) {
			deep[h] = deep[u] + 1;
			pr[h] = u;
			dfs(h);
		}
	}
}



int parent[MAXN], mindeep[MAXN];



void init() {
	for (int i = 1; i <= n; i++) {
		parent[i] = i;
		mindeep[i] = i;
	}
}


int findset(int v) {
	if (v == parent[v]) {
		return v;
	}
	return parent[v] = findset(parent[v]);
}


void setunion(int a, int b) {
	a = findset(a);
	b = findset(b);
	if (a == b) {
		return;
	}
	if (rand() & 1) {
		parent[a] = b;
	} else {
		parent[b] = a;
		mindeep[a] = mindeep[b];
	}
}




int qval[MAXN];


struct Query {
    int a, b, index;
    Query(int _a = 0, int _b = 0, int _index = 0):
        a(_a), b(_b), index(_index) {}
    friend bool operator<(const Query &arg0, const Query &arg1) {
        return qval[arg0.index] < qval[arg1.index];
    }
};


vector<Query> minq, maxq;
int lowerBound[MAXN], upperBound[MAXN];


void mark(int bound[], int a, int b, int index) {
    a = mindeep[findset(a)], b = mindeep[findset(b)];
    while (a != b) {
        if (deep[a] > deep[b]) {
            swap(a, b);
        }
        bound[b] = index;
        setunion(b, pr[b]);
        b = mindeep[findset(b)];
    }
}
 

vector<int> v[MAXN];
int l[MAXN], r[MAXN];
bool used[MAXN];

 
bool dfs1(int u) {
    if (used[u]) {
        return false;
    }
    used[u] = true;
    for (auto h: v[u]) {
    	if (!r[h] || dfs1(r[h])) {
    		l[u] = h;
    		r[h] = u;
    		return true;
    	}
    }
    return false;
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
//	read(FILENAME);
	cin >> n;
    for (int i = 0; i < n - 1; i++) {
    	int a, b;
    	cin >> a >> b;
    	g[a].pb(b);
    	g[b].pb(a);
    }
    dfs(1);
    int m;
    cin >> m;
    for (int i = 1; i <= m; i++) {
        char type;
        cin >> type;
        int a, b, c;
        cin >> a >> b >> c;
        qval[i] = c;
        if (type == 'm') {
            minq.emplace_back(a, b, i);
        } else {
            maxq.emplace_back(a, b, i);
        }
    }
    sort(all(maxq));
    sort(all(minq));
    reverse(all(minq));
    init();	
    for (auto it: maxq) {
        mark(upperBound, it.a, it.b, it.index);
    }
    init();
    for (auto it: minq) {
        mark(lowerBound, it.a, it.b, it.index);
    }
    for (int i = 2; i <= n; i++) {
        if (lowerBound[i]) {
            v[i].pb(lowerBound[i]);
        }
        if (upperBound[i]) {
            v[i].pb(upperBound[i]);
        }
    }
 	bool ok = true;
    while (ok) {
        ok = false;
        for (int i = 2; i <= n; i++) {
        	used[i] = false;
        }
        for (int i = 2; i <= n; i++) {
        	if (!l[i] && dfs1(i)) {
        		ok = true;
        	}
        }
    }
    for (int i = 2; i <= n; i++) {
        int val = qval[l[i]];
        if (!l[i]) {
            if (lowerBound[i]) {
                val = qval[lowerBound[i]];
            } else {
                val = 1;
            }
        }
        cout << i << ' ' << pr[i] << ' ' << val << '\n';
    }
	return 0; 		
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 6 ms 5220 KB Output is correct
3 Correct 7 ms 5228 KB Output is correct
4 Correct 9 ms 5252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 16012 KB Output is correct
2 Correct 142 ms 16012 KB Output is correct
3 Correct 199 ms 16012 KB Output is correct
4 Correct 169 ms 16692 KB Output is correct
5 Correct 140 ms 16692 KB Output is correct
6 Correct 139 ms 16692 KB Output is correct
7 Correct 139 ms 16692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 16692 KB Output is correct
2 Correct 166 ms 16692 KB Output is correct
3 Correct 112 ms 16692 KB Output is correct
4 Correct 145 ms 16692 KB Output is correct
5 Correct 133 ms 16692 KB Output is correct
6 Correct 143 ms 16692 KB Output is correct
7 Correct 125 ms 16692 KB Output is correct
8 Correct 172 ms 16692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 6 ms 5220 KB Output is correct
3 Correct 7 ms 5228 KB Output is correct
4 Correct 9 ms 5252 KB Output is correct
5 Correct 193 ms 16012 KB Output is correct
6 Correct 142 ms 16012 KB Output is correct
7 Correct 199 ms 16012 KB Output is correct
8 Correct 169 ms 16692 KB Output is correct
9 Correct 140 ms 16692 KB Output is correct
10 Correct 139 ms 16692 KB Output is correct
11 Correct 139 ms 16692 KB Output is correct
12 Correct 110 ms 16692 KB Output is correct
13 Correct 166 ms 16692 KB Output is correct
14 Correct 112 ms 16692 KB Output is correct
15 Correct 145 ms 16692 KB Output is correct
16 Correct 133 ms 16692 KB Output is correct
17 Correct 143 ms 16692 KB Output is correct
18 Correct 125 ms 16692 KB Output is correct
19 Correct 172 ms 16692 KB Output is correct
20 Correct 146 ms 16692 KB Output is correct
21 Correct 138 ms 16692 KB Output is correct
22 Correct 195 ms 16692 KB Output is correct
23 Correct 160 ms 16692 KB Output is correct
24 Correct 160 ms 16692 KB Output is correct
25 Correct 192 ms 17752 KB Output is correct
26 Correct 185 ms 17752 KB Output is correct
27 Correct 237 ms 17752 KB Output is correct
28 Correct 161 ms 17752 KB Output is correct
29 Correct 150 ms 17752 KB Output is correct
30 Correct 151 ms 17752 KB Output is correct
31 Correct 164 ms 17752 KB Output is correct
32 Correct 171 ms 17752 KB Output is correct
33 Correct 140 ms 17752 KB Output is correct
34 Correct 156 ms 17752 KB Output is correct
35 Correct 205 ms 17752 KB Output is correct