#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
class EquivRelation {
int n;
vector <bool> vis;
vector <vector <int>> adj;
public:
EquivRelation(int _n) {
n = _n;
adj.resize(n);
vis.resize(n);
}
void addEdge(int u, int v) {
adj[u].push_back(v);
}
void dfs(int x, vector <int> &comp) {
vis[x] = true;
comp.push_back(x);
for (auto it : adj[x])
if (!vis[it])
dfs(it, comp);
}
bool isClique(const vector <int> &v) const {
for (auto it : v)
if (adj[it].size() != v.size())
return false;
return true;
}
vector <vector <int>> getClasses() {
vector <vector <int>> res;
for (int i = 0; i < n; i++)
if (!vis[i]) {
vector <int> curr;
dfs(i, curr);
if (!isClique(curr))
return vector <vector <int>> ();
res.push_back(curr);
}
return res;
}
};
int construct(vector <vector <int>> p) {
int N = p.size();
EquivRelation conn(N), one(N);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
if (p[i][j])
conn.addEdge(i, j);
if (p[i][j] == 1)
one.addEdge(i, j);
else if (p[i][j] == 3)
return 0;
}
vector <vector <int>> comps = conn.getClasses();
vector <vector <int>> groups = one.getClasses();
if (comps.empty() || groups.empty())
return 0;
int sz = comps.size();
vector <int> idx(sz);
for (int i = 0; i < sz; i++)
for (auto it : comps[i])
idx[it] = i;
vector <vector <int>> b(N, vector <int> (N, 0));
auto make_edge = [&](int u, int v) {
b[u][v] = b[v][u] = 1;
};
vector <vector <int>> cycle(sz);
for (auto it : groups) {
for (int i = 1; i < it.size(); i++)
make_edge(it[0], it[i]);
cycle[idx[it[0]]].push_back(it[0]);
}
for (auto it : cycle)
if (it.size() > 1)
for (int i = 0; i < it.size(); i++)
make_edge(it[i], it[(i + 1) % it.size()]);
build(b);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:72:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 1; i < it.size(); i++)
| ~~^~~~~~~~~~~
supertrees.cpp:78:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 0; i < it.size(); i++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Runtime error |
3 ms |
768 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Runtime error |
3 ms |
768 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Incorrect |
1 ms |
256 KB |
Answer gives possible 1 while actual possible 0 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Runtime error |
30 ms |
4856 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Runtime error |
3 ms |
768 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Runtime error |
3 ms |
768 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |