이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
const int MAX_N = 1e3 + 5;
int n;
vector <int> graph[MAX_N];
bool visited[MAX_N];
vector <vector <int>> answer;
int cmp[MAX_N];
int parent[MAX_N];
vector <int> vertex[MAX_N];
int root(int u) {
if(u == parent[u]) {
return u;
}
return parent[u] = root(parent[u]);
}
void dfs(int u) {
visited[u] = true;
for(auto v : graph[u]) {
if(visited[v] == false) {
cmp[v] = cmp[u];
answer[u][v] = answer[v][u] = 1;
dfs(v);
}
}
}
int solve(vector <int> nodes, vector <vector <int>> p) {
bool two = false, three = false;
for(auto u : nodes) {
for(auto v : nodes) {
if(p[u][v] == 1 and u != v) {
graph[u].push_back(v);
graph[v].push_back(u);
}
}
}
for(auto u : nodes) {
if(visited[u] == false) {
visited[u] = true;
cmp[u] = u;
dfs(u);
}
}
for(auto u : nodes) {
parent[u] = u;
}
for(auto u : nodes) {
for(auto v : nodes) {
if(p[u][v] == 2 or p[u][v] == 3) {
if(p[u][v] == 2) {
two = true;
}
else {
three = true;
}
parent[root(cmp[v])] = root(cmp[u]);
}
}
}
if(two == true and three == true) {
return 0;
}
else if(two == true or three == true) {
vector <int> cycle;
for(auto u : nodes) {
if(cmp[u] == u) {
cycle.push_back(u);
}
}
if(two == true and cycle.size() <= 2) {
return 0;
}
if(three == true and cycle.size() <= 3) {
return 0;
}
int pr = cycle.back();
for(auto v : cycle) {
answer[v][pr] = answer[pr][v] = 1;
pr = v;
}
if(three == true) {
answer[cycle[0]][cycle[2]] = answer[cycle[2]][cycle[0]] = 1;
}
}
return 1;
}
int construct(vector <vector <int>> p) {
n = p.size();
fill(cmp, cmp + n, -1);
answer.resize(n, vector <int> (n, 0));
iota(parent, parent + n, 0);
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(p[i][j] != 0) {
parent[root(j)] = root(i);
}
}
}
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(p[i][j] == 0 and root(i) == root(j)) {
return 0;
}
}
}
vector <int> components;
for(int i = 0; i < n; i++) {
vertex[root(i)].push_back(i);
if(i == root(i)) {
components.push_back(i);
}
}
for(auto c : components) {
if(solve(vertex[c], p) == 0) {
return 0;
}
}
build(answer);
return 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... |