# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
363297 | mathking1021 | Connecting Supertrees (IOI20_supertrees) | C++17 | 279 ms | 24172 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 <vector>
using namespace std;
int cp[1005];
int cp2[1005];
vector<int> ve[1005];
vector<int> ve2[1005];
vector<int> ve3;
int fnd(int p)
{
if(cp[p] == p) return p;
return cp[p] = fnd(cp[p]);
}
void uni(int p, int q)
{
if(fnd(p) == fnd(q)) return;
cp[fnd(p)] = fnd(q);
}
int fnd2(int p)
{
if(cp2[p] == p) return p;
return cp2[p] = fnd2(cp2[p]);
}
void uni2(int p, int q)
{
if(fnd2(p) == fnd2(q)) return;
cp2[fnd2(p)] = fnd2(q);
}
int construct(vector<vector<int> > p) {
int n = p.size();
vector<vector<int> > answer;
for(int i = 0; i < n; i++)
{
cp[i] = i;
cp2[i] = i;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(p[i][j]) uni(i, j);
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(!p[i][j] && fnd(i) == fnd(j)) return 0;
}
}
for(int i = 0; i < n; i++)
{
vector<int> row;
for(int j = 0; j < n; j++)
{
row.push_back(0);
}
answer.push_back(row);
}
for(int i = 0; i < n; i++)
{
ve[fnd(i)].push_back(i);
}
for(int i = 0; i < n; i++)
{
if(ve[i].size() == 0) continue;
if(ve[i].size() == 1) continue;
for(int j = 0; j < ve[i].size(); j++)
{
for(int k = j + 1; k < ve[i].size(); k++)
{
if(p[ve[i][j]][ve[i][k]] == 1) uni2(ve[i][j], ve[i][k]);
}
}
for(int j = 0; j < ve[i].size(); j++)
{
ve2[fnd2(ve[i][j])].push_back(ve[i][j]);
}
ve3.clear();
for(int j = 0; j < ve[i].size(); j++)
{
if(ve2[ve[i][j]].size() == 0) continue;
for(int k = 1; k < ve2[ve[i][j]].size(); k++)
{
answer[ve2[ve[i][j]][k]][ve2[ve[i][j]][k - 1]] = 1;
answer[ve2[ve[i][j]][k - 1]][ve2[ve[i][j]][k]] = 1;
}
ve3.push_back(ve[i][j]);
}
if(ve3.size() == 1) continue;
for(int j = 1; j < ve3.size(); j++)
{
answer[ve3[j]][ve3[j - 1]] = 1;
answer[ve3[j - 1]][ve3[j]] = 1;
}
answer[ve3[0]][ve3[ve3.size() - 1]] = 1;
answer[ve3[ve3.size() - 1]][ve3[0]] = 1;
}
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... |