# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1185020 | islam_2010 | Connecting Supertrees (IOI20_supertrees) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
vector<int> comp(n, -1);
int c = 0;
void dfs(int n, vector<vector<int>> p, int node){
comp[node] = c;
for(int i = 0; i < n; i++){
if(p[node][i]==1 && comp[i]==-1){
dfs(n, p, node);
}
}
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> b(n, vector<int>(n, 0));
for(int i = 0; i < n; i++){
if(comp[i]==-1){
dfs(n, p, i);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if (p[i][j] == 1 && comp[i] != comp[j]) {
return 0;
}
if (p[i][j] == 0 && comp[i] == comp[j]) {
return 0;
}
}
}
for (int c = 0; c < cid; c++) {
vector<int> g;
for (int i = 0; i < n; i++) {
if (comp[i] == c) g.push_back(i);
}
for (int i = 0; i + 1 < g.size(); i++) {
int u = g[i], v = g[i + 1];
b[u][v] = b[v][u] = 1;
}
}
build(b);
return 1;
}