# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
300743 | IgorI | Connecting Supertrees (IOI20_supertrees) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
void build(vector<vector<int> > b) ;
void build(vector<vector<int> > b) ;
int Root(int x, vector<int> &root)
{
if (x == root[x]) return x;
return root[x] = Root(root[x], root);
}
void Merge(int v, int u, vector<int> &root, vector<int> &sz)
{
v = Root(v, root), u = Root(u, root);
if (v == u) return;
if (sz[v] > sz[u]) swap(v, u);
sz[u] += sz[v];
root[v] = u;
}
int construct(vector<vector<int> > p)
{
int n = p.size();
vector<int> root(n), sz(n);
for (int i = 0; i < n; i++) root[i] = i, sz[i] = 1;
int t = 0;
forn(i, n)
{
forn(j, n)
{
if (p[i][j] == 3)
{
return 0;
}
if (p[i][j])
{
Merge(i, j, root, sz);
}
}
}
forn(i, n)
{
forn(j, n)
{
if (Root(i, root) == Root(j, root) && p[i][j] == 0)
{
return 0;
}
}
}
vector<vector<int> > b(n, vector<int>(n));
forn(i, n)
{
vector<int> L;
forn(j, n)
{
if (Root(j, root) == i)
{
L.push_back(j);
}
}
vector<int> root2(L.size()), sz2(L.size());
iota(root2.begin(), root2.end(), 0);
fill(sz2.begin(), sz2.end(), 1);
for (int a = 0; a < L.size(); a++)
{
for (int b = 0; b < L.size(); b++)
{
if (p[L[a]][L[b]] == 1)
{
Merge(a, b, root2, sz2);
}
}
}
for (int a = 0; a < L.size(); a++)
{
for (int b = 0; b < L.size(); b++)
{
if (p[L[a]][L[b]] == 2 && Root(a, root2) == Root(b, root2))
return 0;
}
}
vector<vector<int> > z(L.size());
for (int a = 0; a < L.size(); a++)
{
z[Root(a, root2)].push_back(L[a]);
}
vector<int> q;
for (int j = 0; j < z.size(); j++)
{
if (z[j].size()) q.push_back(z[j][0]);
for (int e = 0; e + 1 < z[j].size(); e++)
{
b[z[j][e]][z[j][e + 1]] = 1;
b[z[j][e + 1]][z[j][e]] = 1;
}
}
if (q.size() == 2)
{
return 0;
}
if (q.size() > 2)
for (int i = 0; i < q.size(); i++)
{
b[q[i]][q[(i + 1) % q.size()]] = 1;
b[q[(i + 1) % q.size()]][q[i]] = 1;
}
}
build(b);
return 1;
}
int main()
{
int n;
cin >> n;
vector<vector<int> > p(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> p[i][j];
construct(p);
}
void build(vector<vector<int> > b)
{
forn(i, b.size())
{
forn(j, b.size())
{
cout << b[i][j] << " ";
}
cout << "\n";
}
}