이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "supertrees.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 1e3 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
vector <int> s[N];
int ans[N][N];
int a[N][N];
int c[N];
int n,comp;
bool vis[N];
int solve() {
for (int i = 0; i < n; i++) if (!vis[i]) {
queue <int> q({i});
vis[i] = true;
while (q.size()) {
int u = q.front(); q.pop();
s[comp].push_back(u);
c[u] = comp;
for (int v = 0; v < n; v++) if (a[u][v] > 0) {
if (!vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
comp++;
}
for (int i = 0; i < comp; i++) {
for (int j : s[i])
for (int k = 0; k < n; k++) {
if (c[j] == c[k] && a[j][k] == 0)
return 0;
if (c[j] != c[k] && a[j][k] > 0)
return 0;
}
vector <int> tree, cir;
int r = s[i][0];
for (int j : s[i])
for (int k : s[i])
if (j != k && a[j][k] == 1)
r = j;
for (int j : s[i])
if (a[r][j] == 1)
tree.push_back(j);
for (int j : tree)
for (int k : tree)
if (a[j][k] != 1)
return 0;
for (int i = 0; i + 1 < (int) tree.size(); i++) {
int u = tree[i];
int v = tree[i + 1];
ans[u][v] = ans[v][u] = 1;
}
int cur = 0;
for (int j : s[i]) if (a[r][j] != 1) {
if (cur > 0 && a[r][j] != cur)
return 0;
cur = a[r][j];
cir.push_back(j);
}
for (int j : cir)
for (int k : s[i])
if (j != k && a[j][k] != cur)
return 0;
cir.push_back(r);
if (cir.size() == 2)
return 0;
if (cir.size() == 1)
continue;
for (int i = 0; i < (int) cir.size(); i++) {
int u = cir[i];
int v = cir[(i + 1) % cir.size()];
ans[u][v] = true;
ans[v][u] = true;
}
if (cur == 3) {
if (cir.size() == 3)
return 0;
int u = cir[0];
int v = cir[2];
ans[u][v] = ans[v][u] = 1;
}
}
return 1;
}
int construct(vector<vector<int>> p) {
n = p.size();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = p[i][j];
int res = solve();
if (!res)
return 0;
vector<vector<int>> answer;
for (int i = 0; i < n; i++) {
vector<int> row;
for (int j = 0; j < n; j++)
row.push_back(ans[i][j]);
answer.push_back(row);
}
build(answer);
return res;
}
//#include "grader.cpp"
# | 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... |