Submission #853681

#TimeUsernameProblemLanguageResultExecution timeMemory
853681ntkphongConnecting Supertrees (IOI20_supertrees)C++14
96 / 100
176 ms24400 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> par;

int get_root(int u) {
    return par[u] == u ? u : par[u] = get_root(par[u]);
}

bool unite(int u, int v) {
    u = get_root(u);
    v = get_root(v);

    if(u == v) return false;
    
    par[v] = u;
    return true;
}

int construct(vector<vector<int>> p) {
	int n = p.size();

    par.resize(n);
    for(int i = 0; i < n; i ++) par[i] = i;
        
    vector<vector<int>> answer(n, vector<int> (n, 0));
    vector<bool> mark(n, false);
    
    for(int i = 0; i < n; i ++) {
        vector<int> a;
        
        for(int j = 0; j < n; j ++) {
            if(i == j || p[i][j] != 1) continue ;
            a.push_back(j);
        }
        
        if(a.empty()) continue ;
        
        for(int j = 0; j < a.size(); j ++) {
            if(unite(i, a[j])) {
                answer[i][a[j]] = 1;
                answer[a[j]][i] = 1;
            }
        }
    }
    
    for(int i = 0; i < n; i ++) {
        for(int j = 0; j < n; j ++) {
            if(i != j && p[i][j] == 1) {
                if(get_root(i) != get_root(j))
                    return 0;
            }
            
            if(i != j && p[i][j] == 0) {
                if(get_root(i) == get_root(j))
                    return 0;
            }
        }
    }
    
    for(int i = 0; i < n; i ++) {
        if(get_root(i) != i) continue ;
        
        vector<int> a;
        
        for(int j = 0; j < n; j ++) {
            if(i == j || p[i][j] != 2) continue ;
            
            if(mark[j]) return 0;
            a.push_back(j);
        }
        
        if(a.empty()) continue ;
        
        vector<int> b;
        b.push_back(i);
        
        for(int j = 0; j < a.size(); j ++) {
            if(unite(b.back(), a[j])) {
                answer[b.back()][a[j]] = 1;
                answer[a[j]][b.back()] = 1;
                
                b.push_back(a[j]);
            }
        }
        
        if(b.size() <= 2) return 0;
        
        answer[i][b.back()] = 1;
        answer[b.back()][i] = 1;
        
        for(int j = 0; j < n; j ++) {
            if(get_root(i) == get_root(j)) {
                mark[j] = true;
            }
        }
    }
    
    for(int i = 0; i < n; i ++) {
        for(int j = 0; j < n; j ++) {
            if(i != j && p[i][j] == 2) {
                if(get_root(i) != get_root(j) || mark[get_root(i)] == false)
                    return 0;
            }
            
            if(i != j && p[i][j] == 0) {
                if(get_root(i) == get_root(j))
                    return 0;
            }
        }
    }
    
    for(int i = 0; i < n; i ++) {
        if(get_root(i) != i) continue ;
        
        vector<int> a;
        
        for(int j = 0; j < n; j ++) {
            if(i == j || p[i][j] != 3) continue ;
            
            if(mark[j]) return 0;
            a.push_back(j);
        }
        
        if(a.empty()) continue ;
        
        vector<int> b;
        b.push_back(i);
        
        for(int j = 0; j < a.size(); j ++) {
            if(unite(b.back(), a[j])) {
                answer[b.back()][a[j]] = 1;
                answer[a[j]][b.back()] = 1;
                
                b.push_back(a[j]);
            }
        }
        
        if(b.back() <= 3) return 0;
        
        answer[i][b.back()] = 1;
        answer[b.back()][i] = 1;
        
        answer[i][b[b.size() - 2]] = 1;
        answer[b[b.size() - 2]][i] = 1;
        
        for(int j = 0; j < n; j ++) {
            if(get_root(i) == get_root(j)) {
                mark[j] = 1;
            }
        }
    }
    
    for(int i = 0; i < n; i ++) {
        for(int j = 0; j < n; j ++) {
            if(i != j && p[i][j] == 3) {
                if(get_root(i) != get_root(j) || mark[get_root(i)] == false)
                    return 0;
            }
            
            if(i != j && p[i][j] == 0) {
                if(get_root(i) == get_root(j))
                    return 0;
            }
        }
    }
	
	build(answer);
	return 1;
}

Compilation message (stderr)

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