Submission #304499

#TimeUsernameProblemLanguageResultExecution timeMemory
304499Leonardo_PaesConnecting Supertrees (IOI20_supertrees)C++17
56 / 100
275 ms22392 KiB
#include "supertrees.h"
#include <vector>
using namespace std;
const int maxn = 1e3+10;
int pai[maxn], sz[maxn];
int find(int x){ return pai[x] = (pai[x] == x ? x : find(pai[x])); }
void join(int a, int b){
    a = find(a), b = find(b);
    if(a == b) return;
    if(sz[a] > sz[b]) swap(a, b);
    sz[b] += sz[a];
    pai[a] = b;
}
int construct(vector<vector<int> > p) {
	int n = p.size();
	vector<vector<int> > ans;
	for(int i=0; i<n; i++){
        vector<int> row;
        for(int j=0; j<n; j++){
            row.push_back(0);
        }
        ans.push_back(row);
	}
    for(int i=0; i<n; i++){
        pai[i] = i;
        sz[i] = 1;
    }
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(p[i][j]) join(i, j);
        }
    }
    bool ok = 1;
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(p[i][j] == 0){
                if(find(i) == find(j)) ok = 0;
            }
        }
    }
    if(!ok) return 0;
    vector<int> cc[n];
    for(int i=0; i<n; i++) cc[find(i)].push_back(i);
    for(int i=0; i<n; i++){
        if(cc[i].empty() or !ok) continue;
        for(int j=0; j<n; j++){
            pai[j] = j;
            sz[j] = 1;
        }
        for(auto x : cc[i]){
            for(auto y : cc[i]){
                if(p[x][y] == 1){
                    join(x, y);
                }
            }
        }
        for(auto x : cc[i]){
            for(auto y : cc[i]){
                if(p[x][y] == 2){
                    if(find(x) == find(y)) ok = 0;
                }
            }
        }
        if(!ok) continue;
        vector<int> v;
        for(auto j : cc[i]){
            int rep = find(j);
            if(rep == j) v.push_back(j);
            else ans[j][rep] = ans[rep][j] = 1;
            for(auto wtf : cc[i]){
                int rep2 = find(wtf);
                if(rep2 != rep and p[j][wtf] != 2) ok = 0;
            }
        }
        if(v.size() == 1) continue;
        for(int j=0; j+1<v.size(); j++){
            if(p[v[j]][v[j+1]] != 2) continue;
            ans[v[j]][v[j+1]] = ans[v[j+1]][v[j]] = 1;
        }
        if(p[v[0]][v.back()] == 2) ans[v.back()][v[0]] = ans[v[0]][v.back()] = 1;
    }
    if(!ok) return 0;
    build(ans);
	return 1;
	/*
6
1 2 2 1 2 2
2 1 2 2 1 2
2 2 1 2 2 1
1 2 2 1 2 2
2 1 2 2 1 2
2 2 1 2 2 1
5
1 1 0 0 0
1 1 0 0 0
0 0 1 2 2
0 0 2 1 2
0 0 2 2 1
*/
}

Compilation message (stderr)

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