Submission #568969

#TimeUsernameProblemLanguageResultExecution timeMemory
568969Stickfish슈퍼트리 잇기 (IOI20_supertrees)C++17
100 / 100
195 ms24028 KiB
#include "supertrees.h" #include <vector> #include <iostream> using namespace std; struct dsu { void resize(int n) { par.resize(n); sz.assign(n, 1); for (int i = 0; i < n; ++i) par[i] = i; } int gst(int i) { if (par[i] == i) return i; return par[i] = gst(par[i]); } void unite(int i, int j) { i = gst(i); j = gst(j); if (i == j) return; if (sz[i] < sz[j]) swap(i, j); par[j] = i; sz[i] += sz[j]; } private: vector<int> par; vector<int> sz; }; int construct(vector<vector<int>> p) { int n = p.size(); dsu suall; suall.resize(n); dsu su1; su1.resize(n); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (p[i][j] > 0) suall.unite(i, j); if (p[i][j] == 1) su1.unite(i, j); if (p[i][j] == 3) return 0; } } for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (p[i][j] == 0 && suall.gst(i) == suall.gst(j)) return 0; if (p[i][j] == 2 && su1.gst(i) == su1.gst(j)) return 0; } } //for (int i = 0; i < n; ++i) //cout << suall.gst(i) << ' '; //cout << endl; //for (int i = 0; i < n; ++i) //cout << su1.gst(i) << ' '; //cout << endl; vector<vector<int>> ans(n, vector<int>(n, 0)); for (int i = 0; i < n; ++i) { if (suall.gst(i) == i) { vector<int> comp; for (int j = 0; j < n; ++j) { if (suall.gst(j) == i) comp.push_back(j); } vector<int> cycle; for (int j : comp) { if (su1.gst(j) == j) cycle.push_back(j); else ans[j][su1.gst(j)] = ans[su1.gst(j)][j] = 1; } if (cycle.size() == 2) { return 0; } if (cycle.size() > 1) { for (int t = 0; t < cycle.size(); ++t) { int v = cycle[t]; int u = cycle[(t + 1) % cycle.size()]; ans[u][v] = ans[v][u] = 1; } } } } build(ans); return 1; }

Compilation message (stderr)

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