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>
using namespace std;
#define endl '\n'
#define db double
#define ll __int128
//#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;
const int Max = 1e6 + 7, Inf = 1e9 + 7;
void build(std::vector<std::vector<int>> b);
struct DSU
{
vector <int> link, size;
int find(int x)
{
while(x != link[x]) x = link[x];
return x;
}
int sz(int x)
{
return size[find(x)];
}
bool same(int a, int b)
{
return find(a) == find(b);
}
void unite(int a, int b)
{
a = find(a); b = find(b);
if(a == b) return;
if(size[a] < size[b]) swap(a, b);
link[b] = a; size[a] += size[b];
}
DSU(int n)
{
link.assign(n+1, 0);
size.assign(n+1, 1);
for(int i = 0; i <= n; i++)
link[i] = i;
}
};
int construct(std::vector<std::vector<int>> p)
{
int n = p.size(); DSU St(n);
vector <vector<int>> g, ans(n, vector <int> (n, 0));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++) if(j != i)
{
if(p[i][j] != p[j][i])
return 0;
if(p[i][j] == 1)
St.unite(i, j);
}
}
map <int, int> mp, pass; int idx = 0;
for(int i = 0; i < n; i++) if(St.sz(i) > 1)
{
int x = St.find(i);
if(mp[x] == 0)
{
idx++;
mp[x] = idx;
g.push_back(vector <int> ());
}
g[mp[x]-1].push_back(i);
}
for(int i = 0; i < (int) g.size(); i++)
{
for(int j = 0; j < g[i].size(); j++)
{
for(int k = j+1; k < g[i].size(); k++)
{
if(p[g[i][j]][g[i][k]] != 1)
return 0;
}
pass[g[i][j]] = i+1;
}
for(int j = 0; j < g[i].size() - 1; j++)
{
ans[g[i][j]][g[i][j+1]] = 1;
ans[g[i][j+1]][g[i][j]] = 1;
}
int x = g[i][0]; vector <int> s;
for(int j = 0; j < n; j++)
{
if(p[x][j] == 2)
{
if(pass[j] != 0)
return 0;
pass[j] = i+1;
s.push_back(j);
St.unite(x, j);
}
}
if(s.size() > 0 && s.size() < 3) return 0;
if(s.size() > 0)
{
ans[x][s[0]] = 1;
ans[s[0]][x] = 1;
int mod = s.size();
for(int j = 0; j < (int) s.size(); j++)
{
ans[s[j]][s[(j+1) % mod]] = 1;
ans[s[(j+1) % mod]][s[j]] = 1;
}
for(int j = 0; j < g[i].size(); j++)
{
for(int k = 0; k < n; k++) if(p[g[i][j]][k] != 0)
{
if(pass[k] != i + 1) return 0;
}
}
for(int j = 0; j < s.size(); j++)
{
for(int k = 0; k < n; k++) if(p[s[j]][k] != 0)
{
if(pass[k] != i + 1) return 0;
}
}
}
}
for(int i = 0; i < n; i++) if(St.sz(i) == 1)
{
vector <int> s; s.push_back(i);
map <int, int> mk; mk[i] = 1;
for(int j = 0; j < n; j++) if(p[i][j] == 2)
{
mk[j] = 1;
s.push_back(j);
St.unite(i, j);
}
int m = (int) s.size();
if(m != 1)
{
if(m == 2) return 0;
for(int j = 0; j < m; j++)
{
for(int k = 0; k < n; k++)
{
if(((p[s[j]][k] != 0) ^ (mk[k])))
return 0;
}
}
for(int j = 0; j < m; j++)
{
ans[s[j]][s[(j+1) % m]] = 1;
ans[s[(j+1) % m]][s[j]] = 1;
}
}
}
build(ans);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:93:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int j = 0; j < g[i].size(); j++)
| ~~^~~~~~~~~~~~~
supertrees.cpp:95:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int k = j+1; k < g[i].size(); k++)
| ~~^~~~~~~~~~~~~
supertrees.cpp:103:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int j = 0; j < g[i].size() - 1; j++)
| ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:138:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int j = 0; j < g[i].size(); j++)
| ~~^~~~~~~~~~~~~
supertrees.cpp:146:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
146 | for(int j = 0; j < s.size(); j++)
| ~~^~~~~~~~~~
# | 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... |