Submission #304059

#TimeUsernameProblemLanguageResultExecution timeMemory
3040592qbingxuanConnecting Supertrees (IOI20_supertrees)C++14
100 / 100
271 ms26232 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(...) qqbx(#__VA_ARGS__, __VA_ARGS__)
template <typename H, typename ...T> void qqbx(const char*s, const H &h, T ...args) {
    for(; *s && *s != ','; ++s) if(*s != ' ') std::cerr << *s;
    std::cerr << " = " << h << (sizeof...(T) ? ", " : "\n");
    if constexpr (sizeof...(T)) qqbx(++s, args...);
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define pb emplace_back

using namespace std;
typedef long long ll;
const int N = 2500025;

struct Dsu {
    vector<int> pa;
    Dsu(int n) : pa(n) { iota(pa.begin(), pa.end(), 0); }
    int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
    bool same(int x, int y) { return anc(x) == anc(y); }
    bool join(int x, int y) {
        if((x=anc(x)) == (y=anc(y))) return false;
        return pa[y] = x, true;
    }
};
int construct(vector<vector<int>> p) {
    int n = p.size();
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(p[i][j] == 3) return 0;
    vector<vector<int>> res(n, vector<int>(n));
    Dsu dsu(n);
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {
        if(p[i][j] == 1) {
            if(dsu.join(i, j)) res[i][j] = res[j][i] = 1;
        }
    }
    vector<vector<int>> g(n);
    vector<int> vis(n);
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {
        if(p[i][j] == 2) {
            int a = dsu.anc(i), b = dsu.anc(j);
            if(a == b) return 0;
            g[a].pb(b);
        }
    }
    function<void(int,vector<int>&)> dfs = [&](int i, vector<int> &v) {
        v.pb(i);
        vis[i] = 1;
        for(int j: g[i]) if(!vis[j]) dfs(j, v);
    };
    for(int i = 0; i < n; i++) if(!vis[i]) {
        if(!g[i].size()) continue;
        vector<int> tmp;
        dfs(i, tmp);
        if(tmp.size() == 2) return 0;
        int last = tmp.back();
        for(int x: tmp) {
            res[last][x] = res[x][last] = 1;
            dsu.join(last, x);
            last = x;
        }
    }
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(p[i][j] == 0 && dsu.same(i, j)) return 0;
    build(res);
    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...