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 <bits/stdc++.h>
#include "supertrees.h"
#include <vector>
using namespace std;
int n;
vector<vector<int>> a;
vector<int> col;
vector<vector<int>> r;
vector<int> gr;
vector<vector<int>> c;
vector<vector<int>> ans;
bool check()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[i][j] == 1)
{
if (col[i] != col[j])
return false;
}
else if (a[i][j] == 2)
{
if (col[i] == col[j] || gr[col[i]] != gr[col[j]])
return false;
}
else
{
if (col[i] == col[j] || gr[col[i]] == gr[col[j]])
return false;
}
}
}
return true;
}
void conect(int v, int u)
{
ans[v][u] = 1;
ans[u][v] = 1;
}
void dfs(int v)
{
for (int i = 0; i < n; i++)
{
if (i == v)
continue;
if (a[v][i] == 1 && col[i] == -1)
{
r[col[v]].push_back(i);
col[i] = col[v];
dfs(i);
}
}
}
void dfs2(int v)
{
int u = r[v][0];
for (int i = 0; i < r.size(); i++)
{
if (i == v)
continue;
int to = r[i][0];
if (a[u][to] == 2 && gr[i] == -1)
{
c[gr[v]].push_back(i);
gr[i] = gr[v];
dfs2(i);
}
}
}
int construct(vector<vector<int>> p) {
n = p.size();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (p[i][j] == 3)
return 0;
}
}
a = p;
ans = vector<vector<int>>(n, vector<int>(n, 0));
int cnt = 0;
col = vector<int>(n, -1);
for (int i = 0; i < n; i++)
{
if (col[i] == -1)
{
r.push_back(vector<int>(1, i));
col[i] = cnt++;
dfs(i);
}
}
cnt = 0;
gr = vector<int>(r.size(), -1);
for (int i = 0; i < r.size(); i++)
{
if (gr[i] == -1)
{
c.push_back(vector<int>(1, i));
gr[i] = cnt++;
dfs2(i);
}
}
if (!check())
return 0;
for (int i = 0; i < r.size(); i++)
{
for (int j = 0; j < r[i].size() - 1; j++)
{
conect(r[i][j], r[i][j + 1]);
}
}
for (int i = 0; i < c.size(); i++)
{
for (int j = 0; j < c[i].size() - 1; j++)
{
//cout << r[c[i][j]][0] << ' ' << r[c[i][j + 1]][0] << endl;
conect(r[c[i][j]][0], r[c[i][j + 1]][0]);
}
if (c[i].size() != 1)
{
//cout << r[c[i][c[i].size() - 1]][0] << ' ' << r[c[i][0]][0] << endl;
conect(r[c[i][c[i].size() - 1]][0], r[c[i][0]][0]);
}
}
build(ans);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'void dfs2(int)':
supertrees.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < r.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:104:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int i = 0; i < r.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (int i = 0; i < r.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:117:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (int j = 0; j < r[i].size() - 1; j++)
| ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:122:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for (int i = 0; i < c.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:124:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int j = 0; j < c[i].size() - 1; j++)
| ~~^~~~~~~~~~~~~~~~~
# | 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... |