# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
829609 | faruk | 슈퍼트리 잇기 (IOI20_supertrees) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "supertrees.h"
#include <iostream>
#include <vector>
#include <algorithm>
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n;
bool check_components(vector<vector<int> > p) {
vector<int> comp(n, -1);
for (int i = 0; i < n; i++) {
if (p[i][i] != 1)
return false;
if (comp[i] == -1) {
for (int j = 0; j < n; j++) {
if (p[i][j] != 0 and comp[j] != -1)
return false;
if (p[i][j] != 0)
comp[j] = i;
}
}
else {
for (int j = 0; j < n; j++)
if (p[i][j] != 0 and comp[j] != comp[i])
return false;
}
}
return true;
}
int construct(std::vector<std::vector<int>> p) {
n = p.size();
if (check_components(p) == false)
return 0;
vector<bool> visited(n);
vector<vector<int> > out(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
if (visited[i])
continue;
vector<int> mine;
for (int j = 0; j < n; j++)
if (p[i][j] != 0)
mine.push_back(j), visited[j] = true;
if (mine.size() == 1)
continue;
vector<int> all2, everyone_else;
for (int a : mine) {
bool has1 = false;
for (int j : mine)
if (p[a][j] == 1 and j != a)
has1 = true;
if (has1)
everyone_else.push_back(a);
else
all2.push_back(a);
}
for (int a : everyone_else) {
for (int b : all2)
if (p[a][b] != 2)
//return 0;
for (int b : everyone_else)
if (p[a][b] != 1)
//return 0;
}
if (everyone_else.size() == 0) { // cycle
if (all2.size() <= 2)
//return 0;
for (int i = 0; i < all2.size(); i++)
{
int f = all2[i], s = i < (all2.size() - 1) ? all2[i + 1] : all2[0];
out[f][s] = 1, out[s][f] = 1;
}
}
else if (all2.size() == 0) {// line
for (int i = 0; i < everyone_else.size() - 1; i++)
{
int f = everyone_else[i], s = everyone_else[i + 1];
out[f][s] = 1, out[s][f] = 1;
}
}
else {
// made cycle chain
for (int i = 0; i < all2.size() - 1; i++)
{
int f = all2[i], s = all2[i + 1];
out[f][s] = 1, out[s][f] = 1;
}
if (all2.size() == 1)
//return 0;
// made one chain
for (int i = 0; i < everyone_else.size() - 1; i++)
{
int f = everyone_else[i], s = everyone_else[i + 1];
out[f][s] = 1, out[s][f] = 1;
}
int A = everyone_else[0], B = all2[0], C = all2.back();
out[A][B] = 1;
out[B][A] = 1;
out[A][C] = 1;
out[C][A] = 1;
}
}
build(out);
return 1;
}