Submission #1091791

# Submission time Handle Problem Language Result Execution time Memory
1091791 2024-09-22T06:09:35 Z Zero_OP Min-max tree (BOI18_minmaxtree) C++14
0 / 100
80 ms 28628 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 Incorrect 2 ms 2652 KB Expected integer, but "19!" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 80 ms 28628 KB Expected integer, but "571006141?" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 61 ms 20176 KB Expected integer, but "104540451?" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2652 KB Expected integer, but "19!" found
2 Halted 0 ms 0 KB -