Submission #433567

#TimeUsernameProblemLanguageResultExecution timeMemory
433567sean617Connecting Supertrees (IOI20_supertrees)C++17
56 / 100
300 ms25064 KiB
#include "supertrees.h" #include <vector> #include <cstring> #define N 1005 using namespace std; int n, cnt, bu[N], bu2[N], gut[N]; bool v[N], co[N][N], icy[N]; vector<int> gu[N], cy[N], cy2, ans, ch[N]; vector<std::vector<int> > answer; int f(int p) { if (bu[p] == p) return p; else return bu[p] = f(bu[p]); } int f2(int p) { if (bu2[p] == p) return p; else return bu2[p] = f2(bu2[p]); } void f_co(int p, int q) { co[p][q] = co[q][p] = 1; } int construct(std::vector<std::vector<int> > p) { int i, j, k, t, t1, t2, la, tcnt; n = p.size(); for (i = 0; i < n; i++) {bu[i] = i; bu2[i] = i;} for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (p[i][j]== 0) continue; if (p[i][j] == 3) return 0; t1 = f(i); t2 = f(j); if (t1 != t2) bu[t1] = t2; } } memset(gut, -1, sizeof(gut)); for (i = 0; i < n; i++) f(i); for (i = 0; i < n; i++) { tcnt = 0; for (j = 0; j < n; j++) { if (i == j) continue; if (bu[i] != bu[j]) continue; if (p[i][j] == 0) return 0; if (p[i][j] == 1) { t1 = f2(i); t2 = f2(j); if (t1 != t2) bu2[t1] = t2; tcnt++; } } if (tcnt == 0) { icy[i] = 1; cy[bu[i]].push_back(i); } } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (bu2[i] != bu2[j]) continue; if (p[i][j] != 1) return 0; } } for (i = 0; i < n; i++) { if (cy[i].empty()) continue; for (j = 0; j < cy[i].size(); j++) { for (k = j + 1; k < cy[i].size(); k++) { t1 = cy[i][j]; t2 = cy[i][k]; if (p[t1][t2] != 2) return 0; } } // int cysz = cy[i].size(); // for (j = 0; j < cysz; j++) { // t1 = cy[i][j]; // t2 = cy[i][(j + 1) % cysz]; // f_co(t1, t2); // } } for (i = 0; i < n; i++) { if (icy[i]) continue; for (j = i + 1; j < n; j++) { if (bu[i] != bu[j]) continue; if (bu2[i] == bu2[j] && p[i][j] != 1 || bu2[i] != bu2[j] && p[i][j] != 2) return 0; } if (gut[bu2[i]] == -1) { cy[bu[i]].push_back(i); } else { f_co(gut[bu2[i]], i); } gut[bu2[i]] = i; } for (i = 0; i < n; i++) { if (cy[i].empty() || cy[i].size() == 1) continue; int cysz = cy[i].size(); for (j = 0; j < cysz; j++) { t1 = cy[i][j]; t2 = cy[i][(j + 1) % cysz]; f_co(t1, t2); } } /*for (i =0; i < n; i++) { t = f(i); gu[t].push_back(i); } for (i = 0; i < n; i++) { t = f(i); if (gu[t].size() == 1) continue; for (j = 0; j < gu[t].size(); j++) { if (gu[t][j] == i) continue; if (p[i][gu[t][j]] != 2) break; } if (j == gu[t].size()) { v[i] = 1; cy[t].push_back(i); } } for (i = 0; i < n; i++) { if (v[i]) continue; t = f(i); if (gu[t].size() == 1) continue; for (j = 0; j < gu[t].size(); j++) { if (gu[t][j] == i) continue; if (p[i][gu[t][j]] == 1 && v[gu[t][j]]) break; } if (j == gu[t].size()) { v[i] = 1; cy[t].push_back(i); cy2.push_back(i); } } for (i = 0; i < n; i++) { int sz = cy[i].size(); if (sz >= 2) { for (j = 0; j < sz; j++) { f_co(cy[i][j], cy[i][(j + 1) % sz]); } } } for (i = 0; i < cy2.size(); i++) { la = t = cy2[i]; t2 = f(t); ch[i].push_back(la); for (j = 0; j < gu[t2].size(); j++) { if (gu[t2][j] != t && p[t][gu[t2][j]] == 1) { f_co(la, gu[t2][j]); la = gu[t2][j]; ch[i].push_back(la); } } } for (i = 0; i < cy2.size(); i++) { for (j = 0; j < ch[i].size(); j++) { for (k = j + 1; k < ch[i].size(); k++) { if (p[ch[i][j]][ch[i][k]] != 1) return 0; } } } */ for (i = 0; i < n; i++) { ans.clear(); for (j =0; j < n; j++) { ans.push_back(co[i][j]); } answer.push_back(ans); } build(answer); return 1; }

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:66:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |   for (j = 0; j < cy[i].size(); j++) {
      |               ~~^~~~~~~~~~~~~~
supertrees.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |    for (k = j + 1; k < cy[i].size(); k++) {
      |                    ~~^~~~~~~~~~~~~~
supertrees.cpp:84:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   84 |    if (bu2[i] == bu2[j] && p[i][j] != 1 || bu2[i] != bu2[j] && p[i][j] != 2) return 0;
      |        ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
supertrees.cpp:26:18: warning: unused variable 't' [-Wunused-variable]
   26 |     int i, j, k, t, t1, t2, la, tcnt;
      |                  ^
supertrees.cpp:26:29: warning: unused variable 'la' [-Wunused-variable]
   26 |     int i, j, k, t, t1, t2, la, tcnt;
      |                             ^~
#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...