이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "supertrees.h"
#include "bits/stdc++.h"
#define MAXN 1005
using namespace std;
int N;
int sizes[MAXN], par[MAXN];
vector<int> comps[MAXN], comps1[MAXN];
vector<vector<int>> answer;
int find(int u) {
if (u != par[u])
par[u] = find(par[u]);
return par[u];
}
void join(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
//answer[u][v] = 1;
//answer[v][u] = 1;
if (sizes[u] > sizes[v]) {
par[v] = u;
sizes[u] += sizes[v];
} else {
par[u] = v;
sizes[v] += sizes[u];
}
}
void reset_dsu() {
for (int i = 0; i < N; i++) {
par[i] = i;
sizes[i] = 1;
}
}
int construct(vector<vector<int>> p) {
N = p.size();
answer = vector<vector<int>>(N, vector<int>(N, 0));
//run dsu
reset_dsu();
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (p[i][j])
join(i, j);
}
}
//build components
for (int i = 0; i < N; i++)
comps[find(i)].push_back(i);
//run dsu again to find comps of 1
reset_dsu();
for (int i = 0; i < N; i++) {
for (int j = 0; j < comps[i].size(); j++) {
for (int k = j+1; k < comps[i].size(); k++) {
int x = comps[i][j], y = comps[i][k];
if (p[x][y] == 0) return 0;
if (p[x][y] == 1) join(x, y);
}
}
}
vector<int> reps;
for (int i = 0; i < N; i++)
comps1[find(i)].push_back(i);
//check validity
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (find(i) == find(j) && p[i][j] != 1)
return 0;
}
}
//build paths
for (int i = 0; i < N; i++) {
for (int j = 0; j < ((int) comps1[i].size())-1; j++) {
answer[comps1[i][j]][comps1[i][j+1]] = 1;
answer[comps1[i][j+1]][comps1[i][j]] = 1;
}
}
//build rings
for (int i = 0; i < N; i++) {
vector<int> reps;
for (int j : comps[i]) {
if (j == find(j))
reps.push_back(j);
}
if (reps.size() == 2)
return 0;
if (reps.size() <= 1) continue;
for (int j = 0; j < reps.size() - 1; j++) {
answer[reps[j]][reps[j+1]] = 1;
answer[reps[j+1]][reps[j]] = 1;
}
answer[reps[0]][reps[reps.size()-1]] = 1;
answer[reps[reps.size()-1]][reps[0]] = 1;
}
build(answer);
return 1;
}
컴파일 시 표준 에러 (stderr) 메시지
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:58:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j = 0; j < comps[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:59:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int k = j+1; k < comps[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:97:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int j = 0; j < reps.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... |