제출 #434563

#제출 시각아이디문제언어결과실행 시간메모리
434563yuto1115슈퍼트리 잇기 (IOI20_supertrees)C++17
56 / 100
290 ms22216 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); i++)
#define rrep(i, n) for(ll i = ll(n)-1; i >= 0; i--)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;

template<class T>
bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

class unionfind {
    int n;
    vi p;
public:
    unionfind(int n) : n(n), p(n, -1) {}
    
    int root(int x) {
        if (p[x] < 0) return x;
        return p[x] = root(p[x]);
    }
    
    bool same(int x, int y) { return root(x) == root(y); }
    
    void merge(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return;
        if (p[x] > p[y]) swap(x, y);
        p[x] += p[y];
        p[y] = x;
    }
};

int construct(vvi p) {
    int n = p.size();
    unionfind uf(n);
    rep(i, n) rep2(j, i + 1, n) {
            if (p[i][j] == 3) return 0;
            if (p[i][j]) uf.merge(i, j);
        }
    rep(i, n) rep2(j, i + 1, n) {
            if (!p[i][j] and uf.same(i, j)) return 0;
        }
    map<int, vi> mp;
    rep(i, n) mp[uf.root(i)].pb(i);
    vvi ans(n, vi(n));
    for (auto[_, ls] : mp) {
        int sz = ls.size();
        uf = unionfind(sz);
        rep(i, sz) rep2(j, i + 1, sz) {
                if (p[ls[i]][ls[j]] == 1) uf.merge(i, j);
            }
        rep(i, sz) rep2(j, i + 1, sz) {
                if (p[ls[i]][ls[j]] == 2 and uf.same(i, j)) return 0;
            }
        vi rs;
        rep(i, sz) {
            int r = uf.root(i);
            if (r == i) {
                rs.pb(r);
            } else {
                ans[ls[r]][ls[i]] = 1;
                ans[ls[i]][ls[r]] = 1;
            }
        }
        if (rs.size() >= 2) {
            rep(i, rs.size() - 1) {
                ans[ls[rs[i]]][ls[rs[i + 1]]] = 1;
                ans[ls[rs[i + 1]]][ls[rs[i]]] = 1;
            }
            ans[ls[rs[0]]][ls[rs.back()]] = 1;
            ans[ls[rs.back()]][ls[rs[0]]] = 1;
        }
    }
    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...