답안 #343476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343476 2021-01-04T00:55:04 Z ijxjdjd Min-max tree (BOI18_minmaxtree) C++14
100 / 100
194 ms 26556 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();
        if (deg[i] == 1) {
            deq.push(i);
        }
        else {
            unvis.insert(i);
        }
    }
    while (unvis.size() > 0 || deq.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';
                    assert (to[j].size() <= 2);
                    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++) {
      |                     ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 21884 KB Output is correct
2 Correct 145 ms 19836 KB Output is correct
3 Correct 145 ms 19956 KB Output is correct
4 Correct 155 ms 22396 KB Output is correct
5 Correct 144 ms 19828 KB Output is correct
6 Correct 152 ms 20348 KB Output is correct
7 Correct 148 ms 19836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 15468 KB Output is correct
2 Correct 95 ms 15468 KB Output is correct
3 Correct 90 ms 16108 KB Output is correct
4 Correct 96 ms 17004 KB Output is correct
5 Correct 105 ms 16236 KB Output is correct
6 Correct 109 ms 16748 KB Output is correct
7 Correct 106 ms 16212 KB Output is correct
8 Correct 101 ms 15980 KB Output is correct
# 결과 실행 시간 메모리 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 159 ms 21884 KB Output is correct
6 Correct 145 ms 19836 KB Output is correct
7 Correct 145 ms 19956 KB Output is correct
8 Correct 155 ms 22396 KB Output is correct
9 Correct 144 ms 19828 KB Output is correct
10 Correct 152 ms 20348 KB Output is correct
11 Correct 148 ms 19836 KB Output is correct
12 Correct 98 ms 15468 KB Output is correct
13 Correct 95 ms 15468 KB Output is correct
14 Correct 90 ms 16108 KB Output is correct
15 Correct 96 ms 17004 KB Output is correct
16 Correct 105 ms 16236 KB Output is correct
17 Correct 109 ms 16748 KB Output is correct
18 Correct 106 ms 16212 KB Output is correct
19 Correct 101 ms 15980 KB Output is correct
20 Correct 180 ms 20916 KB Output is correct
21 Correct 135 ms 18804 KB Output is correct
22 Correct 139 ms 20852 KB Output is correct
23 Correct 148 ms 20852 KB Output is correct
24 Correct 192 ms 25400 KB Output is correct
25 Correct 194 ms 26556 KB Output is correct
26 Correct 178 ms 25740 KB Output is correct
27 Correct 189 ms 25672 KB Output is correct
28 Correct 192 ms 23544 KB Output is correct
29 Correct 194 ms 23820 KB Output is correct
30 Correct 162 ms 21624 KB Output is correct
31 Correct 161 ms 21752 KB Output is correct
32 Correct 184 ms 23380 KB Output is correct
33 Correct 177 ms 22120 KB Output is correct
34 Correct 173 ms 22132 KB Output is correct
35 Correct 183 ms 21624 KB Output is correct