#include "supertrees.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
using ll = int;
struct dsu{
ll n;
vector<ll> p;
dsu(ll N):n(N),p(N,-1){}
ll g(ll i){
return p[i]<0?i:p[i]=g(p[i]);
}
bool e(ll a, ll b){
return g(a)==g(b);
}
bool m(ll a, ll b){
a = g(a);
b = g(b);
if(a==b) return false;
if (p[a]>p[b]) swap(a,b);
p[a] += p[b];
p[b] = a;
return true;
}
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
vector<vector<ll>> ans(n,vector<ll>(n,0));
dsu d(n);
for(ll i = 0;i<n;i++) for(ll j = 0;j<n;j++){
if (p[i][j]==3) return 0;
if (p[i][j]) d.m(i,j);
}
for(ll i = 0;i<n;i++) for(ll j = 0;j<n;j++){
if (!p[i][j]&&d.e(i,j)) return 0;
}
vector<ll> g;
for(ll i = 0;i<n;i++) if(d.g(i)==i) g.push_back(i);
ll gc = g.size();
vector<vector<ll>> gs(gc);
for(ll i = 0;i<n;i++) gs[lower_bound(g.begin(),g.end(),d.g(i))-g.begin()].push_back(i);
for(auto &o : gs) if(o.size()>1){
ll ty = 3;
ll is_1 = 1, is_2 = 1;
for(ll i : o) for(ll j : o) if(i!=j){
if (p[i][j]!=1) is_1==0;
if (p[i][j]!=2) is_2==0;
}
if(is_1) ty=1;
if(is_2) ty=2;
if (ty!=3){
for(ll i = 1;i<o.size();i++) ans[o[i-1]][o[i]] = ans[o[i]][o[i-1]] = 1;
if (ty == 2){
if (o.size()==2) return 0;
ans[o.front()][o.back()] = ans[o.back()][o.front()] = 1;
}
}else{
ll m = o.size();
dsu d(m);
for(ll i = 0;i<m;i++)for(ll j = 0;j<m;j++) if(i!=j){
if (p[o[i]][o[j]]==1) d.m(i,j);
}
for(ll i = 0;i<m;i++)for(ll j = 0;j<m;j++) if(i!=j){
if (d.e(i,j)&&p[o[i]][o[j]]==1) return 0;
}
vector<ll> c;
for(ll i = 0;i<m;i++) if(d.g(i)==i) c.push_back(i);
ll cc = c.size();
if (cc<3) return 0;
vector<vector<ll>> cs(cc);
for(ll i = 0;i<m;i++) cs[lower_bound(c.begin(),c.end(),d.g(i))-c.begin()].push_back(o[i]);
for(auto &cl : cs){
for(ll i = 1;i<cl.size();i++) ans[cl[i-1]][cl[i]] = ans[cl[i]][cl[i-1]] = 1;
}
for(ll i = 1;i<cc;i++) ans[cs[i-1][0]][cs[i][0]] = ans[cs[i][0]][cs[i-1][0]] = 1;
ans[cs.front()[0]][cs.back()[0]] = ans[cs.back()[0]][cs.front()[0]] = 1;
}
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:47:24: warning: statement has no effect [-Wunused-value]
47 | if (p[i][j]!=1) is_1==0;
| ~~~~^~~
supertrees.cpp:48:24: warning: statement has no effect [-Wunused-value]
48 | if (p[i][j]!=2) is_2==0;
| ~~~~^~~
supertrees.cpp:53:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(ll i = 1;i<o.size();i++) ans[o[i-1]][o[i]] = ans[o[i]][o[i-1]] = 1;
| ~^~~~~~~~~
supertrees.cpp:74:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(ll i = 1;i<cl.size();i++) ans[cl[i-1]][cl[i]] = ans[cl[i]][cl[i-1]] = 1;
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
1116 KB |
Output is correct |
9 |
Correct |
99 ms |
22036 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1276 KB |
Output is correct |
13 |
Correct |
91 ms |
22140 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
53 ms |
12060 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
23 ms |
5720 KB |
Output is correct |
22 |
Correct |
95 ms |
22096 KB |
Output is correct |
23 |
Correct |
93 ms |
22104 KB |
Output is correct |
24 |
Correct |
110 ms |
22096 KB |
Output is correct |
25 |
Correct |
41 ms |
12112 KB |
Output is correct |
26 |
Correct |
41 ms |
12124 KB |
Output is correct |
27 |
Correct |
97 ms |
21944 KB |
Output is correct |
28 |
Correct |
99 ms |
22096 KB |
Output is correct |
29 |
Correct |
43 ms |
12296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Too many ways to get from 1 to 4, should be 1 found no less than 2 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |