이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define IOI
#ifdef IOI
#include "supertrees.h"
#endif
using namespace std;
const int Nmax = 1e3 + 10;
int n;
#ifndef IOI
std::vector<std::vector<int>> p;
std::vector<std::vector<int>> b;
bool called = false;
void check(bool cond, std::string message) {
if (!cond) {
printf("%s\n", message.c_str());
exit(0);
}
}
void build(std::vector<std::vector<int>> _b) {
check(!called, "build is called more than once");
called = true;
check((int)_b.size() == n, "Invalid number of rows in b");
for (int i = 0; i < n; i++) {
check((int)_b[i].size() == n, "Invalid number of columns in b");
}
b = _b;
}
#endif // IOI
int root[2][Nmax], sz[2][Nmax];
vector <int> rt[Nmax];
int Find(int _, int u) {
return (u == root[_][u] ? u : root[_][u] = Find(_, root[_][u]));
}
void Union(int _, int u, int v) {
if ((u = Find(_, u)) == (v = Find(_, v))) return;
if (sz[_][u] < sz[_][v]) swap(u, v);
sz[_][u] += sz[_][v];
root[_][v] = u;
}
int construct(vector <vector <int> > p) {
int n = p.size();
vector <vector <int> > b(n, vector <int> (n, 0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (p[i][j] != p[j][i] || p[i][j] == 3) return 0;
for (int i = 0; i < n; ++i) if (p[i][i] != 1) return 0;
for (int i = 0; i < n; ++i) for (int j = 0; j < 2; ++j) root[j][i] = i, sz[j][i] = 1;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (p[i][j] == 1) Union(0, i, j);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (Find(0, i) == Find(0, j) && p[i][j] != 1) return 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (p[i][j] == 2) Union(1, Find(0, i), Find(0, j));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (Find(0, i) != Find(0, j) && p[i][j] != 2 && Find(1, Find(0, i)) == Find(1, Find(0, j))) return 0;
for (int i = 0; i < n; ++i)
if (i != Find(0, i)) {
b[i][Find(0, i)] = b[Find(0, i)][i] = 1;
}
for (int i = 0; i < n; ++i) {
if (i == Find(0, i)) rt[Find(1, i)].push_back(i);
}
for (int i = 0; i < n; ++i) {
if (rt[i].size() <= 1) continue;
if (rt[i].size() == 2) return 0;
for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 1;
b[rt[i].front()][rt[i].back()] = b[rt[i].back()][rt[i].front()] = 1;
}
build(b);
return 1;
}
#ifndef IOI
int main() {
freopen("test.inp", "r", stdin);
assert(scanf("%d", &n) == 1);
p.resize(n);
for (int i = 0; i < n; i++) {
p[i].resize(n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
assert(scanf("%d", &p[i][j]) == 1);
}
}
int possible = construct(p);
check(possible == 0 || possible == 1, "Invalid return value of construct");
if (possible == 1) {
check(called, "construct returned 1 without calling build");
} else {
check(!called, "construct called build but returned 0");
}
printf("%d\n", possible);
if (possible == 1) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j) {
printf(" ");
}
printf("%d", b[i][j]);
}
printf("\n");
}
}
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (b[i][j]) cout << i << " " << j << "\n";
}
#endif
컴파일 시 표준 에러 (stderr) 메시지
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 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... |