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 "supertrees.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define INF 1e9+7
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using ppi=pair<int,pii>;
using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T>
void _print(vector<T> x) {cerr<<"{"; for(auto e:x) cerr<<e<<","; cerr<<"}";}
void _print(pii x) {cerr<<"{"<<x.first<<","<<x.second<<"}";}
template<typename T>
void _print(T x) {cerr<<x;}
void dbg() {cerr<<endl;}
template<typename Head,typename... Tail>
void dbg(Head H,Tail... T) {
_print(H);
if(sizeof...(T)) cerr<<",";
else cerr<<"\"]";
dbg(T...);
}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:[\"",dbg(__VA_ARGS__)
const int mxn=1003;
vector<vector<int>> adj,p;
vector<int> group[mxn];
int n;
vector<int> par;
int findp(int x) {
while(par[x]!=x) par[x]=par[par[x]],x=par[x]; return x;
}
void unionset(int u,int v) {
int U=findp(u),V=findp(v);
par[U]=V;
}
bool solve(int root) {
int len=group[root].size();
vector<int> two,mix,one;
for(int i=0;i<len;i++) {
bool istwo=false,isone=false;
for(int j=0;j<len;j++) {
if(i==j) continue;
if(p[i][j]==0) return false;
if(p[i][j]==1) isone=true;
else istwo=true;
}
if(isone and !istwo) one.push_back(i);
else if(isone and istwo) mix.push_back(i);
else two.push_back(i);
}
if(!one.empty() and (!two.empty()|| !mix.empty())) return 0;
int l2=two.size();
for(int i=0;i<l2-1;i++) {
int u=two[i],v=two[i+1];
adj[u][v]=adj[v][u]=1;
}
int l1=one.size();
for(int i=0;i<l1-1;i++) {
int u=one[i],v=one[i+1];
adj[u][v]=adj[v][u]=1;
}
if(l1) return true;
if(mix.empty()) {
int u=two[l2-1];
int v=two[0];
adj[u][v]=adj[v][u]=1;
return true;
}
int l12=mix.size();
for(int i=0;i<l12-1;i++) {
int u=mix[i],v=mix[i+1];
adj[u][v]=adj[v][u]=1;
}
int u=mix[l12-1];
adj[u][two[0]]=adj[two[0]][u]=1;
adj[u][two[l2-1]]=adj[two[l2-1]][u]=1;
return true;
}
int construct(std::vector<std::vector<int>> P) {
p=P;
n=p.size(); par.resize(n);
iota(all(par),0);
adj.resize(n,vector<int>(n,0));
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(p[i][j]!=0) {
unionset(i,j);
}
}
}
for(int i=0;i<n;i++) {
par[i]=findp(par[i]);
}
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(p[i][j]>2) return 0;
if(!p[i][j]) {
if(par[i]==par[j]) return 0;
}
else {
if(par[i]!=par[j]) return 0;
}
}
}
for(int i=0;i<n;i++) {
par[i]=findp(par[i]);
group[par[i]].push_back(i);
}
vector<bool> flag(n+1,false);
for(int i=0;i<n;i++) {
if(flag[par[i]]) continue;
flag[par[i]]=true;
bool ok=solve(par[i]);
if(!ok) return 0;
}
for(int i=0;i<n;i++) adj[i][i]=0;
build(adj);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int findp(int)':
supertrees.cpp:38:5: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
38 | while(par[x]!=x) par[x]=par[par[x]],x=par[x]; return x;
| ^~~~~
supertrees.cpp:38:51: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
38 | while(par[x]!=x) par[x]=par[par[x]],x=par[x]; return x;
| ^~~~~~
# | 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... |