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 "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
vector<int>g[1005], v[1005], v2[1005];
int sz[1005], par[1005];
int parent(int v)
{
while(v!=par[v]) v=par[v];
return v;
}
bool merge(int u, int v)
{
u=parent(u);
v=parent(v);
if(u==v) return 0;
if(sz[u]<sz[v])
sz[v]+=sz[u], par[u]=v;
else sz[u]+=sz[v], par[v]=u;
return 1;
}
bool check(int i, int j)
{
set<int>s1, s2;
s1.insert(i);
s2.insert(j);
for(int e : v[i])
s1.insert(e);
for(int e : v[j])
s2.insert(e);
return (s1==s2);
}
bool check2(int i, int j)
{
set<int>s1, s2;
s1.insert(i);
s2.insert(j);
for(int e : v2[i])
s1.insert(e);
for(int e : v2[j])
s2.insert(e);
return (s1==s2);
}
int construct(vector<vector<int>> p)
{
int n = p.size();
for(int i=0; i<=n; i++)
sz[i]=1, par[i]=i;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(p[i][j]==1)
v[i].pb(j), v[j].pb(i);
if(p[i][j])
v2[i].pb(j), v2[j].pb(i);
}
}
for(int i=0; i<n; i++)
{
for(int u : v[i])
{
if(i<u and !check(i, u))
return 0;
}
}
for(int i=0; i<n; i++)
{
for(int u : v2[i])
{
if(i<u and !check(i, u))
return 0;
}
}
for(int i=0; i<n; i++)
{
if(v[i].size() and merge(v[i][0], i))
{
g[i].pb(v[i][0]);
g[v[i][0]].pb(i);
for(int j=1; j<(int)v[i].size(); j++)
{
g[v[i][j]].pb(v[i][j-1]);
g[v[i][j-1]].pb(v[i][j]);
merge(v[i][j], v[i][j-1]);
}
}
}
for(int i=0; i<n; i++)
{
int lst=-1;
if(v2[i].size() and merge(v2[i][0], i))
{
g[i].pb(v2[i][0]);
g[v2[i][0]].pb(i);
lst=v2[i][0];
for(int j=1; j<(int)v2[i].size(); j++)
{
if(merge(v2[i][j], lst))
{
g[v2[i][j]].pb(lst);
g[lst].pb(v2[i][j]);
lst=v2[i][j];
}
}
g[i].pb(lst);
g[lst].pb(i);
}
}
vector<vector<int>>ans(n, vector<int>(n, 0));
for(int i=0; i<n; i++)
{
for(int u : g[i])
ans[i][u]=1, ans[u][i]=1;
}
build(ans);
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... |