Submission #621288

#TimeUsernameProblemLanguageResultExecution timeMemory
621288Mr_HusanboyConnecting Supertrees (IOI20_supertrees)C++14
46 / 100
198 ms24116 KiB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;


struct DSU{
    vector<int> parent, sz;
    void init(int n){
        parent.resize(n), sz.resize(n);
        for(int i = 0; i < n; i ++) parent[i] = i, sz[i] = 1;
    }

    int get(int a){
        return parent[a] == a ? a : parent[a] = get(parent[a]);
    }

    void unite(int a, int b){
        a = get(a); b = get(b);
        if(a==b) return;
        if(sz[a] < sz[b]) swap(a,b);
        sz[a] += sz[b];
        parent[b] = a;
    }
};


int construct(vector<vector<int>> p) {

	int n = p.size();
	vector<vector<int>> b(n,vector<int> (n));
    DSU t; t.init(n);
    for(int j = 0; j < n; j ++){
        for(int i = 0; i < n; i ++){
            if(p[i][j] == 1) t.unite(i,j);
        }
    }

    vector<vector<int>> par(n);
 //   cout << "a" << endl;
    for(int i = 0; i < n; i ++){
        par[t.get(i)].push_back(i);
    }

    vector<int> comp;
//    cout << "b" << endl;
    for(int i = 0; i < n; i ++){
        if(par[i].empty()) continue;
        for(int j = 1; j < par[i].size(); j ++){
            int u = par[i][j-1], v = par[i][j];
            b[u][v] = b[v][u] = 1;
        }
        comp.push_back(par[i][0]);
    }
    t.init(n);
    for(int i = 0; i < comp.size(); i ++){
        for(int j = i + 1; j < comp.size(); j++){
            int u = comp[i], v = comp[j];
            if(p[u][v] == 2){
                t.unite(u,v); //cout << u << ' ' << v << endl;
            }
        }
    }
    for(int i = 0; i < n; i ++) par[i].clear();
    for(int i = 0; i < n; i ++){
        par[t.get(i)].push_back(i);
    }
    for(int i = 0; i < n; i ++){
        for(int j = 1; j < par[i].size(); j ++){
            int u = par[i][j-1], v = par[i][j];
            b[u][v] = b[v][u] = 1;
        }
        if(par[i].size() > 1){
            int u = par[i][0], v = par[i].back();
            b[u][v] = b[v][u] = 1;
        }
    }


    build(b);
    return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:49:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |         for(int j = 1; j < par[i].size(); j ++){
      |                        ~~^~~~~~~~~~~~~~~
supertrees.cpp:56:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     for(int i = 0; i < comp.size(); i ++){
      |                    ~~^~~~~~~~~~~~~
supertrees.cpp:57:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(int j = i + 1; j < comp.size(); j++){
      |                            ~~^~~~~~~~~~~~~
supertrees.cpp:69:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         for(int j = 1; j < par[i].size(); j ++){
      |                        ~~^~~~~~~~~~~~~~~
#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...