# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
469075 | hoanghq2004 | 슈퍼트리 잇기 (IOI20_supertrees) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
const int Nmax = 1e3 + 10;
static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;
static 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;
}
int root[3][Nmax], sz[3][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 vis[Nmax];
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]) 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 < 3; ++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));
if (p[i][j] == 3) Union(2, Find(0, i), Find(0, j));
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (p[i][j] == 3 && Find(1, Find(0, i)) == Find(1, Find(0, j))) return 0;
for (int i = 0; i < n; ++i)
if (i != Find(0, i)) {
// cout << i << " " << Find(0, i) << "\n";
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 (auto u: rt[i]) vis[u] = 1;
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;
}
for (int i = 0; i < n; ++i) rt[i].clear();
for (int i = 0; i < n; ++i) {
if (i == Find(0, i)) rt[Find(2, i)].push_back(i);
}
for (int i = 0; i < n; ++i) {
if (rt[i].size() <= 1) continue;
if (rt[i].size() == 2 || rt[i].size() == 3) return 0;
// for (auto u: rt[i]) vis[u] = 1;
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;
b[rt[i][0]][rt[i][2]] = b[rt[i][2]][rt[i][0]] = 1;
}
// for (int i = 0; i < n; ++i) if (i == Find(0, i) && !vis[i]) return 0;
build(b);
return 1;
}
//
//int main() {
// 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");
// }
// }
//}