# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1119104 | adaawf | 슈퍼트리 잇기 (IOI20_supertrees) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
//#include "supertrees.h"
using namespace std;
vector<int> v[1005], vv[1005];
int p[1005];
vector<vector<int>> res;
int Find(int x) {
if (x == p[x]) return x;
return p[x] = Find(p[x]);
}
void Merge(int x, int y, int fl) {
x = Find(x);
y = Find(y);
if (x == y) return;
if (v[x].size() < v[y].size()) {
swap(x, y);
}
for (int w : v[y]) v[x].push_back(w);
v[y].clear();
if (fl == 0) res[x][y] = res[y][x] = 1;
p[y] = x;
if (fl == 1) {
for (int w : vv[y]) vv[x].push_back(w);
}
}
void build(vector<vector<int>> b) {
for (int i = 0; i < b.size(); i++) {
for (int j = 0; j < b.size(); j++) {
cout << b[i][j] << " ";
}
cout << '\n';
}
}
int construct(vector<vector<int>> a) {
int n = a.size();
res = vector<vector<int>>(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
p[i] = i;
v[i].clear(); vv[i].clear();
v[i].push_back(i);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1) {
Merge(i, j, 0);
}
if (a[i][j] == 3) return 0;
}
}
for (int i = 0; i < n; i++) {
if (Find(i) == i) {
for (int w : v[i]) {
for (int ww : v[i]) {
if (a[w][ww] != 1) return 0;
}
}
vv[i].push_back(i);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 2) {
Merge(i, j, 1);
}
}
}
for (int i = 0; i < n; i++) {
if (Find(i) == i) {
for (int j = 0; j < vv[i].size() - 1; j++) {
res[vv[i][j]][vv[i][j + 1]] = res[vv[i][j + 1]][vv[i][j]] = 1;
}
if (vv[i].size() != 1) res[vv[i][0]][vv[i].back()] = res[vv[i].back()][vv[i][0]] = 1;
if (vv[i].size() == 2) return 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] && Find(i) != Find(j)) return 0;
if (a[i][j] == 0 && Find(i) == Find(j)) return 0;
}
}
build(res);
return 1;
}
int main() {
construct({{1, 2}, {2, 1}});
}