Submission #1002269

#TimeUsernameProblemLanguageResultExecution timeMemory
1002269toast12Connecting Supertrees (IOI20_supertrees)C++14
11 / 100
146 ms24048 KiB
#include "supertrees.h" #include <vector> #include <map> using namespace std; struct DSU { int n; vector<int> link, size; DSU(int sz) { n = sz; link.resize(sz); size.resize(sz); for (int i = 0; i < n; i++) { link[i] = i; size[i] = 1; } } int find(int x) { while (x != link[x]) x = link[x]; return x; } bool same(int a, int b) { return find(a) == find(b); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (size[a] < size[b]) swap(a, b); link[b] = a; size[a] += size[b]; } }; int construct(std::vector<vector<int>> p) { int n = p.size(); vector<vector<int>> answer(n, vector<int>(n)); vector<vector<int>> components(n); DSU d(n+1); for (int i = 0; i < n; i++) { components[i].push_back(i); for (int j = 0; j < n; j++) { if (i == j) continue; if (p[i][j]) { if (!d.same(i, j)) { d.unite(i, j); components[d.find(i)].push_back(j); } } } } for (int i = 0; i < n; i++) { if (components[i].empty()) continue; for (int j = 0; j < (int)components[i].size(); j++) { for (int k = j+1; k < (int)components[i].size(); k++) { if (!p[j][k]) return -1; } } for (int j = 0; j < (int)components[i].size()-1; j++) { answer[components[i][j]][components[i][j+1]] = 1; answer[components[i][j+1]][components[i][j]] = 1; } } build(answer); return 1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...