이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <unordered_map>
#include "supertrees.h"
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
int root(int a, vector<int> & parent) {
if (a == parent[a])
return a;
return parent[a] = root(parent[a], parent);
}
void merge(int i, int j, vector<int> & parent, vector<int> & rnk, int & numCCs) {
int root_i = root(i, parent), root_j = root(j, parent);
if (root_i == root_j) return;
numCCs--;
if (rnk[i] != rnk[j]) {
if (rnk[i] > rnk[j]) parent[j] = parent[i];
else parent[i] = parent[j];
} else {
parent[i] = parent[j];
rnk[j]++;
}
}
int construct (vvi p) {
int N = (int) p.size(), numCCs = N;
vector<int> parent(N), rnk(N, 0);
for (int i = 0; i < N; i++)
parent[i] = i;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (p[i][j]) {
if (p[i][j] == 3) return 0;
merge(i, j, parent, rnk, numCCs);
} else if (root(i, parent) == root(j, parent))
return 0;
}
}
unordered_map<int, int> roots;
int c = 0;
for (int i = 0; i < N; i++) {
if (parent[i] == i)
roots[i] = c++;
}
vvi CCs(numCCs, vi());
for (int i = 0; i < N; i++)
CCs[roots[root(i, parent)]].push_back(i);
vi allTwos, rest;
vvi result(N, vi(N, 0));
bool twos;
for (int i = 0; i < numCCs; i++) {
allTwos.clear();
rest.clear();
for (int j = 0; j < (int) CCs[i].size(); j++) {
twos = true;
for (int k = 0; k < (int) CCs[i].size(); k++) {
if (p[CCs[i][j]][CCs[i][k]] == 1) {
twos = false;
break;
}
}
if (twos) allTwos.push_back(CCs[i][j]);
else rest.push_back(CCs[i][j]);
}
for (int i = 0; i < (int) allTwos.size()-1; i++)
result[allTwos[i]][allTwos[i+1]] = result[allTwos[i+1]][allTwos[i]] = 1;
for (int i = 0; i < (int) rest.size()-1; i++)
result[rest[i]][rest[i+1]] = result[rest[i+1]][rest[i]] = 1;
if (allTwos.size() && (int) allTwos.size() <= 2) return 0;
if (rest.empty())
result[allTwos[0]][*allTwos.rbegin()] = result[*allTwos.rbegin()][allTwos[0]] = 1;
else if (allTwos.size())
result[allTwos[0]][rest[0]] = result[rest[0]][allTwos[0]] = result[rest[0]][*allTwos.rbegin()] = result[*allTwos.rbegin()][rest[0]] = 1;
}
build(result);
return 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... |