Submission #310437

#TimeUsernameProblemLanguageResultExecution timeMemory
310437DS007Connecting Supertrees (IOI20_supertrees)C++14
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;

class DSU {
    int par[N], n;
    
public:
    DSU(int n) {
        this->n = n;
        iota(par, par + n, 0);
    }
    
    int find(int x) {
        if (x == par[x])
            return x;
        return par[x] = find(par[x]);
    }
    
    void merge(int u, int v) {
        u = find(u), v = find(v);
        par[u] = v;
    }
};

int construct(vector<vector<int>> p) {
    int n = p.size();
    DSU dsu(n);
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 3)
                return 1;
            dsu.merge(i, j);
        }
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 0 && dsu.find(i) == dsu.find(j))
                return 1;
        }
    }
    
    vector<vector<int>> comp(n);
    vector<vector<int>> b(n);
    for (int i = 0; i < n; i++) b[i].resize(n, 0);
    vector<bool> done(n, false);
    
    for (int i = 0, k = 0; i < n; i++) {
        if (done[i])
            continue;
            
        comp[k].push_back(i);
        for (int j = i + 1; j < n; j++) {
            if (dsu.find(i) == dsu.find(j))
                comp[k].push_back(j), done[j] = true;
        }
        k++;
    }
    
    for (int i = 0; i < comp.size() && !comp[i].empty(); i++) {
        //cout << "Done" << endl;
        set<int> s;
        for (int j = 0; j < comp[i].size(); j++) {
            for (int k = 0; k < comp[i].size(); k++) {
                if (k != j && p[comp[i][j]][comp[i][k]] == 1)
                    s.insert(comp[i][j]), s.insert(comp[i][k]);
            }
        }
        
        for (int j : s) {
            for (int k : s) {
                if (p[j][k] != 1)
                    return 1;
            }
        }
        
        if (s.size() == comp[i].size() - 1)
            return 1;
            
        if (s.empty()) {
            if (comp[i].size() == 2)
                return 1;
                
            int last = -1;
            for (int j : s) {
                if (last != -1)
                    b[j][last] = b[last][j] = 1;
                last = j;
            }
            b[*s.begin()][*s.rbegin()] = b[*s.rbegin()][*s.begin()] = 1;
        }
        
        int last = -1;
        for (int j : s) {
            if (last != -1)
                b[j][last] = b[last][j] = 1;
            last = j;
        }
        
        int last1 = -1, first1 = -1;
        for (int j : comp[i]) {
            if (last1 != -1)
                b[last1][j] = b[j][last1] = 1;
            else
                first1 = j;
            last1 = j;
        }
        
        b[last][first1] = b[first1][last] = 1;
        b[last][last1] = b[last1][last] = 1;
    }
    
    build(b);
    return 0;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int i = 0; i < comp.size() && !comp[i].empty(); i++) {
      |                     ~~^~~~~~~~~~~~~
supertrees.cpp:65:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         for (int j = 0; j < comp[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~
supertrees.cpp:66:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |             for (int k = 0; k < comp[i].size(); k++) {
      |                             ~~^~~~~~~~~~~~~~~~
supertrees.cpp:115:5: error: 'build' was not declared in this scope
  115 |     build(b);
      |     ^~~~~