This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
#define fi first
#define se second
#define _size(x) (int)x.size()
#define BIT(i, x) ((x >> i) & 1)
#define MASK(n) ((1 << n) - 1)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--)
#define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i)
#define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i)
#define FORALL(i, a) for (auto i: a)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
vector<int> root;
int getroot(int u) {
return root[u] == u ? u : (root[u] = getroot(root[u]));
}
bool unite(int u, int v) {
u = getroot(u), v = getroot(v);
if (u == v) return false;
root[v] = u;
return true;
}
int construct(vector<vector<int>> p) {
int n = _size(p);
root.resize(n);
REP(i, n) root[i] = i;
vector<pair<int, int>> ans;
REP(i, n) REP(j, n) {
if (i == j) {
if (p[i][j] != 1) return 0;
continue;
}
if (p[i][j] == 1) if (unite(i, j)) ans.push_back({i, j});
if (p[i][j] > 2) return 0;
}
vector<vector<int>> adj(n);
REP(i, n) adj[getroot(i)].push_back(i);
vector<int> used(n);
vector<vector<int>> cnt(n, vector<int>(n));
REP(i, n) {
FORALL(u, adj[i]) FORALL(v, adj[i]) cnt[u][v] = 1;
if (!_size(adj[i]) || used[i]) continue;
vector<int> g;
REP(j, n) if (p[i][j] == 2) g.push_back(getroot(j));
sort(g.begin(), g.end());
g.resize(unique(g.begin(), g.end()) - g.begin());
if (!_size(g)) {
FORALL(u, adj[i]) used[u]++;
continue;
}
if (_size(g) == 1) return 0;
g.push_back(i);
REP(j, _size(g) - 1) ans.push_back({g[j], g[j + 1]});
ans.push_back({g.back(), g[0]});
vector<int> allVtx;
FORALL(id, g) FORALL(u, adj[id]) allVtx.push_back(u), used[u]++;
FORALL(u, allVtx) FORALL(v, allVtx) {
if (getroot(u) == getroot(v)) continue;
cnt[u][v] = 2;
}
}
REP(i, n) REP(j, n) if (cnt[i][j] != p[i][j]) return 0;
vector<vector<int>> b(n, vector<int>(n));
FORALL(e, ans) b[e.fi][e.se] = b[e.se][e.fi] = 1;
build(b);
return 1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |