This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "supertrees.h"
//#define DEBUGGING
#ifdef DEBUGGING
#include "../debug.h"
#else
#define debug(x...) void(42)
#endif
using namespace std;
constexpr static int MXN = 1e3 + 5;
struct Node
{
int head, tail, size, nxt;
Node(int i) : head(i), tail(i), size(1), nxt(-1) {}
Node() : Node(-1) {}
};
Node node[MXN];
Node supernode[MXN];
void merge(int a, int b, Node arr[MXN]);
int construct(vector<vector<int>> p)
{
int n = p.size();
vector<vector<int>> answer(n, vector<int>(n));
for (int i = 0; i < n; i++)
node[i] = Node(i);
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (p[i][j] == 3)
return 0;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (p[i][j] == 1 && node[i].head != node[j].head)
{
merge(node[i].head, node[j].head, node);
answer[i][j] = answer[j][i] = 1;
}
}
}
for (int i = 0; i < n; i++)
supernode[i] = Node(i);
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (p[i][j] == 2 && supernode[node[i].head].head != supernode[node[j].head].head)
merge(supernode[node[i].head].head, supernode[node[j].head].head, supernode);
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if ((p[i][j] == 0 && supernode[node[i].head].head == supernode[node[j].head].head) || (p[i][j] == 1 && node[i].head != node[j].head) || (p[i][j] == 2 && (node[i].head == node[j].head || supernode[node[i].head].head != supernode[node[j].head].head)))
return 0;
for (int i = 0; i < n; i++)
{
if (i != node[i].head || i != supernode[i].head || supernode[i].size == 1)
continue;
if (supernode[i].size == 2)
return 0;
for (int j = i; supernode[j].nxt != -1; j = supernode[j].nxt)
answer[j][supernode[j].nxt] = answer[supernode[j].nxt][j] = 1;
answer[i][supernode[i].tail] = answer[supernode[i].tail][i] = 1;
}
build(answer);
return 1;
}
void merge(int a, int b, Node arr[MXN])
{
if (arr[a].size < arr[b].size) swap(a, b);
arr[a].size += arr[b].size;
arr[arr[a].tail].nxt = b;
arr[a].tail = arr[b].tail;
for (;b!=-1;b=arr[b].nxt)
arr[b].head = a;
}
# | 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... |