답안 #61485

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
61485 2018-07-26T05:16:40 Z Benq Min-max tree (BOI18_minmaxtree) C++11
100 / 100
637 ms 39652 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 70010;

vi adj[MX];
set<array<int,3>> ADJ[MX];
priority_queue<int> mx[MX], mn[MX];
int N;
bool done[MX];
map<int,int> m;
vi rm;

void merge(priority_queue<int>& a, priority_queue<int>& b) {
    if (sz(a) < sz(b)) swap(a,b);
    while (sz(b)) a.push(b.top()), b.pop();
}

void del(priority_queue<int>& a) {
    while (sz(a) > 1) {
        int x = a.top(); a.pop();
        if (a.top() == x) a.pop();
        else {
            a.push(x);
            break;
        }
    }
}
void merge(int a, int b) {
    // cout << "TT " << sz(mn[a]) << " " << sz(mn[b]) << "\n";
    merge(mn[a],mn[b]);
    //cout << "TT " << sz(mn[a]) << " " << sz(mn[b]) << "\n";
    del(mn[a]);
    merge(mx[a],mx[b]);
    del(mx[a]);
}

void dfs(int x, int p) {
    for (int i: adj[x]) if (i != p) {
        dfs(i,x);
        // cout << "OH " << i << " " << x << "\n";
        merge(x,i);
    }
    
    int lo = (sz(mn[x]) ? mn[x].top() : -MOD);
    int hi = (sz(mx[x]) ? -mx[x].top() : MOD);
    
    if (p) {
        // cout << "ZZ " << x << " " << p << "\n";
        ADJ[m[lo]].insert({m[hi],x,p});
        ADJ[m[hi]].insert({m[lo],x,p});
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N;
    F0R(i,N-1) {
        // int A = rand() % (i+1)+1, B = i+2;
        int A,B; cin >> A >> B;
        adj[A].pb(B), adj[B].pb(A);
    }
    int K; cin >> K;
    m[MOD] = 0, m[-MOD] = 1;
    rm.pb(MOD), rm.pb(-MOD);
    F0R(i,K) {
        char c; cin >> c;
        int x,y,z; cin >> x >> y >> z;
        if (c == 'M') mx[x].push(-z), mx[y].push(-z);
        else mn[x].push(z), mn[y].push(z);
        m[z] = i+2, rm.pb(z);
    }
    dfs(1,0);
    set<pi> S;
    FOR(i,2,K+2) S.insert({sz(ADJ[i]),i});
    done[0] = done[1] = 1;
    
    while (sz(S)) {
        auto a = *S.begin(); S.erase(a); done[a.s] = 1;
        // cout << "OOOPS " << sz(ADJ[a.s]) << "\n";
        auto it = *ADJ[a.s].begin(); ADJ[a.s].erase(it);
        if (!done[it[0]]) S.erase({sz(ADJ[it[0]]),it[0]});
        ADJ[it[0]].erase({a.s,it[1],it[2]});
        if (!done[it[0]]) {
            S.insert({sz(ADJ[it[0]]),it[0]});
            assert(sz(ADJ[it[0]]));
        }
        cout << it[1] << " " << it[2] << " " << rm[a.s] << "\n";
    }
    
    F0R(i,K+2) for (auto a: ADJ[i]) if (a[0] >= i)
        cout << a[1] << " " << a[2] << " " << rm[i] << "\n";
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 9848 KB Output is correct
2 Correct 12 ms 9848 KB Output is correct
3 Correct 13 ms 9848 KB Output is correct
4 Correct 14 ms 9848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 623 ms 35880 KB Output is correct
2 Correct 606 ms 35880 KB Output is correct
3 Correct 418 ms 35880 KB Output is correct
4 Correct 474 ms 37196 KB Output is correct
5 Correct 635 ms 37196 KB Output is correct
6 Correct 491 ms 37196 KB Output is correct
7 Correct 537 ms 37196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 359 ms 37196 KB Output is correct
2 Correct 394 ms 37196 KB Output is correct
3 Correct 307 ms 37196 KB Output is correct
4 Correct 304 ms 37196 KB Output is correct
5 Correct 302 ms 37196 KB Output is correct
6 Correct 337 ms 37196 KB Output is correct
7 Correct 386 ms 37196 KB Output is correct
8 Correct 440 ms 37196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 9848 KB Output is correct
2 Correct 12 ms 9848 KB Output is correct
3 Correct 13 ms 9848 KB Output is correct
4 Correct 14 ms 9848 KB Output is correct
5 Correct 623 ms 35880 KB Output is correct
6 Correct 606 ms 35880 KB Output is correct
7 Correct 418 ms 35880 KB Output is correct
8 Correct 474 ms 37196 KB Output is correct
9 Correct 635 ms 37196 KB Output is correct
10 Correct 491 ms 37196 KB Output is correct
11 Correct 537 ms 37196 KB Output is correct
12 Correct 359 ms 37196 KB Output is correct
13 Correct 394 ms 37196 KB Output is correct
14 Correct 307 ms 37196 KB Output is correct
15 Correct 304 ms 37196 KB Output is correct
16 Correct 302 ms 37196 KB Output is correct
17 Correct 337 ms 37196 KB Output is correct
18 Correct 386 ms 37196 KB Output is correct
19 Correct 440 ms 37196 KB Output is correct
20 Correct 597 ms 37196 KB Output is correct
21 Correct 562 ms 37196 KB Output is correct
22 Correct 628 ms 37196 KB Output is correct
23 Correct 498 ms 37196 KB Output is correct
24 Correct 623 ms 37196 KB Output is correct
25 Correct 579 ms 39652 KB Output is correct
26 Correct 574 ms 39652 KB Output is correct
27 Correct 563 ms 39652 KB Output is correct
28 Correct 637 ms 39652 KB Output is correct
29 Correct 530 ms 39652 KB Output is correct
30 Correct 508 ms 39652 KB Output is correct
31 Correct 623 ms 39652 KB Output is correct
32 Correct 619 ms 39652 KB Output is correct
33 Correct 522 ms 39652 KB Output is correct
34 Correct 613 ms 39652 KB Output is correct
35 Correct 569 ms 39652 KB Output is correct