Submission #521330

#TimeUsernameProblemLanguageResultExecution timeMemory
521330ddy888Connecting Supertrees (IOI20_supertrees)C++17
21 / 100
199 ms28040 KiB
#include "supertrees.h"
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;  
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#include <vector>

int N;
vector<vector<int> > A;
vector<vector<int> > twos;
vector<vector<int> > ans;

struct DSU {
    vector<int> par;
    void init(int x) { par.resize(x + 1); for (int i = 0; i <= N; ++i) par[i] = i; }
    int root(int x) { return (par[x]==x)? x:par[x]=root(par[x]); }
    bool same(int a, int b) { return root(a) == root(b); }
    void merge(int a, int b) {par[root(a)] = root(b);}
};

void link(int x, int y) {
    if (x == y) return;
    ans[x - 1][y - 1] = ans[y - 1][x - 1] = 1;
}

int construct(std::vector<std::vector<int>> p) {
    N = p.size();
    A.resize(N + 1, vector<int>(N + 1));
    ans.resize(N, vector<int>(N));
    for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) A[i][j] = p[i - 1][j - 1];
    
    DSU dsu1, dsu2;
    dsu1.init(N);
    dsu2.init(N);
    for (int i = 1; i <= N; ++i) {
        for (int j = i + 1; j <= N; ++j) {
            if (A[i][j] == 3) return 0;
            if (A[i][j] == 1) {
                if (dsu1.same(i, j)) continue;
                dsu1.merge(i, j);
                link(i, dsu1.root(i)); link(j, dsu1.root(j));
            } 
        }
    }
    for (int i = 1; i <= N; ++i) {
        bool non = false;
        for (int j = i + 1; j <= N; ++j) {
            if (A[i][j] == 1) non = true;
        }
        if (!non) {
            vector<int> nodes;
            nodes.pb(i);
            for (int j = i + 1; j <= N; ++j) {
                if (A[i][j] == 2) {
                    nodes.pb(j);
                }   
            }
            twos.pb(nodes);
        }
    }
    for (auto st: twos) {
        int ss = st.size();
        for (int i = 1; i < ss; ++i) {
            dsu1.merge(st[i], st[i - 1]);
            link(st[i], st[i - 1]);
        }
        link(st[0], st[ss - 1]);
    }
    for (int i = 1; i <= N; ++i) {
        for (int j = i + 1; j <= N; ++j) {
            if (A[i][j] == 0 && dsu1.same(i, j)) return 0;
        }
    }
    build(ans);
    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...