Submission #251206

# Submission time Handle Problem Language Result Execution time Memory
251206 2020-07-20T15:19:40 Z Sorting Pipes (CEOI15_pipes) C++14
0 / 100
5000 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

const int k_M = 6e6 + 3;
const int k_N = 1e5 + 3;
const int k_LOG_N = 20;

int n, m;
vector<pair<int, int>> adj[k_N];

vector<pair<int, int>> edges;

int p[k_N], root[k_N], suffix[k_N];
int up[k_LOG_N][k_N], d[k_N];
vector<int> nodes[k_N];

bool not_critical[k_N];

int find_p(int x){
    if(x == p[x]) return x;
    return p[x] = find_p(p[x]);
}

int dfs(int x, int parent = -1){
    int sum = 0;
    for(auto [to, idx]: adj[x]){
        if(to == parent) continue;

        int curr = dfs(to, x);
        if(curr) not_critical[idx] = true;
        sum += curr;
    }

    return sum += suffix[x];
}

void dfs_join(int x, int parent){
    up[0][x] = parent;
    d[x] = d[parent] + 1;

    for(auto [to, idx]: adj[x]){
        if(to == parent) continue;
        dfs_join(to, x);
    }
}

bool unite(int x, int y){
    if(find_p(x) == find_p(y)) return false;
    if(nodes[p[x]].size() < nodes[p[y]].size()) swap(x, y);

    dfs(root[p[y]]);
    dfs_join(y, x);

    for(int i = 1; i < k_LOG_N; ++i)
        for(int node: nodes[p[y]])
            up[i][node] = up[i - 1][up[i - 1][node]];

    for(int node: nodes[p[y]])
        nodes[p[x]].push_back(node);
    nodes[p[y]].clear();
    p[p[y]] = p[x];
    return true;
}

int get_lca(int u, int v){
    if(d[u] < d[v]) swap(u, v);

    int diff = d[u] - d[v];
    if(diff){
        for(int i = k_LOG_N - 1; i >= 0; --i){
            if(diff & (1 << i)){
                u = up[i][u];
            }
        }
    }

    if(u == v) return u;

    for(int i = k_LOG_N - 1; i >= 0; --i){
        if(up[i][u] != up[i][v]){
            u = up[i][u];
            v = up[i][v];
        }
    }

    return up[0][u];
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;

    for(int i = 1; i <= n; ++i){
        p[i] = i;
        nodes[i].push_back(i);
        root[i] = i;
        suffix[i] = 0;
        d[i] = 0;

        //for(int j = 0; j < k_LOG_N; ++j)
        //    up[j][i] = i;
    }

    for(int i = 0; i < m; ++i){
        int u, v;
        cin >> u >> v;

        if(unite(u, v)){
            edges.push_back({u, v});
            adj[u].push_back({v, edges.size() - 1});
            adj[v].push_back({u, edges.size() - 1});
        }
        else{
            int lca = get_lca(u, v);
            suffix[lca] -= 2;
            suffix[u]++;
            suffix[v]++;
        }

        cout << "\n";
        cout << "u v " << u << " " << v << "\n\n";
        for(int i = 1; i <= n; ++i){
            cout << i << ":\n";
            cout << "p[i]: " << p[i] << "\n";
            cout << "root[i]: " << root[i] << "\n";
            cout << "suffix[i]: " << suffix[i] << "\n";
        }
        cout << "\n";
    }

    for(int i = 1; i <= n; ++i)
        if(i == find_p(i))
            dfs(root[i]);

    for(int i = 0; i < edges.size(); ++i)
        if(!not_critical[i])
            cout << edges[i].first << " " << edges[i].second << "\n";
}

Compilation message

pipes.cpp: In function 'int dfs(int, int)':
pipes.cpp:27:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
     for(auto [to, idx]: adj[x]){
              ^
pipes.cpp: In function 'void dfs_join(int, int)':
pipes.cpp:42:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
     for(auto [to, idx]: adj[x]){
              ^
pipes.cpp:42:22: warning: unused variable 'idx' [-Wunused-variable]
     for(auto [to, idx]: adj[x]){
                      ^
pipes.cpp: In function 'int main()':
pipes.cpp:138:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < edges.size(); ++i)
                    ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 5632 KB Expected integer, but "u" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5019 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5047 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5028 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5075 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5047 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5049 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5064 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5033 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5021 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -