Submission #304886

#TimeUsernameProblemLanguageResultExecution timeMemory
304886Jarif_RahmanConnecting Supertrees (IOI20_supertrees)C++17
96 / 100
284 ms27512 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
vector<vector<int>> v;
vector <vector<int>> dsu1, dsu11;
vector <int> dsu2, dsu12;
void unite(int a, int b){
    if(dsu2[a] == dsu2[b]) return;
    int x = dsu2[a], y = dsu2[b];
    if(dsu1[x].size() < dsu1.size()){
        while(!dsu1[x].empty()){
            int ss = dsu1[x].back();
            dsu1[x].pop_back();
            dsu2[ss] = y;
            dsu1[y].pb(ss);
        }
    }
    else{
        while(!dsu1[y].empty()){
            int ss = dsu1[y].back();
            dsu1[y].pop_back();
            dsu2[ss] = x;
            dsu1[x].pb(ss);
        }
    }
}
void unite1(int a, int b){
    if(dsu12[a] == dsu12[b]) return;
    int x = dsu12[a], y = dsu12[b];
    if(dsu11[x].size() < dsu11.size()){
        while(!dsu11[x].empty()){
            int ss = dsu11[x].back();
            dsu11[x].pop_back();
            dsu12[ss] = y;
            dsu11[y].pb(ss);
        }
    }
    else{
        while(!dsu1[y].empty()){
            int ss = dsu1[y].back();
            dsu1[y].pop_back();
            dsu2[ss] = x;
            dsu1[x].pb(ss);
        }
    }
}
int construct(vector<vector<int>> p){
    v.clear();
    dsu1.clear();
    dsu2.clear();
    dsu11.clear();
    dsu12.clear();
    int n = p.size();
    v = vector<vector<int>>(n, vector<int>(n, 0));
    dsu1 = vector<vector<int>>(n);
    dsu2 = vector<int>(n);
    for(int i = 0; i < n ; i++) dsu1[i].pb(i);
    for(int i = 0; i < n; i++) dsu2[i] = i;
    dsu11 = dsu1;
    dsu12 =  dsu2;
    vector <int> zot(4, 0);
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(i != j) zot[p[i][j]]++;
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(p[i][j] != 0) unite(i, j);
    if(zot[2] > 0 && zot[3] > 0) return 0;
    bool bl = 1;
    for(auto sth: dsu1){
        if(sth.size() < 2) continue;
        if(sth.size() == 2){
            int x = sth[0], y = sth[1];
            if(p[x][y] != 1){
                bl = 0;
                break;
            }
            else continue;
        }
        for(int i = 0; i < sth.size(); i++) for(int j = 0; j < sth.size(); j++){
            if(i == j) continue;
            if(p[sth[i]][sth[j]] == 0){
                bl = 0;
                break;
            }
        }
        if(sth.size() == 3){
            for(int x: sth) for(int y: p[x]) if(y == 3) return 0;
        }
    }
    if(!bl) return 0;
    for(auto sth: dsu1){
        if(sth.size() < 2) continue;
        vector <int> ttt, one;
        for(int x: sth) for(int y: sth){
            if(x == y) continue;
            if(p[x][y] == 1) unite1(x, y);
        }
        for(int x: sth){
            int o = 0,tw = 0;
            for(int k = 0; k < n; k++){
              	if(k == x) continue;
                if(p[x][k] == 1) o++;
            }
            if(o == 0) ttt.pb(x);
            else one.pb(x);
        }
        for(int a: sth){
            if(dsu11[a].size() < 2) continue;
            for(int x: dsu11[a]){
                int o = 0, tw = 0;
                for(int y: dsu11[a]) if(p[x][y] != 1) return 0;
                for(int y: p[x]) if(y == 1) o++;
                if(o > dsu11[a].size()) return 0;
            }
            for(int i = 0; i < (int)dsu11[a].size() - 1; i++){
                int x = dsu11[a][i], y = dsu11[a][i+1];
                v[x][y] = 1;
                v[y][x] = 1;
            }
        }
        vector <int> oo;
        for(int a: sth) if(dsu11[a].size() > 0) oo.pb(dsu11[a][0]);
        for(int i = 1; i < oo.size(); i++){
            int x = oo[i], y = oo[i-1];
            v[x][y] = 1;
            v[y][x] = 1;
        }
        if(oo.size() > 1 && zot[2] > 0){
            int x = oo[0], y = oo.back();
            v[x][y] = 1;
            v[y][x] = 1;
        }
        if(oo.size() != 1 && oo.size() < 4 && zot[3] > 0) return 0;
        if(oo.size() > 3 && zot[3] > 0){
            int x = oo[0], y = oo[1], z = oo.back();
            v[x][z] = 1;
            v[z][x] = 1;
            v[y][z] = 1;
            v[z][y] = 1;
        }
    }
    if(!bl) return 0;
    build(v);
    return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:81:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for(int i = 0; i < sth.size(); i++) for(int j = 0; j < sth.size(); j++){
      |                        ~~^~~~~~~~~~~~
supertrees.cpp:81:62: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for(int i = 0; i < sth.size(); i++) for(int j = 0; j < sth.size(); j++){
      |                                                            ~~^~~~~~~~~~~~
supertrees.cpp:101:23: warning: unused variable 'tw' [-Wunused-variable]
  101 |             int o = 0,tw = 0;
      |                       ^~
supertrees.cpp:115:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |                 if(o > dsu11[a].size()) return 0;
      |                    ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:112:28: warning: unused variable 'tw' [-Wunused-variable]
  112 |                 int o = 0, tw = 0;
      |                            ^~
supertrees.cpp:125:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |         for(int i = 1; i < oo.size(); i++){
      |                        ~~^~~~~~~~~~~
#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...