Submission #536382

#TimeUsernameProblemLanguageResultExecution timeMemory
536382ddy888Connecting Supertrees (IOI20_supertrees)C++17
100 / 100
199 ms28168 KiB
#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 "supertrees.h"

int N;
int A[1010][1010], branch[1010];
vector<vector<int> > ans;
set<int> adj[1010];

struct dsu {    
    int par[1010];
    void init(int x) {for (int i = 0; i <= x; ++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) { // b absorbs a
        if (same(a, b)) return;
        par[root(a)] = root(b);
    }
};

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

int construct(std::vector<std::vector<int>> p) {
    N = p.size();
    ans.resize(N, vector<int>(N));
    dsu one, two;
    one.init(N), two.init(N);
    for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) A[i][j] = p[i - 1][j - 1];
    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] == 2) two.merge(i, j);
            if (A[i][j] == 1) {
                join(one.root(i), one.root(j));
                one.merge(one.root(i), one.root(j));
            }
        }
    }
    for (int i = 1; i <= N; ++i) branch[i] = (one.root(i) != i);
    for (int i = 1; i <= N; ++i) adj[two.root(i)].insert(i);
    for (int i = 1; i <= N; ++i) {
        if ((int)adj[i].size() < 2) continue;
        vector<int> nodes;
        for (auto j: adj[i]) {
            if (branch[j]) continue;
            nodes.pb(j);
        }
        if ((int)nodes.size() <= 2) return 0;
        int cnt = 0;
        for (auto j: nodes) {
            for (auto k: nodes) {
                if (j == k) continue;
                cnt += (A[j][k] == 2);
            }
        }
        int sz = (int)nodes.size();
        if (cnt != sz * (sz - 1)) return 0;
        for (int j = 1; j < (int)nodes.size(); ++j) join(nodes[j], nodes[j - 1]);
        join(nodes[0], nodes.back());
    }
    for (int i = 1; i <= N; ++i) {
        for (int j = i + 1; j <= N; ++j) {
            if (A[i][j] == 0 && (one.same(i, j) || two.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...