Submission #814834

#TimeUsernameProblemLanguageResultExecution timeMemory
814834jlallas384Connecting Supertrees (IOI20_supertrees)C++17
100 / 100
170 ms24068 KiB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;

struct ufds{
    vector<int> sz, par;
    ufds(int n): sz(n, 1), par(n){
        iota(par.begin(), par.end(), 0);
    }
    int find(int a){
        return par[a] == a ? a : par[a] = find(par[a]);
    }
    int unite(int a, int b){
        a = find(a), b = find(b);
        if(a == b) return 0;
        if(sz[a] < sz[b]) swap(a, b);
        par[b] = a;
        sz[a] += sz[b];
        return 1;
    }
};

int construct(std::vector<std::vector<int>> p){
    int n = p.size();
    vector<vector<int>> ans(n, vector<int>(n));
    ufds ds(n);
    for(int i = 0; i < n; i++){
        if(p[i][i] != 1) return 0;
        for(int j = 0; j < n; j++){
            if(p[i][j] != p[j][i]) return 0;
            if(p[i][j] == 3) return 0;
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(p[i][j] == 1){
                ds.unite(i, j);
            }else if(p[i][j] == 0){
                if(ds.find(i) == ds.find(j)){
                    return 0;
                }
            }
        }
    }
    vector<int> rep;
    for(int i = 0; i < n; i++){
        int p = ds.find(i);
        if(i != p){
            ans[i][p] = ans[p][i] = 1;
        }else{
            rep.push_back(i);
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(p[i][j] == 2){
                ds.unite(i, j);
            }else if(p[i][j] == 0){
                if(ds.find(i) == ds.find(j)){
                    return 0;
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(p[i][j] == 0 && ds.find(i) == ds.find(j)) return 0;
        }
    }
    vector<vector<int>> comp(n);
    for(int x: rep){
        comp[ds.find(x)].push_back(x);
    }
    for(int i = 0; i < n; i++){
        if(comp[i].size() == 2) return 0;
        else if(comp[i].size() >= 3){
            for(int j = 0; j < comp[i].size(); j++){
                int a = comp[i][j], b = comp[i][(j + 1) % comp[i].size()];
                ans[a][b] = ans[b][a] = 1;
            }
        }
    }
    build(ans);
    return 1;
}


Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:77:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |             for(int j = 0; j < comp[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...