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 <vector>
using namespace std;
#include <bits/stdc++.h>
vector<vector<int>> p;
vector<vector<int>> groups;
vector<vector<int>> answer;
vector<int> isIn;
int n;
void dfs(int node, int cur, int par)
{
isIn[node] = cur;
groups[cur].push_back(node);
if (par != -1)
{
answer[node][par] = 1;
answer[par][node] = 1;
}
for (int i = 0; i < n; i++)
{
if (isIn[i] == -1 && p[node][i] == 1)
{
dfs(i, cur, node);
}
}
}
int construct(std::vector<std::vector<int>> P) {
p = P;
n = p.size();
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if (p[i][j] >= 3) return 0;
}
}
isIn.resize(n, -1);
answer.resize(n, vector<int>(n));
int cur = 0;
for (int i = 0; i < n; i++)
{
if (isIn[i] == -1)
{
groups.push_back(vector<int>());
dfs(i, cur, -1);
cur++;
}
}
for (auto g : groups)
{
for (int i = 0; i < g.size(); i++)
{
for (int j = i + 1; j < g.size(); j++)
{
if (p[g[i]][g[j]] != 1) return 0;
}
}
}
vector<vector<int>> relation(n, vector<int>(n, -1));
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (isIn[i] != isIn[j])
{
if (relation[isIn[i]][isIn[j]] == -1)
{
relation[isIn[i]][isIn[j]] = p[i][j];
relation[isIn[j]][isIn[i]] = p[i][j];
} else if (relation[isIn[i]][isIn[j]] != p[i][j]) return 0;
}
}
}
for (int i = 0; i < groups.size(); i++)
{
int next = i + 1;
while (next != i)
{
next %= groups.size();
if (relation[i][next] == 2)
{
answer[groups[i].front()][groups[next].front()] = 1;
answer[groups[next].front()][groups[i].front()] = 1;
break;
}
next++;
next %= groups.size();
}
}
build(answer);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < g.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:62:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int j = i + 1; j < g.size(); j++)
| ~~^~~~~~~~~~
supertrees.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < groups.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... |