# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1078757 | Faisal_Saqib | Connecting Supertrees (IOI20_supertrees) | C++17 | 1075 ms | 24100 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 "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int N=1e3;
int reach[N][N],tree_case[N][N];
int par[N];
void init(int n)
{
for(int i=0;i<n;i++)
par[i]=i;
}
int get(int x)
{
if(par[x]==x)return x;
return par[x]=get(par[x]);
}
bool merge(int x,int y)
{
x=get(x);
y=get(y);
if(x==y)return 0;
par[y]=x;
return 1;
}
vector<int> ma[N];
int head;
void dfs(int x)
{
reach[head][x]=1;
for(auto y:ma[x])
if(!reach[head][y])
dfs(y);
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
init(n);
bool two=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
two|=(p[i][j]==2);
tree_case[i][j]=reach[i][j]=0;
}
}
if(two)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(p[i][j]==2)
{
merge(i,j);
}
}
}
for(int i=0;i<n;i++)
{
if(get(i)==i)
{
vector<int> component;
for(int j=0;j<n;j++)
{
if(get(j)==i)
{
component.push_back(j);
}
}
component.push_back(component[0]);
for(int i=1;i<component.size();i++)
{
tree_case[component[i-1]][component[i]]=1;
tree_case[component[i]][component[i-1]]=1;
}
}
}
}
else
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(p[i][j]==1)
{
tree_case[i][j]=tree_case[j][i]=merge(i,j);
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
ma[i].push_back(j);
ma[j].push_back(i);
}
}
vector<vector<int>> answer(n,vector<int>(n,0));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
answer[i][j]=tree_case[i][j];
reach[i][j]=0;
}
head=i;
dfs(i);
for(int j=0;j<n;j++)
{
p[i][j]-=(p[i][j]==2);// make 2 to 1
if(reach[i][j]!=p[i][j])
{
return 0;
}
}
}
build(answer);
return 1;
}
Compilation message (stderr)
# | 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... |