Submission #366986

#TimeUsernameProblemLanguageResultExecution timeMemory
366986PurpleCrayonConnecting Supertrees (IOI20_supertrees)C++17
21 / 100
262 ms22252 KiB
#include "supertrees.h" #include <bits/stdc++.h> using namespace std; #define ar array #define sz(v) int(v.size()) int n; struct DSU { vector<int> p, sz; void init(int n){ sz.assign(n, 1); p.resize(n); iota(p.begin(), p.end(), 0); } int find_set(int v){ return v==p[v]?v:p[v]=find_set(p[v]); } bool union_sets(int a, int b){ if ((a=find_set(a))==(b=find_set(b))) return 0; if (sz[a] < sz[b]) swap(a, b); p[b]=a, sz[a]+=sz[b], sz[b]=0; return 1; } } d; int construct(vector<vector<int>> g) { n = sz(g); vector<vector<int>> ans(n, vector<int>(n)); bool done=0, bad=0; [&](){ //handle case with a 3 if (done || bad) return; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (g[i][j] == 3) { bad=1; return; } }(); [&](){ //only 1's (tree) if (done || bad) return; for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j] != 1) return; for (int i = 1; i < n; i++) ans[i][0] = ans[0][i] = 1; build(ans), done=1; }(); [&](){ //0, 1's (just deal with separate comps) if (done || bad) return; for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j] != 0 && g[i][j] != 1) return; d.init(n); for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j]) if (d.union_sets(i, j)) ans[i][j] = ans[j][i] = 1; for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if ((d.find_set(i) == d.find_set(j))^(g[i][j])) return; build(ans), done=1; }(); return !bad && done; }
#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...