Submission #578065

#TimeUsernameProblemLanguageResultExecution timeMemory
5780651neConnecting Supertrees (IOI20_supertrees)C++14
100 / 100
204 ms22108 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<int>cyc(n,0); for (int i = 0;i<n;++i){ vector<int>cycle; cycle.push_back(st.findsets(i)); for (int j = 0;j<n;++j){ if (p[i][j] == 2){ if (st.findsets(i) == st.findsets(j))continue; cycle.push_back(st.findsets(j)); st.unionset(i,j); } } if (cycle.size()==2)return 0; if (cycle.size()==1)continue; cyc[st.findsets(i)]++; for (int j = 0;j<cycle.size();++j){ answer[cycle[j]][cycle[(j + 1)%(int)cycle.size()]] = true; answer[cycle[(j + 1)%(int)cycle.size()]][cycle[j]] = true; } } for (int i = 0;i<n;++i){ for (int j = 0;j<n;++j){ if (p[i][j] && st.findsets(i)!=st.findsets(j))return 0; if (!p[i][j] && st.findsets(i)==st.findsets(j))return 0; } } for (int i = 0;i<n;++i){ if (cyc[st.findsets(i)] > 1)return 0; } build(answer); return 1; }

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:78:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for (int j = 0;j<cycle.size();++j){
      |                  ~^~~~~~~~~~~~~
#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...