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 <iostream>
#include <vector>
#include <algorithm>
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
vector<vector<int> > out;
int n;
bool check_components(vector<vector<int> > p) {
vector<int> comp(n, -1);
for (int i = 0; i < n; i++) {
if (p[i][i] != 1)
return false;
if (comp[i] == -1) {
for (int j = 0; j < n; j++) {
if (p[i][j] != 0 and comp[j] != -1)
return false;
if (p[i][j] != 0)
comp[j] = i;
}
}
else {
for (int j = 0; j < n; j++)
if (p[i][j] != 0 and comp[j] != comp[i])
return false;
}
}
return true;
}
void make_line(vector<int> a) {
if (a.size() == 1)
return;
for (int i = 0; i < a.size() - 1; i++)
{
int f = a[i], s = a[i + 1];
out[f][s] = 1, out[s][f] = 1;
}
}
vector<vector<int> > p;
vector<bool> vis;
vector<int> ch;
void dfs(int c) {
ch.push_back(c);
vis[c] = true;
for (int i = 0; i < n; i++) {
if (p[i][c] == 1 and !vis[i])
dfs(i);
}
}
int construct(std::vector<std::vector<int>> p) {
n = p.size();
::p = p;
std::vector<std::vector<int>> answer;
if (check_components(p) == false)
return 0;
vector<bool> visited(n);
out = vector<vector<int> >(n, vector<int>(n, 0));
for (int v = 0; v < n; v++) {
if (visited[v])
continue;
vector<int> mine;
for (int j = 0; j < n; j++)
if (p[v][j] != 0)
mine.push_back(j), visited[j] = true;
if (mine.size() == 1)
continue;
vector<int> everyone_else = mine;
vector<vector<int> > chains;
vis = vector<bool>(n);
for (int a : everyone_else) {
// now we decompose into sub-chains
ch.clear();
if (!vis[a]) {
dfs(a);
chains.push_back(ch);
}
}
// now check if chains are good
for (int i = 0; i < chains.size(); i++) {
for (int a : chains[i])
for (int b : chains[i])
if (p[a][b] != 1)
return 0;
for (int j = i + 1; j < chains.size(); j++) {
for (int a : chains[i])
for (int b : chains[j])
if (p[a][b] != 2)
return 0;
}
}
for (vector<int> chai : chains)
make_line(chai);
// connecting chains and cyc
if (chains.size() > 1) {
vector<int> fin;
for (int i = 0; i < chains.size(); i++)
fin.push_back(chains[i][0]);
fin.push_back(chains[0][0]);
if (chains.size() == 2)
return 0;
make_line(fin);
}
}
build(out);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'void make_line(std::vector<int>)':
supertrees.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < a.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:90:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i < chains.size(); i++) {
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int j = i + 1; j < chains.size(); j++) {
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:108:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (int i = 0; i < chains.size(); i++)
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |