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"
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) ((int)((x).size()))
#define pb push_back
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int MxN=1e3+10;
int n, par[MxN], cyc[MxN];
bool v[MxN];
int fnd(int u) {
return par[u]=par[u]==u?u:fnd(par[u]);
}
void mrg(int a, int b) {
a=fnd(a); b=fnd(b);
if(a==b) return;
par[a]=b;
}
int construct(std::vector<std::vector<int>> p) {
n=sz(p);
FOR(i, 0, n) par[i]=i;
vector b(n, vector(n, 0));
FOR(i, 0, n) {
FOR(j, 0, n) {
int u=fnd(i);
if(p[i][j]==1) {
b[u][j]=b[j][u]=1;
mrg(i, j);
}
}
}
vector used(n, false);
FOR(i, 0, n) {
int u=fnd(i);
if(used[u]) continue;
used[u]=true;
cyc[u]=i;
int cn=1, lst=u, frst=u;
FOR(j, 0, n) {
int w=fnd(j);
if(used[w]) continue;
if(p[u][w]==2) {
used[w]=true;
cyc[w]=i;
cn++;
b[lst][w]=b[w][lst]=1;
lst=w;
}
}
b[lst][frst]=b[frst][lst]=1;
if(cn==2) {
return 0;
}
}
bool ok=true;
FOR(i, 0, n) {
FOR(j, 0, n) {
int u=fnd(i), w=fnd(j);
if(u==w) ok&=p[u][w]==1;
else if(cyc[u]==cyc[w]) ok&=p[u][w]==2;
else ok&=p[u][w]==0;
}
}
FOR(i, 0, n) b[i][i]=0;
if(!ok) return 0;
build(b);
return 1;
}
/*
Example 1:
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
Example 2:
2
1 0
0 1
Example 3:
2
1 3
3 1
5
1 1 2 2 2
1 1 2 2 2
2 2 1 2 2
2 2 2 1 1
2 2 2 1 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... |