이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(x) cout << #x << " " << x << endl;
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define pb push_back
#define ld long double
#define ll long long
void build(std::vector<std::vector<int>> p);
const int N = 2e5 + 10, M = 1e3 + 10;
// vector<int> cycles[N];
int pr[N];
map<int, vector<int>> mp1, mp2;
struct dsu {
vector<int> par, sz;
void init(int n) {
par.resize(n + 1);
sz.resize(n + 1);
for(int i = 0; i < n; i++) {
par[i] = i, sz[i] = 1;
}
}
int find(int a) {
if(a != par[a]) {
par[a] = find(par[a]);
}
return par[a];
}
int same(int a, int b) {
if(find(a) == find(b)) {
return 1;
} else {
return 0;
}
}
int merge(int a, int b) {
a = find(a), b = find(b);
if(a == b) {
return 0;
}
if(sz[a] < sz[b]) {
swap(a, b);
}
par[b] = a, sz[a] += sz[b];
return 1;
}
} d1, d2;
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
d1.init(n + 1), d2.init(n + 1);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(p[i][j] == 3) {
return 0;
}
}
}
vector<vector<int>> answ(n, vector<int>(n, 0));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(p[i][j] == 1) {
d1.merge(i, j);
}
}
}
for(int i = 0; i < n; i++) {
int hayr = d1.find(i);
pr[i] = hayr;
if(hayr != i) {
mp1[hayr].push_back(i);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(p[i][j] != p[pr[i]][pr[j]]) {
return 0;
}
}
}
vector<int> cycle;
for(int i = 0; i < n; i++) {
if(pr[i] == i) {
cycle.push_back(i);
}
}
for(auto i: cycle) {
for(auto j: cycle) {
if(p[i][j] == 2) {
d2.merge(i, j);
}
}
}
for(auto i: cycle) {
int hayr = d2.find(i);
pr[i] = hayr;
if(hayr != i) {
mp2[hayr].push_back(i);
}
}
for(auto i: cycle) {
for(auto j: cycle) {
if(i != j && p[i][j] != 2 && d2.same(i, j)) {
return 0;
}
}
}
for(auto i: mp1) {
for(auto j: i.second) {
answ[i.first][j] = answ[j][i.first] = 1;
}
}
for(auto i: mp2) {
if(sz(i.second) == 1) {
return 0;
}
int curr = i.first;
for(auto j: i.second) {
answ[curr][j] = answ[j][curr] = 1;
curr = j;
}
answ[curr][i.first] = answ[i.first][curr] = 1;
}
build(answ);
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... |