이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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());
if(new_room_group_keys[i].size())
new_room_group_keys[i].erase(unique(new_room_group_keys[i].begin(), new_room_group_keys[i].end()), new_room_group_keys[i].end());
std::sort(new_connector[i].begin(), new_connector[i].end());
if(new_room_group_keys[i].size())
new_connector.erase(unique(new_connector.begin(), new_connector.end()), new_connector.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) {
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();
}
컴파일 시 표준 에러 (stderr) 메시지
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:132:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for(int i = 0; i < r.size(); i++) {
| ~~^~~~~~~~~~
keys.cpp:136:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for(int i = 0; i < u.size(); i++) {
| ~~^~~~~~~~~~
keys.cpp:140:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for(int i = 0; i < r.size(); i++)
| ~~^~~~~~~~~~
# | 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... |