Submission #335716

#TimeUsernameProblemLanguageResultExecution timeMemory
335716richyrichConnecting Supertrees (IOI20_supertrees)C++17
0 / 100
1 ms364 KiB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3+3;
const int inf = 1e9;

int par[N], ran[N];
int find_p(int a) {
    return (a == par[a]) ? a : find_p(par[a]);
}
void unitee(int a, int b) {
    a = find_p(a), b = find_p(b);
    if(a == b) return;
    if(ran[a] < ran[b]) swap(a, b);
    par[b] = par[a];
    if(ran[a]==ran[b]) ran[a]++;
}
int construct(std::vector<std::vector<int>> p) {
    int n = p.size();
    std::vector<std::vector<int>> answer;
    for (int i = 0; i < n; i++) {
        std::vector<int> row;
        row.resize(n);
        answer.push_back(row);
    }
    for(int i = 0; i < n; i++) par[i] = i, ran[i] = 1;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(p[i][j]==2) {
                unitee(i, j);
            }
        }
    }
    map<int, set<int>> M;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(i==j) {
                answer[i][j] = 0;
                continue;
            }
            if(p[i][j] == 0 && find_p(i)==find_p(j)) return 0;
            answer[i][j] = 0;
            if(p[i][j]==1 && find_p(i)==find_p(j)) {
                int pp = find_p(i);
                if(pp != i)M[pp].insert(i);
                if(pp != j)M[pp].insert(j);
            }
        }
    }
    bool yes = 1;
    for(auto c : M) {
        int f = c.first, prev = c.first;
        for(auto x : c.second) {
            answer[prev][x] = 1, answer[x][prev] = 1;
            prev = x;
        }
        if(c.second.size() > 1){
            answer[f][prev] = answer[prev][f] = 1;
        }
        else {
            yes = 0;
            break;
        }
    }
    if(!yes) return 0;
    build(answer);
    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...