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>
#include <unordered_set>
#include <unordered_map>
using namespace std;
vector<vector<int>> groups;
int n;
vector<vector<int>> adj;
vector<bool> vis;
int total;
unordered_map<int, int> group;
// void build(vector<vector<int>> answer) {
// for (int i = 0; i < answer.size(); i++) {
// for (int j = 0; j < answer.size(); j++) {
// printf("%d ", answer[i][j]);
// }
// printf("\n");
// }
// }
void dfs(int s) {
vis[s] = true;
groups[groups.size() - 1].push_back(s);
group[s] = groups.size() - 1;
for (auto u: adj[s]) {
total++;
if (!vis[u]) {
dfs(u);
}
}
}
int construct(vector<vector<int>> p) {
n = p.size();
adj.resize(n);
vis.resize(n, false);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j] == 1) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
vector<int> pb;
bool ok = true;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
total = 0;
groups.push_back(pb);
dfs(i);
int t = groups[groups.size() - 1].size();
if (t * (t - 1) != total) ok = false;
}
}
if (!ok) return 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j] == 2) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
unordered_set<int> rep;
for (auto u: groups) {
rep.insert(u[0]);
}
vector<vector<int>> concomp;
for (int i = 0; i < n; i++) vis[i] = false;
for (auto u: groups) {
if (!vis[u[0]]) {
vis[u[0]] = true;
concomp.push_back(pb);
concomp[concomp.size() - 1].push_back(u[0]);
for (auto v: adj[u[0]]) {
if (!vis[v]) {
if (rep.find(v) != rep.end()) concomp[concomp.size() - 1].push_back(v);
vis[v] = true;
}
}
}
}
ok = true;
for (int i = 0; i < concomp.size(); i++) {
if (concomp[i].size() == 2) ok = false;
}
for (int i = 0; i < concomp.size(); i++) {
unordered_set<int> curcomp;
for (int j = 0; j < concomp[i].size(); j++) {
for (auto u: groups[group[concomp[i][j]]]) {
curcomp.insert(u);
}
}
for (auto u: curcomp) {
for (int i = 0; i < n; i++) {
if (curcomp.find(i) != curcomp.end()) if (p[u][i] == 0) ok = false;
if (curcomp.find(i) == curcomp.end()) if (p[u][i] != 0) ok = false;
}
}
}
if (!ok) {
// printf("first\n");
return 0;
}
vector<vector<int>> answer(n, vector<int>(n, 0));
for (int i = 0; i < groups.size(); i++) {
for (int j = 1; j < groups[i].size(); j++) {
answer[groups[i][0]][groups[i][j]] = 1;
answer[groups[i][j]][groups[i][0]] = 1;
}
}
for (int i = 0; i < concomp.size(); i++) {
if (concomp[i].size() != 1) {
for (int j = 0; j < concomp[i].size() - 1; j++) {
answer[concomp[i][j]][concomp[i][j+1]] = 1;
answer[concomp[i][j+1]][concomp[i][j]] = 1;
}
answer[concomp[i][concomp[i].size() - 1]][concomp[i][0]] = 1;
answer[concomp[i][0]][concomp[i][concomp[i].size() - 1]] = 1;
}
}
build(answer);
return 1;
}
// int main() {
// int n;
// scanf("%d", &n);
// vector<vector<int>> p(n, vector<int>(n));
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// scanf("%d", &p[i][j]);
// }
// }
// printf("Return value: %d\n", construct(p));
// return 0;
// }
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 0; i < concomp.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
supertrees.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < concomp.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
supertrees.cpp:94:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int j = 0; j < concomp[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~
supertrees.cpp:112:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for (int i = 0; i < groups.size(); i++) {
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:113:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int j = 1; j < groups[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:118:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for (int i = 0; i < concomp.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
supertrees.cpp:120:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int j = 0; j < concomp[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... |