#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct UFDS {
vector<int> link, siz;
UFDS(int n) : link(n), siz(n, 1) { iota(link.begin(), link.end(), 0); } //each element as its own subset, with size 1
int findRoot(int x) {
if(x == link[x]) return x;
return link[x] = findRoot(link[x]);
/*
A shorter way of writing the following is:
return x == link[x] ? x : link[x] = findRoot(link[x]);
*/
}
bool same(int x, int y) { return findRoot(x) == findRoot(y); }
bool unite(int x, int y) {
x = findRoot(x);
y = findRoot(y);
if (x == y)
return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
link[y] = x;
return true;
}
int size(int x) { return siz[findRoot(x)]; }
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
std::vector<std::vector<int>> answer;
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
UFDS ds(n);
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
if (p[i][j] == 2) ds.unite(i, j);
else{
if (ds.same(i, j)) return 0;
}
}
}
vector<vector<int>> parents(n);
for (int i = 0; i < n; ++i) parents[ds.findRoot(i)].push_back(i);
for (auto item: parents){
int u = item.size();
for (int i = 0; i < u-1; i++){
answer[item[i]][item[i+1]] = 1;
answer[item[i+1]][item[i]] = 1;
}
// if (u > 1){
// answer[item[0]][item[u-1]] = 1;
// answer[item[u-1]][item[0]] = 1;
// }
}
build(answer);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
2 |
Halted |
0 ms |
0 KB |
- |