#include "supertrees.h"
#include <bits/stdc++.h>
#define MAX 300001
#define INF LLONG_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define gett(x,m) get<m>(x)
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define sorta(a,sz) sort(a,a+sz)
#define inputar(a,b){\
for(int i=0;i<b;i++){\
cin >> a[i];\
}\
}
#define inputvec(a,b){\
for(int i=0;i<b;i++){\
ll num;\
cin >> num;\
a.pb(num);\
}\
}
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << "\n";\
}
#define outputvec(a){\
for(auto x:a){\
cout << x << " ";\
}\
cout << "\n";\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
struct dsu{
vector<ll> e;
void init(int n){
e.resize(n+1,-1);
}
int get(int x){
if(e[x]<0){
return x;
}
else{
return e[x]=get(e[x]);
}
}
int size(int x){
return -e[get(x)];
}
bool same_set(int x,int y){
return (get(x)==get(y));
}
bool unite(int x,int y){
x=get(x);
y=get(y);
if(x==y){
return false;
}
if(e[x]>e[y]){
swap(x,y);
}
e[x]+=e[y];
e[y]=x;
return true;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
dsu d;
d.init(n);
vector<vector<int>> answer;
answer.resize(n,vector<int>(n,0));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j]==1){
d.unite(i,j);
}
if(p[i][j]==3){
return 0;
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(d.same_set(i,j) && p[i][j]!=1){
//cout << 1 << "\n";
return 0;
}
}
}
dsu d2;
dsu d3;
d2.init(n);
d3.init(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j]==2){
d2.unite(d.get(i),d.get(j));
}
else if(p[i][j]==3){
d3.unite(d.get(i),d.get(j));
}
}
}
for(int i=0;i<n;i++){
if(d2.size(i)==2){
//cout << 2 << "\n";
return 0;
}
}
for(int i=0;i<n;i++){
if(d3.size(i)==2 || d3.size(i)==3){
//cout << 3 << "\n";
return 0;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j]==0 && (d2.same_set(i,j) || d3.same_set(i,j))){
return 0;
}
if(d2.same_set(i,j) && d3.same_set(i,j) && i!=j){
//cout << 4 << "\n";
return 0;
}
}
}
set<ll> st;
for(int i=0;i<n;i++){
st.ins(i);
}
for(int i=0;i<n;i++){
if(st.find(i)==st.end()){
continue;
}
vector<int> c;
for(auto x:st){
if(d.same_set(i,x)){
c.pb(x);
}
}
for(int i=1;i<c.size();i++){
answer[c[i-1]][c[i]]=1;
answer[c[i]][c[i-1]]=1;
}
for(auto x:c){
st.erase(x);
}
}
st.clear();
for(int i=0;i<n;i++){
st.ins(i);
}
for(int i=0;i<n;i++){
if(st.find(i)==st.end()){
continue;
}
set<int> e;
vector<int> c;
for(auto x:st){
if(d2.same_set(i,x)){
e.ins(d.get(x));
}
}
for(auto x:e){
c.pb(x);
}
if(c.size()>1){
for(int i=1;i<c.size();i++){
answer[c[i-1]][c[i]]=1;
answer[c[i]][c[i-1]]=1;
}
answer[c[0]][c[c.size()-1]]=1;
answer[c[c.size()-1]][c[0]]=1;
}
for(auto x:c){
st.erase(x);
}
}
st.clear();
for(int i=0;i<n;i++){
st.ins(i);
}
for(int i=0;i<n;i++){
if(st.find(i)==st.end()){
continue;
}
set<int> e;
vector<int> c;
for(auto x:st){
if(d3.same_set(i,x)){
e.ins(d.get(x));
}
}
for(auto x:e){
c.pb(x);
}
if(c.size()>1){
for(int i=1;i<c.size();i++){
answer[c[i-1]][c[i]]=1;
answer[c[i]][c[i-1]]=1;
}
answer[c[0]][c[c.size()-1]]=1;
answer[c[c.size()-1]][c[0]]=1;
answer[c[1]][c[c.size()-1]]=1;
answer[c[c.size()-1]][c[1]]=1;
}
for(auto x:c){
st.erase(x);
}
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:161:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
161 | for(int i=1;i<c.size();i++){
| ~^~~~~~~~~
supertrees.cpp:188:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
188 | for(int i=1;i<c.size();i++){
| ~^~~~~~~~~
supertrees.cpp:218:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
218 | for(int i=1;i<c.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
9 ms |
1184 KB |
Output is correct |
7 |
Correct |
201 ms |
24012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
9 ms |
1184 KB |
Output is correct |
7 |
Correct |
201 ms |
24012 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1212 KB |
Output is correct |
13 |
Correct |
193 ms |
23936 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
800 KB |
Output is correct |
17 |
Correct |
94 ms |
14028 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
52 ms |
6240 KB |
Output is correct |
21 |
Correct |
214 ms |
24060 KB |
Output is correct |
22 |
Correct |
207 ms |
24048 KB |
Output is correct |
23 |
Correct |
226 ms |
24064 KB |
Output is correct |
24 |
Correct |
216 ms |
24012 KB |
Output is correct |
25 |
Correct |
77 ms |
14028 KB |
Output is correct |
26 |
Correct |
76 ms |
14084 KB |
Output is correct |
27 |
Correct |
257 ms |
24048 KB |
Output is correct |
28 |
Correct |
193 ms |
24052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
8 ms |
1256 KB |
Output is correct |
9 |
Correct |
253 ms |
23924 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
9 ms |
1236 KB |
Output is correct |
13 |
Correct |
211 ms |
24028 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
852 KB |
Output is correct |
17 |
Correct |
91 ms |
14112 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
52 ms |
6216 KB |
Output is correct |
22 |
Correct |
205 ms |
23932 KB |
Output is correct |
23 |
Correct |
213 ms |
24020 KB |
Output is correct |
24 |
Correct |
206 ms |
24028 KB |
Output is correct |
25 |
Correct |
83 ms |
14200 KB |
Output is correct |
26 |
Correct |
79 ms |
14080 KB |
Output is correct |
27 |
Correct |
195 ms |
24000 KB |
Output is correct |
28 |
Correct |
213 ms |
23924 KB |
Output is correct |
29 |
Correct |
77 ms |
14072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
53 ms |
6248 KB |
Output is correct |
5 |
Correct |
200 ms |
24052 KB |
Output is correct |
6 |
Correct |
203 ms |
24020 KB |
Output is correct |
7 |
Correct |
258 ms |
24008 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
48 ms |
6224 KB |
Output is correct |
10 |
Correct |
230 ms |
23976 KB |
Output is correct |
11 |
Correct |
188 ms |
24044 KB |
Output is correct |
12 |
Correct |
209 ms |
23928 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
53 ms |
6212 KB |
Output is correct |
17 |
Correct |
194 ms |
23820 KB |
Output is correct |
18 |
Correct |
202 ms |
24052 KB |
Output is correct |
19 |
Correct |
201 ms |
23956 KB |
Output is correct |
20 |
Correct |
204 ms |
23972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
9 ms |
1184 KB |
Output is correct |
7 |
Correct |
201 ms |
24012 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1212 KB |
Output is correct |
13 |
Correct |
193 ms |
23936 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
800 KB |
Output is correct |
17 |
Correct |
94 ms |
14028 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
52 ms |
6240 KB |
Output is correct |
21 |
Correct |
214 ms |
24060 KB |
Output is correct |
22 |
Correct |
207 ms |
24048 KB |
Output is correct |
23 |
Correct |
226 ms |
24064 KB |
Output is correct |
24 |
Correct |
216 ms |
24012 KB |
Output is correct |
25 |
Correct |
77 ms |
14028 KB |
Output is correct |
26 |
Correct |
76 ms |
14084 KB |
Output is correct |
27 |
Correct |
257 ms |
24048 KB |
Output is correct |
28 |
Correct |
193 ms |
24052 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
300 KB |
Output is correct |
36 |
Correct |
8 ms |
1256 KB |
Output is correct |
37 |
Correct |
253 ms |
23924 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
9 ms |
1236 KB |
Output is correct |
41 |
Correct |
211 ms |
24028 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
91 ms |
14112 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
52 ms |
6216 KB |
Output is correct |
50 |
Correct |
205 ms |
23932 KB |
Output is correct |
51 |
Correct |
213 ms |
24020 KB |
Output is correct |
52 |
Correct |
206 ms |
24028 KB |
Output is correct |
53 |
Correct |
83 ms |
14200 KB |
Output is correct |
54 |
Correct |
79 ms |
14080 KB |
Output is correct |
55 |
Correct |
195 ms |
24000 KB |
Output is correct |
56 |
Correct |
213 ms |
23924 KB |
Output is correct |
57 |
Correct |
77 ms |
14072 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
296 KB |
Output is correct |
60 |
Correct |
4 ms |
852 KB |
Output is correct |
61 |
Correct |
82 ms |
14068 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
66 ms |
6284 KB |
Output is correct |
66 |
Correct |
74 ms |
13988 KB |
Output is correct |
67 |
Correct |
74 ms |
14068 KB |
Output is correct |
68 |
Correct |
85 ms |
14084 KB |
Output is correct |
69 |
Correct |
83 ms |
14068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
9 ms |
1184 KB |
Output is correct |
7 |
Correct |
201 ms |
24012 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1212 KB |
Output is correct |
13 |
Correct |
193 ms |
23936 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
800 KB |
Output is correct |
17 |
Correct |
94 ms |
14028 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
52 ms |
6240 KB |
Output is correct |
21 |
Correct |
214 ms |
24060 KB |
Output is correct |
22 |
Correct |
207 ms |
24048 KB |
Output is correct |
23 |
Correct |
226 ms |
24064 KB |
Output is correct |
24 |
Correct |
216 ms |
24012 KB |
Output is correct |
25 |
Correct |
77 ms |
14028 KB |
Output is correct |
26 |
Correct |
76 ms |
14084 KB |
Output is correct |
27 |
Correct |
257 ms |
24048 KB |
Output is correct |
28 |
Correct |
193 ms |
24052 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
300 KB |
Output is correct |
36 |
Correct |
8 ms |
1256 KB |
Output is correct |
37 |
Correct |
253 ms |
23924 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
9 ms |
1236 KB |
Output is correct |
41 |
Correct |
211 ms |
24028 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
91 ms |
14112 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
52 ms |
6216 KB |
Output is correct |
50 |
Correct |
205 ms |
23932 KB |
Output is correct |
51 |
Correct |
213 ms |
24020 KB |
Output is correct |
52 |
Correct |
206 ms |
24028 KB |
Output is correct |
53 |
Correct |
83 ms |
14200 KB |
Output is correct |
54 |
Correct |
79 ms |
14080 KB |
Output is correct |
55 |
Correct |
195 ms |
24000 KB |
Output is correct |
56 |
Correct |
213 ms |
23924 KB |
Output is correct |
57 |
Correct |
77 ms |
14072 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
53 ms |
6248 KB |
Output is correct |
62 |
Correct |
200 ms |
24052 KB |
Output is correct |
63 |
Correct |
203 ms |
24020 KB |
Output is correct |
64 |
Correct |
258 ms |
24008 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
48 ms |
6224 KB |
Output is correct |
67 |
Correct |
230 ms |
23976 KB |
Output is correct |
68 |
Correct |
188 ms |
24044 KB |
Output is correct |
69 |
Correct |
209 ms |
23928 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
53 ms |
6212 KB |
Output is correct |
74 |
Correct |
194 ms |
23820 KB |
Output is correct |
75 |
Correct |
202 ms |
24052 KB |
Output is correct |
76 |
Correct |
201 ms |
23956 KB |
Output is correct |
77 |
Correct |
204 ms |
23972 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
296 KB |
Output is correct |
80 |
Correct |
4 ms |
852 KB |
Output is correct |
81 |
Correct |
82 ms |
14068 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
66 ms |
6284 KB |
Output is correct |
86 |
Correct |
74 ms |
13988 KB |
Output is correct |
87 |
Correct |
74 ms |
14068 KB |
Output is correct |
88 |
Correct |
85 ms |
14084 KB |
Output is correct |
89 |
Correct |
83 ms |
14068 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
1 ms |
212 KB |
Output is correct |
92 |
Correct |
4 ms |
852 KB |
Output is correct |
93 |
Correct |
74 ms |
14076 KB |
Output is correct |
94 |
Correct |
1 ms |
304 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
18 ms |
3700 KB |
Output is correct |
98 |
Correct |
70 ms |
14028 KB |
Output is correct |
99 |
Correct |
72 ms |
14068 KB |
Output is correct |
100 |
Correct |
76 ms |
13996 KB |
Output is correct |
101 |
Correct |
69 ms |
14068 KB |
Output is correct |
102 |
Correct |
73 ms |
14072 KB |
Output is correct |
103 |
Correct |
68 ms |
14080 KB |
Output is correct |
104 |
Correct |
72 ms |
14084 KB |
Output is correct |
105 |
Correct |
78 ms |
14064 KB |
Output is correct |