Submission #1002313

#TimeUsernameProblemLanguageResultExecution timeMemory
1002313toast12Connecting Supertrees (IOI20_supertrees)C++14
19 / 100
136 ms24148 KiB
#include "supertrees.h" #include <vector> #include <map> #include <iostream> 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>> 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++) { // cout << i << ": "; // for (auto x : c[i]) // cout << x << ' '; // cout << '\n'; // } for (int i = 0; i < n; i++) { if (c[i].empty()) continue; if (c[i].size() == 2) return 0; vector<bool> done(n); vector<int> x; for (int j = 0; j < (int)c[i].size(); j++) { if (!done[c[i][j]]) x.push_back(c[i][j]); done[c[i][j]] = true; for (int k = j+1; k < (int)c[i].size(); k++) { if (!p[c[i][j]][c[i][k]]) return 0; } } for (int j = 0; j < (int)x.size()-1; j++) { answer[x[j]][x[j+1]] = 1; answer[x[j+1]][x[j]] = 1; } answer[x[0]][x.back()] = 1; answer[x.back()][x[0]] = 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...