Submission #524050

#TimeUsernameProblemLanguageResultExecution timeMemory
524050TurkhuuConnecting Supertrees (IOI20_supertrees)C++17
0 / 100
1 ms204 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>> g(n); 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; g[i].push_back(k); g[k].push_back(i); 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; int cnt = 1; 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; g[cur].push_back(j); g[j].push_back(cur); b[j] = true; cur = j; cnt += 1; } if(cnt == 1){ continue; } if(cnt == 2){ return 0; } ans[i][cur] = 1; ans[cur][i] = 1; g[i].push_back(cur); g[cur].push_back(i); } for(int i = 0; i < n; i++){ int cnt = s.parent[i] == i ? (int)v1[i].size() + 2 : 1; if((int)g[i].size() != cnt){ return 0; } } 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...