#include <cstdio>
#include <vector>
#include <deque>
#include <algorithm>
#include <set>
using namespace std;
int increment = 0;
vector<vector<int>> room_group_keys;// --- room group ... data: keys assigned to room group, ordered by key
vector<vector<int>> room_group_rooms;
vector<vector<pair<int, int>>> connector;// --- connector ... data: (outbound room group, key)
vector<int> stack;
vector<int> mutex, depth;
void dfs_init() {
stack.clear();
increment = 0;
mutex.resize(room_group_keys.size());
for(auto& i : mutex) i = 0;
depth.resize(room_group_keys.size());
for(auto& i : depth) i = 0;
}
void dfs(int x) {
if(mutex[x] == 1) return;
mutex[x] = 1;
for(auto i : connector[x]) {
if(binary_search(room_group_keys[x].begin(), room_group_keys[x].end(), i.second))
dfs(i.first);
}
stack.push_back(x);
}
bool rdfs(int x) {
if(mutex[x] == 2) return false;
mutex[x] = 2;
depth[x] = increment;
for(auto i : connector[x]) {
if(binary_search(room_group_keys[i.first].begin(), room_group_keys[i.first].end(), i.second))
rdfs(i.first);
}
return true;
}
void dfs_final(int x, vector<int>& logging) {
if(mutex[x] == 1)
return;
mutex[x] = 1;
for(auto i : connector[x]) {
if(binary_search(room_group_keys[x].begin(), room_group_keys[x].end(), i.second)) {
if(x != i.first)
logging[x] = false;
dfs_final(i.first, logging);
}
}
}
vector<int> find_reachable_internal() {
int prev = room_group_keys.size();
// To-do: construct SCC
dfs_init();
for(int i = 0; i < room_group_keys.size(); i++) dfs(i);
while(stack.size()) {
int top = stack.back();
if(rdfs(top))
increment++;
stack.pop_back();
}
// Report result (if SCC is equal with graph)
if(prev == increment) {
dfs_init();
vector<int> logging;
logging.resize(prev, true);
for(int i = 0; i < prev; i++)
dfs_final(i, logging);
int min_size = 1e9, sum = 0;
for(int i = 0; i < prev; i++) {
int s = room_group_rooms[i].size();
sum += s;
if(min_size > s && logging[i])
min_size = s;
}
vector<int> ans;
ans.resize(sum, 0);
for(int i = 0; i < prev; i++) {
int s = room_group_rooms[i].size();
if(min_size != s || !logging[i])
continue;
for(auto j : room_group_rooms[i]) {
ans[j] = 1;
}
}
return ans;
}
// Query (if SCC is smaller than graph)
vector<vector<int>> new_room_group_keys;
vector<vector<int>> new_room_group_rooms;
vector<vector<pair<int, int>>> new_connector;
new_room_group_keys.resize(increment, {});
new_room_group_rooms.resize(increment, {});
new_connector.resize(increment, {});
for(int i = 0; i < prev; i++) {
for(auto j : room_group_keys[i])
new_room_group_keys[depth[i]].push_back(j);
for(auto j : room_group_rooms[i])
new_room_group_rooms[depth[i]].push_back(j);
for(auto j : connector[i])
new_connector[depth[i]].push_back({depth[j.first], j.second});
}
for(int i = 0; i < increment; i++)
std::sort(new_room_group_keys[i].begin(), new_room_group_keys[i].end());
room_group_keys = new_room_group_keys;
room_group_rooms = new_room_group_rooms;
connector = new_connector;
//return {};
return find_reachable_internal();
}
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) {
// vector<vector<int>> room_group_keys, vector<vector<int>> room_group_rooms, vector<vector<pair<int, int>>> connector
connector.resize(r.size(), {});
for(int i = 0; i < r.size(); i++) {
room_group_keys.push_back({r[i]});
room_group_rooms.push_back({i});
}
for(int i = 0; i < u.size(); i++) {
connector[u[i]].push_back({v[i], c[i]});
connector[v[i]].push_back({u[i], c[i]});
}
for(int i = 0; i < r.size(); i++)
std::sort(room_group_keys[i].begin(), room_group_keys[i].end());
connector.resize(r.size());
return find_reachable_internal();
}
Compilation message
keys.cpp: In function 'std::vector<int> find_reachable_internal()':
keys.cpp:63:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < room_group_keys.size(); i++) dfs(i);
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
keys.cpp: In function 'std::vector<int> find_reachable(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
keys.cpp:127:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for(int i = 0; i < r.size(); i++) {
| ~~^~~~~~~~~~
keys.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 = 0; i < u.size(); i++) {
| ~~^~~~~~~~~~
keys.cpp:135:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(int i = 0; i < r.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
280 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
280 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
30 |
Correct |
2 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
544 KB |
Output is correct |
34 |
Correct |
1 ms |
544 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
3 ms |
844 KB |
Output is correct |
37 |
Correct |
3 ms |
844 KB |
Output is correct |
38 |
Correct |
4 ms |
844 KB |
Output is correct |
39 |
Correct |
3 ms |
844 KB |
Output is correct |
40 |
Correct |
3 ms |
588 KB |
Output is correct |
41 |
Correct |
4 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Execution timed out |
3084 ms |
538384 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
280 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
30 |
Correct |
2 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
544 KB |
Output is correct |
34 |
Correct |
1 ms |
544 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
3 ms |
844 KB |
Output is correct |
37 |
Correct |
3 ms |
844 KB |
Output is correct |
38 |
Correct |
4 ms |
844 KB |
Output is correct |
39 |
Correct |
3 ms |
844 KB |
Output is correct |
40 |
Correct |
3 ms |
588 KB |
Output is correct |
41 |
Correct |
4 ms |
716 KB |
Output is correct |
42 |
Execution timed out |
3084 ms |
538384 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |