Submission #1091792

# Submission time Handle Problem Language Result Execution time Memory
1091792 2024-09-22T06:10:09 Z Zero_OP Min-max tree (BOI18_minmaxtree) C++14
100 / 100
146 ms 36712 KB
#include "bits/stdc++.h"

#define all(v) begin(v), end(v)
#define dbg(x) "[" #x " = " << (x) << "]"

using namespace std;

struct edge{
    int v, flow, cap;
    edge(int v, int flow, int cap) : v(v), flow(flow), cap(cap) {}
};

struct Dinic{
    vector<int> level, ptr;
    vector<vector<int>> adj;
    vector<edge> edges;
    queue<int> q;

    Dinic(int n) :
         level(n),
         ptr(n),
         adj(n),
         edges(),
         q()
        {}

    void addEdge(int u, int v, int c){
        adj[u].push_back((int)edges.size());
        edges.push_back(edge(v, 0, c));
        adj[v].push_back((int)edges.size());
        edges.push_back(edge(u, 0, c));
    }

    bool bfs(int s, int t){
        fill(all(level), -1);
        level[s] = 0;

        q.push(s);

        while(!q.empty()){
            int u = q.front(); q.pop();
            for(int id : adj[u]){
                int v = edges[id].v;
                if(level[v] == -1 && edges[id].flow < edges[id].cap){
                    level[v] = level[u] + 1;
                    q.push(v);
                }
            }
        }

        return level[t] != -1;
    }

    int dfs(int u, int inc, int t){
        if(u == t) return inc;
        for(; ptr[u] < (int)adj[u].size(); ++ptr[u]){
            int id = adj[u][ptr[u]];
            int v = edges[id].v;

            if(level[u] + 1 == level[v] && edges[id].flow < edges[id].cap){
                int delta = dfs(v, min(inc, edges[id].cap - edges[id].flow), t);
                if(delta > 0){
                    edges[id].flow += delta;
                    edges[id ^ 1].flow -= delta;
                    return delta;
                }
            }
        }
        return 0;
    }

    int maxflow(int s, int t){
        int sumflow = 0;
        while(bfs(s, t)){
            fill(all(ptr), 0);
            while(int pushed = dfs(s, 1e9, t) > 0){
                sumflow += pushed;
            }
        }
        return sumflow;
    }
};

const int MAX = 7e4 + 5;
const int inf = 1e9 + 7;

int n, depth[MAX], queries[MAX], par[MAX], head[MAX], minEdge[MAX], maxEdge[MAX];
int id_minEdge_in_network[MAX], id_maxEdge_in_network[MAX], timerHLD;
vector<int> adj[MAX];

/*

Note :
minEdge[i] -> id of minimum edge (i, par[i])
maxEdge[i] -> id of maximum edge (i, par[i])

*/

int lab[MAX];

int find_root(int u){
    return lab[u] == u ? u : (lab[u] = find_root(lab[u]));
}

bool unite(int u, int v){
    u = find_root(u);
    v = find_root(v);
    if(u == v) return false;
//    if(lab[u] > lab[v]) swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
    return true;
}

void assign_min_path(int u, int v, int id){
    while(true){
        u = find_root(u);
        v = find_root(v);
        if(u == v) return;
        if(depth[u] < depth[v]) swap(u, v);
        minEdge[u] = id;
        lab[u] = par[u];
    }
}

void assign_max_path(int u, int v, int id){
    while(true){
        u = find_root(u);
        v = find_root(v);
        if(u == v) return;
        if(depth[u] < depth[v]) swap(u, v);
        maxEdge[u] = id;
        lab[u] = par[u];
    }
}

void dfs(int u){
    for(int& v : adj[u]){
        adj[v].erase(find(all(adj[v]), u));
        depth[v] = depth[u] + 1;
        par[v] = u;
        dfs(v);
    }
}

