#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef set<int> si;
typedef vector<vi> vvi;
typedef vector<si> vsi;
typedef vector<ii> vii;
#define loop(x, i) for (int i = 0; i < (x); i++)
#define ALL(x) (x).begin(), x.end()
#define pb push_back
vvi adj, ans;
int N;
vi components;
vb hasCycle;
vector<vvi> cycleTakken;
bool dfs(int i, int c)
{
components[i] = c;
loop(N, j)
{
if (adj[i][j] > 0)
{
if (components[j] == c)
continue;
if (components[j] != -1)
return 0;
// ans[i][j] = 1;
// ans[j][i] = 1;
if (!dfs(j, c))
return 0;
}
}
return 1;
}
vb inTak;
bool cycleFinder(int i, int c)
{
inTak[i] = 1;
loop(N, j)
{
if (inTak[j])
continue;
if (adj[i][j] == 1)
{
for (int k : cycleTakken[c].back())
{
if (adj[j][k] != 1)
{
return 0;
}
}
cycleTakken[c].back().pb(j);
cycleFinder(j, c);
}
}
return 1;
}
int construct(vvi p)
{
N = p.size();
adj = p;
components = vi(N, -1);
int c = 0;
ans = vvi(N, vi(N));
loop(N, i)
{
if (components[i] == -1)
{
if (!dfs(i, c))
return 0;
c++;
}
}
loop(N, i)
{
loop(N, j)
{
if (i == j)
continue;
if (p[i][j] == 0 && components[i] == components[j])
return 0;
if (p[i][j] > 0 && components[i] != components[j])
return 0;
if (p[i][j] == 3)
return 0;
}
}
hasCycle.resize(c);
loop(N, i)
{
if (hasCycle[components[i]])
continue;
loop(N, j)
{
if (p[i][j] == 2)
{
hasCycle[components[i]] = 1;
break;
}
}
}
inTak = vb(N);
cycleTakken.resize(c);
loop(N, i)
{
int k = components[i];
if (inTak[i])
continue;
cycleTakken[k].pb({i});
if (!cycleFinder(i, k))
return 0;
}
loop(c, k)
{
// if (!hasCycle[k]) continue;
vi uiteinden;
if (cycleTakken[k].size() == 2)
return 0;
for (vi tak : cycleTakken[k])
{
uiteinden.pb(tak.back());
for (int i = 1; i < tak.size(); i++)
{
ans[tak[i]][tak[i - 1]] = 1;
ans[tak[i - 1]][tak[i]] = 1;
}
}
if (uiteinden.size() >= 2)
{
ans[uiteinden[0]][uiteinden.back()] = 1;
ans[uiteinden.back()][uiteinden[0]] = 1;
for (int i = 1; i < uiteinden.size(); i++)
{
ans[uiteinden[i]][uiteinden[i - 1]] = 1;
ans[uiteinden[i - 1]][uiteinden[i]] = 1;
}
}
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(vvi)':
supertrees.cpp:131:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | for (int i = 1; i < tak.size(); i++)
| ~~^~~~~~~~~~~~
supertrees.cpp:141:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for (int i = 1; i < uiteinden.size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
420 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
115 ms |
27956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
420 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
115 ms |
27956 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
120 ms |
28168 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
1112 KB |
Output is correct |
17 |
Correct |
62 ms |
18052 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
7260 KB |
Output is correct |
21 |
Correct |
116 ms |
27988 KB |
Output is correct |
22 |
Correct |
116 ms |
27988 KB |
Output is correct |
23 |
Correct |
124 ms |
28032 KB |
Output is correct |
24 |
Correct |
123 ms |
28104 KB |
Output is correct |
25 |
Correct |
52 ms |
18000 KB |
Output is correct |
26 |
Correct |
52 ms |
18096 KB |
Output is correct |
27 |
Correct |
120 ms |
27984 KB |
Output is correct |
28 |
Correct |
117 ms |
28060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
1548 KB |
Output is correct |
9 |
Correct |
118 ms |
28164 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
7 ms |
1444 KB |
Output is correct |
13 |
Correct |
115 ms |
28036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
960 KB |
Output is correct |
17 |
Correct |
65 ms |
18056 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 |
34 ms |
7252 KB |
Output is correct |
22 |
Correct |
121 ms |
27984 KB |
Output is correct |
23 |
Correct |
125 ms |
27988 KB |
Output is correct |
24 |
Correct |
123 ms |
27984 KB |
Output is correct |
25 |
Correct |
78 ms |
18256 KB |
Output is correct |
26 |
Correct |
54 ms |
18004 KB |
Output is correct |
27 |
Correct |
124 ms |
28024 KB |
Output is correct |
28 |
Correct |
123 ms |
28160 KB |
Output is correct |
29 |
Correct |
52 ms |
18184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
29 ms |
7304 KB |
Output is correct |
5 |
Correct |
118 ms |
27908 KB |
Output is correct |
6 |
Correct |
114 ms |
27928 KB |
Output is correct |
7 |
Correct |
121 ms |
27984 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
29 ms |
7296 KB |
Output is correct |
10 |
Correct |
118 ms |
27984 KB |
Output is correct |
11 |
Correct |
127 ms |
28176 KB |
Output is correct |
12 |
Correct |
125 ms |
28044 KB |
Output is correct |
13 |
Correct |
1 ms |
508 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
612 KB |
Output is correct |
16 |
Correct |
30 ms |
7252 KB |
Output is correct |
17 |
Correct |
119 ms |
27984 KB |
Output is correct |
18 |
Correct |
128 ms |
27900 KB |
Output is correct |
19 |
Correct |
112 ms |
28044 KB |
Output is correct |
20 |
Correct |
117 ms |
27988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
420 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
115 ms |
27956 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
120 ms |
28168 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
1112 KB |
Output is correct |
17 |
Correct |
62 ms |
18052 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
7260 KB |
Output is correct |
21 |
Correct |
116 ms |
27988 KB |
Output is correct |
22 |
Correct |
116 ms |
27988 KB |
Output is correct |
23 |
Correct |
124 ms |
28032 KB |
Output is correct |
24 |
Correct |
123 ms |
28104 KB |
Output is correct |
25 |
Correct |
52 ms |
18000 KB |
Output is correct |
26 |
Correct |
52 ms |
18096 KB |
Output is correct |
27 |
Correct |
120 ms |
27984 KB |
Output is correct |
28 |
Correct |
117 ms |
28060 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1548 KB |
Output is correct |
37 |
Correct |
118 ms |
28164 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
604 KB |
Output is correct |
40 |
Correct |
7 ms |
1444 KB |
Output is correct |
41 |
Correct |
115 ms |
28036 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
960 KB |
Output is correct |
45 |
Correct |
65 ms |
18056 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
34 ms |
7252 KB |
Output is correct |
50 |
Correct |
121 ms |
27984 KB |
Output is correct |
51 |
Correct |
125 ms |
27988 KB |
Output is correct |
52 |
Correct |
123 ms |
27984 KB |
Output is correct |
53 |
Correct |
78 ms |
18256 KB |
Output is correct |
54 |
Correct |
54 ms |
18004 KB |
Output is correct |
55 |
Correct |
124 ms |
28024 KB |
Output is correct |
56 |
Correct |
123 ms |
28160 KB |
Output is correct |
57 |
Correct |
52 ms |
18184 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
432 KB |
Output is correct |
60 |
Correct |
3 ms |
1112 KB |
Output is correct |
61 |
Correct |
75 ms |
18156 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
30 ms |
7288 KB |
Output is correct |
66 |
Correct |
53 ms |
18000 KB |
Output is correct |
67 |
Correct |
70 ms |
18004 KB |
Output is correct |
68 |
Correct |
51 ms |
18004 KB |
Output is correct |
69 |
Correct |
53 ms |
18172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
420 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
115 ms |
27956 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1368 KB |
Output is correct |
13 |
Correct |
120 ms |
28168 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
1112 KB |
Output is correct |
17 |
Correct |
62 ms |
18052 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
7260 KB |
Output is correct |
21 |
Correct |
116 ms |
27988 KB |
Output is correct |
22 |
Correct |
116 ms |
27988 KB |
Output is correct |
23 |
Correct |
124 ms |
28032 KB |
Output is correct |
24 |
Correct |
123 ms |
28104 KB |
Output is correct |
25 |
Correct |
52 ms |
18000 KB |
Output is correct |
26 |
Correct |
52 ms |
18096 KB |
Output is correct |
27 |
Correct |
120 ms |
27984 KB |
Output is correct |
28 |
Correct |
117 ms |
28060 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1548 KB |
Output is correct |
37 |
Correct |
118 ms |
28164 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
604 KB |
Output is correct |
40 |
Correct |
7 ms |
1444 KB |
Output is correct |
41 |
Correct |
115 ms |
28036 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
960 KB |
Output is correct |
45 |
Correct |
65 ms |
18056 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
34 ms |
7252 KB |
Output is correct |
50 |
Correct |
121 ms |
27984 KB |
Output is correct |
51 |
Correct |
125 ms |
27988 KB |
Output is correct |
52 |
Correct |
123 ms |
27984 KB |
Output is correct |
53 |
Correct |
78 ms |
18256 KB |
Output is correct |
54 |
Correct |
54 ms |
18004 KB |
Output is correct |
55 |
Correct |
124 ms |
28024 KB |
Output is correct |
56 |
Correct |
123 ms |
28160 KB |
Output is correct |
57 |
Correct |
52 ms |
18184 KB |
Output is correct |
58 |
Correct |
0 ms |
436 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
29 ms |
7304 KB |
Output is correct |
62 |
Correct |
118 ms |
27908 KB |
Output is correct |
63 |
Correct |
114 ms |
27928 KB |
Output is correct |
64 |
Correct |
121 ms |
27984 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
29 ms |
7296 KB |
Output is correct |
67 |
Correct |
118 ms |
27984 KB |
Output is correct |
68 |
Correct |
127 ms |
28176 KB |
Output is correct |
69 |
Correct |
125 ms |
28044 KB |
Output is correct |
70 |
Correct |
1 ms |
508 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
612 KB |
Output is correct |
73 |
Correct |
30 ms |
7252 KB |
Output is correct |
74 |
Correct |
119 ms |
27984 KB |
Output is correct |
75 |
Correct |
128 ms |
27900 KB |
Output is correct |
76 |
Correct |
112 ms |
28044 KB |
Output is correct |
77 |
Correct |
117 ms |
27988 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
432 KB |
Output is correct |
80 |
Correct |
3 ms |
1112 KB |
Output is correct |
81 |
Correct |
75 ms |
18156 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
30 ms |
7288 KB |
Output is correct |
86 |
Correct |
53 ms |
18000 KB |
Output is correct |
87 |
Correct |
70 ms |
18004 KB |
Output is correct |
88 |
Correct |
51 ms |
18004 KB |
Output is correct |
89 |
Correct |
53 ms |
18172 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
3 ms |
960 KB |
Output is correct |
93 |
Correct |
57 ms |
18056 KB |
Output is correct |
94 |
Correct |
0 ms |
344 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
17 ms |
4924 KB |
Output is correct |
98 |
Correct |
66 ms |
18056 KB |
Output is correct |
99 |
Correct |
53 ms |
18180 KB |
Output is correct |
100 |
Correct |
52 ms |
18004 KB |
Output is correct |
101 |
Correct |
54 ms |
18000 KB |
Output is correct |
102 |
Correct |
53 ms |
18176 KB |
Output is correct |
103 |
Correct |
65 ms |
18008 KB |
Output is correct |
104 |
Correct |
52 ms |
18008 KB |
Output is correct |
105 |
Correct |
66 ms |
18212 KB |
Output is correct |