Submission #329526

#TimeUsernameProblemLanguageResultExecution timeMemory
329526blueConnecting Supertrees (IOI20_supertrees)C++17
100 / 100
273 ms28140 KiB
#include "supertrees.h" #include <vector> using namespace std; int construct(vector< vector<int> > p) { int n = p.size(); vector<int> chain(n, -1), ring(n, -1); for(int i = 0; i < n; i++) chain[i] = ring[i] = i; for(int j = 0; j < n; j++) { for(int i = 0; i < j; i++) { if(ring[i] == i) { if(p[i][j] >= 1) { ring[j] = ring[i]; break; } } else { if(p[i][j] >= 1 && ring[i] != ring[j]) return 0; if(p[i][j] == 0 && ring[i] == ring[j]) return 0; } } for(int i = 0; i < j; i++) { if(chain[j] == j) { if(p[i][j] == 1) { chain[j] = chain[i]; break; } } else { if(p[i][j] == 1 && chain[i] != chain[j]) return 0; if(p[i][j] != 1 && chain[i] == chain[j]) return 0; } } } for(int i = 0; i < n; i++) { for(int j = i+1; j < n; j++) { if(p[i][j] == 0) { if(chain[i] == chain[j]) return 0; if(ring[i] == ring[j]) return 0; } else if(p[i][j] == 1) { if(chain[i] != chain[j]) return 0; if(ring[i] != ring[j]) return 0; } else if(p[i][j] == 2) { if(chain[i] == chain[j]) return 0; if(ring[i] != ring[j]) return 0; } else if(p[i][j] == 3) { return 0; } if(ring[i] == ring[j]) { if(p[i][j] == 0) return 0; } else { if(p[i][j] != 0) return 0; } if(chain[i] == chain[j]) { if(p[i][j] != 1) return 0; } else { if(p[i][j] == 1) return 0; } } } vector< vector<int> > neighbors(n); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) { if(i == j) continue; if(p[i][j] >= 1) { neighbors[i].push_back(j); } } for(int i = 0; i < n; i++) { if(neighbors[i].size() == 1 && p[i][neighbors[i][0]] != 1) return 0; } vector< vector<int> > b (n, vector<int>(n, 0)); vector<int> oncycle(n, 1); for(int i = 0; i < n; i++) { for(int j = i-1; j >= 0; j--) if(p[i][j] == 1) { oncycle[i] = 0; b[i][j] = b[j][i] = 1; break; } } int pre; for(int i = 0; i < n; i++) { if(!oncycle[i]) continue; pre = i; oncycle[i] = 0; for(int j = i+1; j < n; j++) { if(!oncycle[j]) continue; if(p[pre][j] == 2) { b[pre][j] = b[j][pre] = 1; pre = j; oncycle[j] = 0; } } if(pre != i) b[pre][i] = b[i][pre] = 1; } build(b); 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...