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 MAX = 1005;
int par[MAX];
int find_set(int v){
if(par[v] < 0) return v;
return par[v] = find_set(par[v]);
}
bool unite(int u, int v){
u = find_set(u);
v = find_set(v);
if(u == v) return false;
par[u] += par[v];
par[v] = u;
return true;
}
vector<int> g[MAX];
bool visited[MAX];
void dfs(int node, vector<int>& v){
v[node] += 1;
visited[node] = 1;
for(int& to:g[node]){
if(!visited[to]){
dfs(to, v);
}
}
visited[node] = 0;
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> ans(n, vector<int>(n));
vector<bool> except(n, 0);
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if(p[i][j] == 1){
except[i] = 1;
ans[i][j] = 1;
ans[j][i] = 1;
break;
}
}
}
memset(par, -1, sizeof(par));
for (int i = 0; i < n; i++)
{
if(except[i]) continue;
for (int j = 0; j < n; j++)
{
if(except[j]) continue;
if(p[i][j] == 2){
unite(i, j);
}
}
}
vector<int> comps[MAX];
for (int i = 0; i < n; i++)
{
if(except[i]) continue;
comps[find_set(i)].push_back(i);
}
for(auto& v:comps){
for (int i = 0; i < v.size(); i++)
{
if(i != 0){
ans[v[i]][v[i - 1]] = 1;
}
if(i != v.size() - 1){
ans[v[i]][v[i + 1]] = 1;
}
}
if(v.size() > 2){
ans[v.front()][v.back()] = 1;
ans[v.back()][v.front()] = 1;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if(ans[i][j]){
g[i].push_back(j);
}
}
}
for (int i = 0; i < n; i++)
{
vector<int> v(n, 0);
dfs(i, v);
if(v != p[i]) return 0;
}
build(ans);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:74:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | if(i != v.size() - 1){
| ~~^~~~~~~~~~~~~~~
# | 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... |