#include "supertrees.h"
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f0r(i,n) for(int i = 0; i<n; i++)
#define FOR(i, k, n) for(int i = k; i<n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vb vector<bool>
#define vout(v) for(auto u : v)cout<<u<<' '; cout<<'\n';
#define dout(x) cout<<x<<' '<<#x<<'\n';
using namespace std;
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
/*
std::vector<std::vector<int>> answer;
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
build(answer);
*/
vvi ans(n, vi(n));
vb vis(n);
vi chain(n);
int cnt = 0;
vvi chains;
f0r(i, n){
if(!vis[i]){
vi path;
path.pb(i);
chain[i] = cnt;
vis[i] = 1;
f0r(j, n){
if(p[i][j] == 1 && !vis[j]){
vis[j] = 1;
chain[j] = cnt;
path.pb(j);
}
}
chains.pb(path);
f0r(j, path.size() - 1){
ans[path[j]][path[j+1]] = 1;
ans[path[j+1]][path[j]] = 1;
}
cnt++;
}
}
vb vc(chains.size());
int m = chains.size();
f0r(i,m){
if(!vc[i]){
vc[i] = 1;
vi path;
path.pb(chains[i][0]);
f0r(j, m){
if(!vc[j] && p[chains[i][0]][chains[j][0]] == 2){
vc[j] = 1;
path.pb(chains[j][0]);
}
}
if(path.size() >= 2){
f0r(j, path.size() - 1){
ans[path[j]][path[j+1]] = 1;
ans[path[j+1]][path[j]] = 1;
}
ans[path.back()][path[0]] = 1;
ans[path[0]][path.back()] = 1;
}
}
}
vvi ccs;
vi cc(n);
f0r(i,n)vis[i] = 0;
cnt = 0;
f0r(i,n){
if(!vis[i]){
queue<int>q;
q.push(i);
vis[i] = 1;
vi cur;
cur.pb(i);
cc[i] = cnt;
while(!q.empty()){
int node = q.front();
q.pop();
f0r(j, n){
if(!vis[j] && p[node][j] > 0){
q.push(j);
vis[j] = 1;
cur.pb(j);
cc[j] = cnt;
}
}
}
ccs.pb(cur);
cnt++;
}
}
bool ok = 1;
for(auto v : ccs){
int s = v.size();
// vout(v);
for(auto i : v){
for(auto j : v){
if(p[i][j] == 0)ok = 0;
if(chain[i] == chain[j] && p[i][j] != 1)ok = 0;
if(chain[i] != chain[j] && p[i][j] != 2)ok = 0;
}
}
set<int>thing;
for(auto i : v){
thing.insert(chain[i]);
}
if(thing.size() == 2)ok = 0;
}
if(!ok)return 0;
build(ans);
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... |