# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
434553 | frodakcin | 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 "supertrees.h"
#include <algorithm>
#include <vector>
struct DSU
{
public:
std::vector<int> p, r;
DSU(int N): p(N, -1), r(N, 0) {}
int find(int n) {return p[n] == -1 ? n : p[n] = find(p[n]);}
bool merge(int u, int v)
{
u=find(u), v=find(v);
if(u==v) return v;
if(r[u] < r[v]) std::swap(u, v);
p[v]=u, r[u]+=r[u]==r[v], r[v]=-1;
return 1;
}
};
int construct(std::vector<std::vector<int>> p)
{
int N = p.size();
std::vector<std::vector<int>> answer(N, std::vector<int>(N, 0));
DSU dsu1(N);
for (int i = 0; i < N; i++)
for(int j=i+1;j<N;++j)
if(p[i][j]==1)
{
if(dsu1.merge(i, j))
{
if(p[i] != p[j]) // must have identical adj lists
return 0;
answer[i][j]=1; // this makes a tree among all 1-con components
answer[j][i]=1;
}
}
std::vector<int> head;
for(int i=0;i<N;++i) if(dsu1.p[i]==-1) head.push_back(i);
for(int sz: {2,3}) // handle 2/3 case
{
std::vector<std::vector<int>> group(N, std::vector<int>());
DSU dsu2(N);
for(int i=0;i<(int)head.size();++i)
for(int j=i+1;j<(int)head.size();++j)
if(p[head[i]][head[j]]==sz)
dsu2.merge(head[i], head[j]);
for(int i=0;i<N;++i)
group[dsu2.find(i)].push_back(i);
for(int i=0;i<N;++i)
if(group[i].size() >= 2)
{
if(group[i].size() <= 2) return 0;
if(sz == 3 && group[i].size() <= 4) return 0;
for(int j=0;j<(int)group[i].size();++j) // create a simple cycle
{
int u=group[i][j], v=group[i][(j+1)%group[i].size()];
answer[u][v]=answer[v][u]=1;
}
if(sz == 3)
answer[group[i][0]][group[i][2]]=answer[group[i][2]][group[i][0]]=1;
printf("HERE 1\n");
//identicality test
for(int j=0;j<(int)group[i].size();++j)
for(int k=j+1;k<(int)group[i].size();++k)
if(p[group[i][j]][group[i][k]] != sz)
return 0;
printf("HERE 2\n");
}
}
build(answer);
return 1;
}