제출 #60028

#제출 시각아이디문제언어결과실행 시간메모리
60028egorlifarMin-max tree (BOI18_minmaxtree)C++17
100 / 100
237 ms17752 KiB
/* ЗАПУСКАЕМ ░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░ ▄███▀░◐░░░▌░░░░░░░ ░░░░▌░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▌░░░░░▐▄▄░░░░░ ░░░░▌░░░░▄▀▒▒▀▀▀▀▄ ░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄ ░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░▄▄▌▌▄▌▌░░░░░ */ #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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...