#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define V vector
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()
#define P pair
#define F first
#define S second
V<int>adj[(int)2000];
V<int>comp(2000);
V<bool>visited(2000);
void dfs(int nd,int s,V<int>&vp){
if(visited[nd]){
return;
}
vp.pb(nd);
visited[nd]=true;
comp[nd]=s;
for(auto u:adj[nd]){
dfs(u,s,vp);
}
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
std::vector<std::vector<int>> answer(n,V<int>(n,0));
for(int i=0;i<n;i++){
comp[i]=i;
visited[i]=false;
}
V<V<int>>C;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j]>0 && i!=j){
adj[i].pb(j);
}
}
}
for(int i=0;i<n;i++){
V<int>vp;
dfs(i,i,vp);
if(!vp.empty()){
C.pb(vp);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(comp[i]==comp[j] && p[i][j]==0)return 0;
}
}
for(int i=0;i<n;i++){
adj[i].clear();
visited[i]=false;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j]==2){
adj[i].pb(j);
}
}
}
for(auto u:C){
if(u.size()==1)continue;
visited[u[0]]=true;
for(int i=1;i<(int)u.size();i++){
V<int>vp;
if(!visited[u[i]]){
dfs(u[i],u[i],vp);
if(vp.size()==1){
answer[vp[0]][u[0]]=1;
answer[u[0]][vp[0]]=1;
}
if(vp.size()==2){
for(auto x:vp){
answer[x][u[0]]=1;
answer[u[0]][x]=1;
}
answer[vp[0]][vp[1]]=1;
answer[vp[1]][vp[0]]=1;
}
if(vp.size()>2){
answer[vp[0]][u[0]]=1;
answer[u[0]][vp[0]]=1;
for(int j=0;j<(int)vp.size()-1;j++){
answer[vp[j]][vp[j+1]]=1;
answer[vp[j+1]][vp[j]]=1;
}
answer[vp[0]][vp[(int)vp.size()-1]]=1;
answer[vp[(int)vp.size()-1]][vp[0]]=1;
}
}
}
}
build(answer);
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... |