Submission #1002655

#TimeUsernameProblemLanguageResultExecution timeMemory
1002655toast12Connecting Supertrees (IOI20_supertrees)C++14
11 / 100
117 ms24164 KiB
#include "supertrees.h" #include <vector> #include <map> #include <iostream> #include <set> using namespace std; #define sz(x) (int)x.size() 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>> c(n); DSU d(n+1); for (int i = 0; i < n; 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); int x = d.find(i); c[x].push_back(j); c[x].push_back(i); } } } } for (int i = 0; i < n; i++) { if (sz(c[i]) <= 1) continue; vector<bool> done(n); vector<int> x; for (int j = 0; j < sz(c[i]); j++) { if (!done[c[i][j]]) x.push_back(c[i][j]); done[c[i][j]] = true; } vector<int> one, two; for (int j = 0; j < sz(x); j++) { int r = x[j]; bool o = false; for (int k = 0; k < n; k++) { if (k == r) continue; if (p[r][k] == 1) { o = true; break; } } if (!o) two.push_back(x[j]); else one.push_back(x[j]); } DSU dsu(n+1); set<int> roots; if (!one.empty()) { for (auto o : one) { for (int j = 0; j < n; j++) { if (j == o) continue; if (p[o][j] == 1 && !dsu.same(o, j)) { answer[o][j] = 1; answer[j][o] = 1; dsu.unite(j, o); roots.insert(dsu.find(j)); } } } } if (!two.empty()) { for (auto x : roots) two.push_back(x); for (int j = 0; j < sz(two)-1; j++) { answer[two[j]][two[j+1]] = 1; answer[two[j+1]][two[j]] = 1; dsu.unite(two[j], two[j+1]); } answer[two.back()][two[0]] = 1; answer[two[0]][two.back()] = 1; } } for (int i = 0; i < n; i++) { answer[i][i] = 0; } 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...