Submission #578033

#TimeUsernameProblemLanguageResultExecution timeMemory
5780331neConnecting Supertrees (IOI20_supertrees)C++14
11 / 100
1089 ms22296 KiB
#include "supertrees.h" #include <vector> #include <cassert> #include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <bits/stdc++.h> using namespace std; struct DSU{ vector<int>parent; vector<int>sz; vector<vector<int>>comp; void build(int n){ parent.resize(n); sz.resize(n); comp.resize(n); for (int i = 0;i<n;++i){ parent[i] = i; sz[i] = 1; comp[i].push_back(i); } } int findsets(int v){ if (v == parent[v])return v; parent[v] = findsets(parent[v]); return parent[v]; } bool unionset(int a,int b){ int u = findsets(a); int v = findsets(b); if (u == v)return false; if (sz[u] < sz[v])swap(u,v); parent[v] = u; sz[u]+=sz[v]; sz[v] = 0; while(!comp[v].empty()){ comp[u].push_back(comp[v].back()); comp[v].pop_back(); } return true; }; }; int construct(std::vector<std::vector<int>> p) { int n = p.size(); std::vector<std::vector<int>> answer(n,vector<int>(n,0)); for (int i = 0;i<n;++i){ for (int j = 0;j<n;++j){ if (p[i][j] == 3)return 0; } } DSU st; st.build(n); for (int i = 0;i<n;++i){ for (int j = i + 1;j<n;++j){ int u = st.findsets(i); int v = st.findsets(j); if (p[i][j]==1 && st.unionset(i,j)){ answer[u][v] = true; answer[v][u] = true; } } } vector<vector<bool>>pre(n,vector<bool>(n,false)); for (int i = 0;i<n;++i){ for (int j = i + 1;j<n;++j){ if (st.findsets(i) == st.findsets(j)){ p[i][j]--; p[j][i]--; pre[i][j] = true; pre[j][i] = true; } } } for (int i = 0;i<n;++i){ for (int j = i + 1;j<n;++j){ if (p[i][j] == 2 && !answer[i][j]){ answer[i][j] = true; answer[j][i] = true; int y = st.findsets(i); int q = st.findsets(j); vector<vector<bool>>visited(n,vector<bool>(n,false)); for (auto x:st.comp[y]){ visited[x][j] = true; visited[j][x] = true; } for (auto x:st.comp[q]){ visited[i][x]=true; visited[x][i]=true; } for (int qqq = 0;qqq<n;++qqq){ for (int qq = 0;qq<n;++qq){ p[qqq][qq]-=visited[qqq][qq]; } } st.unionset(i,j); } } } for (int i = 0;i<n;++i){ for (int x = i + 1;x < n ; ++x){ if (p[i][x] && !answer[i][x] && st.findsets(i) == st.findsets(x)){ answer[i][x] = true; answer[x][i] = true; for (auto xx:st.comp[st.findsets(i)]){ for (auto y:st.comp[st.findsets(i)]){ if (xx == y || pre[xx][y])continue; p[xx][y]--; } } } } } for (int i = 0;i<n;++i){ for (int j = 0;j < n;++j){ //cout<<p[i][j]<<" "; if (i == j)continue; if (p[i][j] != 0){ //cout<<i<<" "<<j<<" "<<p[i][j]<<'\n'; return 0; } } //cout<<'\n'; } 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...