Submission #343466

# Submission time Handle Problem Language Result Execution time Memory
343466 2021-01-04T00:44:30 Z ijxjdjd Min-max tree (BOI18_minmaxtree) C++14
58 / 100
180 ms 22396 KB
#include <bits/stdc++.h>

#define FR(i, N) for (int i = 0; i < int(N); i++)
using namespace std;


const int MAXN = (int)(7e4) + 5;
vector<int> adj[MAXN];
int par[MAXN];
int cpar[MAXN][2];
int tin[MAXN];
int tout[MAXN];
vector<int> color[MAXN];
vector<int> to[MAXN];
bool visited[MAXN];
bool seen[MAXN][2];
bool seenMax[MAXN];
int deg[MAXN];
int store[MAXN][3];
unordered_map<int, int> condense;
int rev[MAXN];
int timer = 0;
void root(int n, int p) {
    par[n] = p;
    cpar[n][0] = p;
    cpar[n][1] = p;
    tin[n] = timer++;
    for (int e : adj[n]) {
        if (e != p) {
            root(e, n);
        }
    }
    tout[n] = timer++;
}
int next(int n, int k) {
    if (!seen[n][k]) {
        return n;
    }
    return (cpar[n][k] = next(cpar[n][k], k));
}
bool is_ancestor(int a, int b) {
    return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
void apply(int a, int b, int z, int k) {
    rev[condense.size()] = z;
    condense[z] = condense.size();
    z = condense[z];
    if (is_ancestor(a, b) || is_ancestor(b, a)) {
        if (is_ancestor(a, b)) {
            swap(a, b);
        }
        a = next(a, k);
        while (!is_ancestor(a, b)) {
            color[z].push_back(a);
            to[a].push_back(z);
            seen[a][k] = true;
            a = next(a, k);
        }
        return;
    }
    int u = a;
    u = next(u, k);
    while (!is_ancestor(u, b)) {
        color[z].push_back(u);
        to[u].push_back(z);
        seen[u][k] = true;
        u = next(u, k);
    }
    b = next(b, k);
    while (!is_ancestor(b, a)) {
        color[z].push_back(b);
        to[b].push_back(z);
        seen[b][k] = true;
        b = next(b, k);
    }
}
int main()
{
//    freopen("input.in", "r", stdin);
    cin.tie(0);
    cin.sync_with_stdio(0);
    vector<int> mins;
    vector<int> maxs;
    int N;
    cin >> N;
    FR(i, N-1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    int K;
    cin >> K;
    FR(i, K) {
        char c;
        int x, y, z;
        cin >> c;
        cin >> x >> y >> z;
        x--, y--;
        store[i][0] = x;
        store[i][1] = y;
        store[i][2] = z;
        if (x != y) {
            if (c == 'm') {
                mins.push_back(i);
            }
            else {
                maxs.push_back(i);
            }
        }
    }
    sort(mins.begin(), mins.end(), [&] (const int& lhs, const int& rhs) {
         return store[lhs][2] > store[rhs][2];
            });
    sort(maxs.begin(), maxs.end(), [&] (const int& lhs, const int& rhs) {
     return store[lhs][2] < store[rhs][2];
        });
    root(0, 0);
    for (int i : mins) {
        apply(store[i][0], store[i][1], store[i][2], 0);
    }
    for (int i : maxs) {
        apply(store[i][0], store[i][1], store[i][2], 1);
    }
    queue<int> deq;
    unordered_set<int> unvis;
    for (int i = 0; i < condense.size(); i++) {
        deg[i] = color[i].size();
        assert (deg[i] > 0);
        if (deg[i] == 1) {
            deq.push(i);
        }
        else {
            unvis.insert(i);
        }
    }
    while (unvis.size() > 0) {
        while (deq.size() > 0) {
            int next = deq.front(); deq.pop();
            for (int j : color[next]) {
                if (!visited[j]) {
                    visited[j] = true;
                    cout << j+1 << " " << par[j]+1 << " " << rev[next] << '\n';
                    for (int e : to[j]) {
                        deg[e]--;
                        if (deg[e] == 1 && unvis.count(e)) {
                            deq.push(e);
                            unvis.erase(e);
                        }
                    }
                    break;
                }
            }
        }
        if (unvis.size() > 0) {
            int j = *unvis.begin();
            unvis.erase(j);
            deq.push(j);
        }
    }
    for (int i = 1; i < N; i++) {
        if (!visited[i]) {
            if (to[i].size() > 0) {
                cout << par[i]+1 << " " << i+1 << " " << rev[to[i][0]] << '\n';
            }
            else {
                cout << par[i]+1 << " " << i+1 << " " << 1 << '\n';
            }
        }
    }
    return 0;
}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::unordered_map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |     for (int i = 0; i < condense.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5356 KB Output is correct
2 Correct 4 ms 5356 KB Output is correct
3 Correct 4 ms 5356 KB Output is correct
4 Correct 4 ms 5356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 21884 KB Output is correct
2 Correct 148 ms 19964 KB Output is correct
3 Correct 140 ms 19972 KB Output is correct
4 Correct 152 ms 22396 KB Output is correct
5 Correct 143 ms 19828 KB Output is correct
6 Correct 177 ms 20348 KB Output is correct
7 Correct 159 ms 19836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 15340 KB Output is correct
2 Correct 94 ms 15468 KB Output is correct
3 Correct 91 ms 16108 KB Output is correct
4 Correct 100 ms 17004 KB Output is correct
5 Correct 109 ms 16152 KB Output is correct
6 Correct 110 ms 16748 KB Output is correct
7 Correct 113 ms 16108 KB Output is correct
8 Correct 102 ms 15852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5356 KB Output is correct
2 Correct 4 ms 5356 KB Output is correct
3 Correct 4 ms 5356 KB Output is correct
4 Correct 4 ms 5356 KB Output is correct
5 Correct 157 ms 21884 KB Output is correct
6 Correct 148 ms 19964 KB Output is correct
7 Correct 140 ms 19972 KB Output is correct
8 Correct 152 ms 22396 KB Output is correct
9 Correct 143 ms 19828 KB Output is correct
10 Correct 177 ms 20348 KB Output is correct
11 Correct 159 ms 19836 KB Output is correct
12 Correct 94 ms 15340 KB Output is correct
13 Correct 94 ms 15468 KB Output is correct
14 Correct 91 ms 16108 KB Output is correct
15 Correct 100 ms 17004 KB Output is correct
16 Correct 109 ms 16152 KB Output is correct
17 Correct 110 ms 16748 KB Output is correct
18 Correct 113 ms 16108 KB Output is correct
19 Correct 102 ms 15852 KB Output is correct
20 Correct 180 ms 20916 KB Output is correct
21 Incorrect 151 ms 18804 KB Output isn't correct
22 Halted 0 ms 0 KB -