Submission #568966

#TimeUsernameProblemLanguageResultExecution timeMemory
568966StickfishConnecting Supertrees (IOI20_supertrees)C++17
0 / 100
1 ms212 KiB
#include "supertrees.h"
#include <vector>
#include <iostream>
using namespace std;

struct dsu {
    void resize(int n) {
        par.resize(n);
        sz.assign(n, 1);
        for (int i = 0; i < n; ++i)
            par[i] = i;
    }

    int gst(int i) {
        if (par[i] == i)
            return i;
        return par[i] = gst(par[i]);
    }

    void unite(int i, int j) {
        i = gst(i);
        j = gst(j);
        if (i == j)
            return;
        if (sz[i] < sz[j])
            swap(i, j);
        par[j] = i;
        sz[i] += sz[j];
    }
 private:
     vector<int> par;
     vector<int> sz;
};

int construct(vector<vector<int>> p) {
	int n = p.size();
    dsu suall;
    suall.resize(n);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 2)
                suall.unite(i, j);
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 0 && suall.gst(i) == suall.gst(j))
                return 0;
        }
    }
    //for (int i = 0; i < n; ++i)
        //cout << suall.gst(i) << ' ';
    //cout << endl;
    //for (int i = 0; i < n; ++i)
        //cout << su1.gst(i) << ' ';
    //cout << endl;
    vector<vector<int>> ans(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
        if (suall.gst(i) == i) {
            vector<int> comp;
            for (int j = 0; j < n; ++j) {
                if (suall.gst(j) == i)
                    comp.push_back(j);
            }
            vector<int> cycle = comp;
            if (cycle.size() > 1) {
                for (int t = 0; t < cycle.size(); ++t) {
                    int v = cycle[t];
                    int u = cycle[(t + 1) % cycle.size()];
                    ans[u][v] = ans[v][u] = 1;
                }
            }
        }
    }
    build(ans);
    return 1;
    
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:67:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |                 for (int t = 0; t < cycle.size(); ++t) {
      |                                 ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...