Submission #524029

#TimeUsernameProblemLanguageResultExecution timeMemory
524029TurkhuuConnecting Supertrees (IOI20_supertrees)C++17
56 / 100
195 ms28444 KiB
#include "supertrees.h" #include <vector> #include <bits/stdc++.h> using namespace std; struct dsu{ int n; vector<int> size, parent; dsu(int _n) : n(_n), size(n, 1), parent(n){ iota(parent.begin(), parent.end(), 0); } int get(int a){ return a == parent[a] ? a : parent[a] = get(parent[a]); } bool unite(int b, int a){ a = get(a); b = get(b); if(a == b){ return 0; } size[b] += size[a]; parent[a] = b; return 1; } }; int construct(vector<vector<int>> a){ int n = a.size(); vector<vector<int>> ans(n, vector<int>(n, 0)); vector<vector<int>> v1(n),v2(n); for(int i = 0; i < n; i++){ for(int j = i + 1; j < n; j++){ if(a[i][j] == 1){ v1[i].push_back(j); v1[j].push_back(i); } else if(a[i][j] == 2){ v2[i].push_back(j); v2[j].push_back(i); } else if(a[i][j] == 3){ return 0; } } } dsu s(n); for(int i = 0; i < n; i++){ if(s.parent[i] == i){ for(int j = 0; j < n; j++){ for(int k : v1[i]){ if(a[k][j] != a[i][j]){ return 0; } ans[i][k] = 1; ans[k][i] = 1; s.unite(i, k); } } } } vector<bool> b(n, false); for(int i = 0; i < n; i++){ if(s.parent[i] != i || b[s.parent[i]]){ continue; } b[i] = true; int cur = i; for(int j : v2[i]){ if(s.parent[j] != j){ if(a[i][s.parent[j]] != 2){ return 0; } continue; } for(int k : v2[j]){ if((a[i][k] != 1 && s.parent[k] == i) || (a[i][k] == 1 && s.parent[k] != i)){ return 0; } } ans[cur][j] = 1; ans[j][cur] = 1; b[j] = true; cur = j; } if(cur == i){ continue; } if(ans[i][cur]){ return 0; } ans[i][cur] = 1; ans[cur][i] = 1; } build(ans); 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...