이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> par, sizes;
int get(int u) {
if (par[u] == u) return u;
return par[u] = get(par[u]);
}
bool areInOne(int u, int v) {
return get(u) == get(v);
}
void union_sets(int u, int v) {
int a = get(u), b = get(v);
if (a == b) return;
if (sizes[a] < sizes[b]) swap(a, b);
sizes[a] += sizes[b];
par[b] = a;
}
vector<int> comp;
void dfs(int u, vector<vector<int>>& gp, vector<bool>& vis) {
comp.push_back(u);
vis[u] = 1;
for (int& v : gp[u]) {
if (!vis[v]) {
dfs(v, gp, vis);
}
}
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> gp(n, vector<int>(n));
par = sizes = vector<int>(n);
for (int i = 0; i < n; i++) par[i] = i, sizes[i] = 1;
set<int> vec;
vector<vector<int>> gppppp(n);
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] == 3) {
gppppp[i].push_back(j);
gppppp[j].push_back(i);
union_sets(i, j);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] != 3 && areInOne(i, j)) return 0;
}
}
vector<bool> mv(n);
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] == 3) {
if (!mv[i]) {
comp.clear();
dfs(i, gppppp, mv);
if (comp.size() < 4) return 0;
for (int i = 0; i < comp.size() - 1; ++i) gp[comp[i]][comp[i + 1]] = gp[comp[i + 1]][comp[i]] = 1;
gp[comp.back()][comp[0]] = gp[comp[0]][comp.back()] = 1;
gp[comp[0]][comp[2]] = gp[comp[2]][comp[0]];
set<int> st;
for (auto x : comp) {
for (int i = 0; i < n; ++i) {
if (p[x][i] && st.find(i) != st.end()) return 0;
if(p[x][i] && p[x][i] < 3)st.insert(i);
}
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (!areInOne(i, j) && p[i][j] == 1) {
gp[i][j] = gp[j][i] = 1;
union_sets(i, j);
}
}
}
vector<bool> used(n);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j] == 1) {
used[i] = used[j] = 1;
vec.insert(get(i));
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int u = get(i), v = get(j);
if (u == v && !p[i][j]) return 0;
if (u != v && p[i][j]==1) return 0;
}
}
vector<vector<int>> graph(n);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j]) {
union_sets(i, j);
graph[i].push_back(j);
graph[j].push_back(i);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int u = get(i), v = get(j);
if (u == v && !p[i][j]) return 0;
if (u != v && p[i][j]) return 0;
}
}
vector<bool> vis(n);
for (int i = 0; i < n; i++) {
if (vis[i]) continue;
comp.clear();
dfs(i, graph, vis);
bool f = 0;
vector<int> rightComp;
int node = -1;
for (int i = 0; i < comp.size(); i++) {
if (!used[comp[i]]) rightComp.push_back(comp[i]);
else if (vec.find(comp[i]) != vec.end()) rightComp.push_back(comp[i]);
}
for (int i = 0; i < rightComp.size() - 1; i++) {
gp[rightComp[i]][rightComp[i + 1]] = gp[rightComp[i + 1]][rightComp[i]] = 1;
}
if (rightComp.size() > 2) gp[rightComp[0]][rightComp.back()] = gp[rightComp.back()][rightComp[0]] = 1;
else if (rightComp.size() == 2) {
return 0;
}
}
/*
1 1 2 2 2
1 1 2 2 2
2 2 1 1 2
2 2 1 1 2
2 2 2 2 1
*/
build(gp);
return 1;
}
컴파일 시 표준 에러 (stderr) 메시지
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:67:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < comp.size() - 1; ++i) gp[comp[i]][comp[i + 1]] = gp[comp[i + 1]][comp[i]] = 1;
| ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:131:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | for (int i = 0; i < comp.size(); i++) {
| ~~^~~~~~~~~~~~~
supertrees.cpp:135:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for (int i = 0; i < rightComp.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:128:14: warning: unused variable 'f' [-Wunused-variable]
128 | bool f = 0;
| ^
supertrees.cpp:130:13: warning: unused variable 'node' [-Wunused-variable]
130 | int node = -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... |