void initDSU(){
    for(int i = 0; i < n; ++i) lab[i] = i;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    if(fopen("task.inp", "r")){
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
    }

    cin >> n;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(0);

    int k; cin >> k;

    vector<tuple<int, int, int, int>> minPaints, maxPaints;
    for(int i = 0; i < k; ++i){
        char op; int u, v, w;
        cin >> op >> u >> v >> w;
        --u, --v;

        if(op == 'm') minPaints.push_back({w, u, v, i});
        else maxPaints.push_back({w, u, v, i});

        queries[i] = w;
    }

    sort(all(minPaints), greater<tuple<int, int, int, int>>());
    sort(all(maxPaints));

    memset(minEdge, -1, sizeof(minEdge));
    memset(maxEdge, -1, sizeof(maxEdge));

    initDSU();
    for(auto [w, u, v, i] : minPaints){
        assign_min_path(u, v, i);
    }

    initDSU();
    for(auto [w, u, v, i] : maxPaints){
        assign_max_path(u, v, i);
    }

    int s = 0, t = n + k + 1;
    Dinic Network(n + k + 2);

    for(int i = 1; i < n; ++i){
        Network.addEdge(s, i, 1);
    }

    for(int i = 0; i < k; ++i){
        Network.addEdge(n + i + 1, t, 1);
    }

    for(int i = 1; i < n; ++i){
        if(minEdge[i] != -1){
            id_minEdge_in_network[i] = (int)Network.edges.size();
            Network.addEdge(i, n + minEdge[i] + 1, 1);
        }

        if(maxEdge[i] != -1){
            id_maxEdge_in_network[i] = (int)Network.edges.size();
            Network.addEdge(i, n + maxEdge[i] + 1, 1);
        }

//        cout << minEdge[i] << ' ' << maxEdge[i] << ' ' << dbg(i + 1) << '\n';
    }

    int ret = Network.maxflow(s, t);

    for(int i = 1; i < n; ++i){
        int res = -1;
        cout << par[i] + 1 << ' ' << i + 1 << ' ';
        if(minEdge[i] == -1 && maxEdge[i] == -1){
            cout << inf << '\n';
        } else{
            if(minEdge[i] != -1 && (Network.edges[id_minEdge_in_network[i]].flow > 0 || maxEdge[i] == -1)) cout << queries[minEdge[i]] << "\n";
            else cout << queries[maxEdge[i]] << "\n";
        }
    }

    return 0;
}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:191:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  191 |     for(auto [w, u, v, i] : minPaints){
      |              ^
minmaxtree.cpp:196:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  196 |     for(auto [w, u, v, i] : maxPaints){
      |              ^
minmaxtree.cpp:228:13: warning: unused variable 'res' [-Wunused-variable]
  228 |         int res = -1;
      |             ^~~
minmaxtree.cpp:225:9: warning: unused variable 'ret' [-Wunused-variable]
  225 |     int ret = Network.maxflow(s, t);
      |         ^~~
minmaxtree.cpp:155:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
minmaxtree.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |         freopen("task.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 26284 KB Output is correct
2 Correct 72 ms 25276 KB Output is correct
3 Correct 71 ms 25784 KB Output is correct
4 Correct 75 ms 29436 KB Output is correct
5 Correct 73 ms 25788 KB Output is correct
6 Correct 80 ms 26032 KB Output is correct
7 Correct 75 ms 25144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 18924 KB Output is correct
2 Correct 62 ms 20352 KB Output is correct
3 Correct 57 ms 21672 KB Output is correct
4 Correct 57 ms 23048 KB Output is correct
5 Correct 75 ms 21092 KB Output is correct
6 Correct 68 ms 21460 KB Output is correct
7 Correct 70 ms 20604 KB Output is correct
8 Correct 68 ms 20448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 77 ms 26284 KB Output is correct
6 Correct 72 ms 25276 KB Output is correct
7 Correct 71 ms 25784 KB Output is correct
8 Correct 75 ms 29436 KB Output is correct
9 Correct 73 ms 25788 KB Output is correct
10 Correct 80 ms 26032 KB Output is correct
11 Correct 75 ms 25144 KB Output is correct
12 Correct 64 ms 18924 KB Output is correct
13 Correct 62 ms 20352 KB Output is correct
14 Correct 57 ms 21672 KB Output is correct
15 Correct 57 ms 23048 KB Output is correct
16 Correct 75 ms 21092 KB Output is correct
17 Correct 68 ms 21460 KB Output is correct
18 Correct 70 ms 20604 KB Output is correct
19 Correct 68 ms 20448 KB Output is correct
20 Correct 134 ms 26432 KB Output is correct
21 Correct 98 ms 24020 KB Output is correct
22 Correct 90 ms 24244 KB Output is correct
23 Correct 98 ms 24228 KB Output is correct
24 Correct 140 ms 34908 KB Output is correct
25 Correct 134 ms 36712 KB Output is correct
26 Correct 123 ms 36016 KB Output is correct
27 Correct 124 ms 34556 KB Output is correct
28 Correct 135 ms 27564 KB Output is correct
29 Correct 124 ms 27564 KB Output is correct
30 Correct 123 ms 25816 KB Output is correct
31 Correct 112 ms 25884 KB Output is correct
32 Correct 146 ms 27160 KB Output is correct
33 Correct 118 ms 25888 KB Output is correct
34 Correct 115 ms 26032 KB Output is correct
35 Correct 125 ms 25408 KB Output is correct