Submission #550075

#TimeUsernameProblemLanguageResultExecution timeMemory
550075esomerConnecting Supertrees (IOI20_supertrees)C++17
40 / 100
226 ms24016 KiB
#include "supertrees.h" #include <bits/stdc++.h> using namespace std; struct DSU{ vector<int> v; void init(int n){ v.assign(n, -1); } int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);} void unite(int x, int y){ x = get(x); y = get(y); if(x == y) return; if(x > y) swap(x, y); v[x] += v[y]; v[y] = x; return; } bool check(vector<vector<int>>& p, vector<vector<int>>& ans){ for(int i = 0; i < p.size(); i++){ for(int j = 0; j < p.size(); j++){ if(p[i][j] == 1){ if(get(i) != get(j)) return 0; }else{ if(get(i) == get(j)) return 0; } } } for(int i = 0; i < v.size(); i++){ if(v[i] < 0){ int lst = i; for(int j = 0; j < v.size(); j++){ if(j == i) continue; if(v[j] == i){ ans[j][lst] = ans[lst][j] = 1; lst = j; } } } } return 1; } }; DSU UF2; int construct(vector<vector<int>> p) { // int n = p.size(); vector<bool> vis(n, 0); UF2.init(n); bool pos = 1; vector<vector<int>> ans(n, vector<int>(n, 0)); for(int i = 0; i < n; i++){ if(!vis[i]){ vector<int> three; vector<int> two; vector<int> one; vis[i] = 1; for(int j = 0; j < n; j++){ if(i == j || p[i][j] == 0) continue; if(vis[j] == 1){ pos = 0; break; } vis[j] = 1; UF2.unite(i, j); if(p[i][j] == 3) three.push_back(j); else if(p[i][j] == 2) two.push_back(j); else one.push_back(j); } if((three.size() > 0 && three.size() < 3 ) || (two.size() > 0 && two.size() < 2) || (two.size() > 0 && three.size() > 0)){ pos = 0; break; } if(three.size() > 0){ ans[i][three[three.size() - 1]] = ans[i][three[three.size() - 2]] = 1; ans[three[three.size() - 1]][i] = ans[three[three.size() - 2]][i] = 1; ans[three[0]][i] = ans[i][three[0]] = 1; for(int j = 0; j < three.size() - 1; j++){ ans[three[j]][three[j+1]] = ans[three[j+1]][three[j]] = 1; } } if(two.size() > 0){ ans[i][two[two.size() - 1]] = ans[two[two.size() - 1]][i] = 1; ans[i][two[0]] = ans[two[0]][i] = 1; for(int j = 0; j < two.size() - 1; j++){ ans[two[j]][two[j+1]] = ans[two[j+1]][two[j]] = 1; } } if(one.size() > 0){ ans[one[0]][i] = ans[i][one[0]] = 1; for(int j = 0; j < one.size() - 1; j++){ ans[one[j]][one[j+1]] = ans[one[j+1]][one[j]] = 1; } } for(int q : three){ for(int j : three){ if(j == q) continue; if(p[q][j] != 3){ pos = 0; break; } } for(int j : one){ if(p[q][j] != 3){ pos = 0; break; } } } for(int q : two){ for(int j : two){ if(q == j) continue; if(p[q][j] != 2){ pos = 0; break; } } for(int j : one){ if(p[q][j] != 2){ pos = 0; break; } } } for(int q : one){ for(int j : one){ if(q == j) continue; if(p[q][j] != 1){ pos = 0; break; } } } }else{ for(int j = 0; j < n; j++){ if((p[i][j] != 0 && vis[j] == false) || (p[i][j] != 0 && vis[j] == 1 && UF2.get(i) != UF2.get(j))){ pos = 0; break; } } } } if(pos){ build(ans); // return 1; }else return 0; }

Compilation message (stderr)

supertrees.cpp: In member function 'bool DSU::check(std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for(int i = 0; i < p.size(); i++){
      |                    ~~^~~~~~~~~~
supertrees.cpp:23:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |       for(int j = 0; j < p.size(); j++){
      |                      ~~^~~~~~~~~~
supertrees.cpp:31:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |     for(int i = 0; i < v.size(); i++){
      |                    ~~^~~~~~~~~~
supertrees.cpp:34:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for(int j = 0; j < v.size(); j++){
      |                        ~~^~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:81:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for(int j = 0; j < three.size() - 1; j++){
      |                    ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:88:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for(int j = 0; j < two.size() - 1; j++){
      |                    ~~^~~~~~~~~~~~~~~~
supertrees.cpp:94:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for(int j = 0; j < one.size() - 1; 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...