# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
301597 | Dilshod_Imomov | Connecting Supertrees (IOI20_supertrees) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int nn, comp, used[N], cc[N], cmp[N], ok, first[N], nb[N][2];
set < int > sz[N];
int construct(std::vector<std::vector<int>> p) {
nn = p.size();
std::vector<std::vector<int>> answer;
vector < pair < int, int > > vc;
for (int i = 0; i < nn; i++) {
std::vector<int> row;
row.resize(nn);
answer.push_back(row);
}
int sub1 = 1, sub2 = 0;
for ( int i = 0; i < nn; i++ ) {
for ( int j = 0; j < nn ;j++ ) {
if ( p[i][j] == 2 ) {
sub1 = 0;
}
}
}
if ( !sub1 ) {
for ( int i = 0; i < nn; i++ ) {
for ( int j = 0; j < nn; j++ ) {
if ( p[i][j] == 1 && i != j ) {
p[i][j] = 2;
p[j][i] = 2;
vc.push_back( { i, j } );
vc.push_back( { j, i } );
sub2 = 1;
}
}
}
}
for ( int i = 0; i < nn; i++ ) {
if ( !cc[i] ) {
cc[i] = ++comp;
first[comp] = i;
sz[cc[i]].insert(i);
}
cmp[ cc[i] ] = i;
for ( int j = 0; j < nn; j++ ) {
if ( p[i][j] && i != j ) {
if ( cc[j] && cc[j] != cc[i] ) {
return 0;
}
if( cc[j] ) {
continue;
}
cc[j] = cc[i];
sz[cc[i]].insert(j);
nb[ cmp[ cc[i] ] ][1] = j;
nb[ j ][0] = cmp[ cc[i] ];
answer[ cmp[ cc[i] ] ][j] = 1;
answer[ j ][ cmp[ cc[i] ] ] = 1;
cmp[ cc[i] ] = j;
}
}
}
if ( !sub1 ) {
for ( int i = 1; i <= comp; i++ ) {
if ( !sub1 && sub2 ) {
if ( sz[i].size() == 2 ) {
auto it = sz[i].begin();
used[*it] = 1;
it++;
used[*it] = 1;
continue;
}
}
if ( cmp[ i ] != first[i] ) {
answer[cmp[i]][first[i]] = 1;
answer[ first[i] ][cmp[i]] = 1;
nb[ cmp[i] ][1] = first[i];
nb[ first[i] ][0] = cmp[i];
}
if ( sz[i].size() == 2 ) {
return 0;
}
}
}
if ( !sub1 && sub2 ) {
for ( auto pr: vc ) {
int u = pr.first, v = pr.second;
if ( used[u] || used[v] ) {
continue;
}
//cout << u << ' ' << v << endl;
//cout << nb[u][0] << ' ' << u << ' ' << nb[u][1] << '\n';
if ( nb[u][1] == v ) {
//cout << 1 << endl;
answer[v][ nb[v][1] ] = 0;
answer[ nb[v][1] ][v] = 0;
answer[ u ][ nb[v][1] ] = 1;
answer[ nb[v][1] ][ u ] = 1;
used[v] = 1;
nb[u][1] = nb[v][1];
nb[ nb[v][1] ][0] = u;
}
else {
//cout << 2 << endl;
answer[v][ nb[v][0] ] = 0;
answer[ nb[v][0] ][v] = 0;
answer[ u ][ nb[v][0] ] = 1;
answer[ nb[v][0] ][ u ] = 1;
used[v] = 1;
nb[u][0] = nb[v][0];
nb[ nb[v][0] ][1] = u;
}
//cout << nb[u][0] << ' ' << u << ' ' << nb[u][1] << endl;
}
for ( int i = 0; i < nn; i++ ) {
anwer[i][i] = 0;
}
return 1;
build(answer);
}
for ( int i = 0; i < nn; i++ ) {
for ( int j = 0; j < nn; j++ ) {
if ( cc[i] == cc[j] ) {
if ( !p[i][j] || !p[j][i] ) {
return 0;
}
}
}
}
build(answer);
return 1;
}
/*
7
1 2 2 2 1 0 0
2 1 2 2 2 0 0
2 2 1 1 2 0 0
2 2 1 1 2 0 0
1 2 2 2 1 0 0
0 0 0 0 0 1 1
0 0 0 0 0 1 1
*/