# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1084778 | Izzy | Bosses (BOI16_bosses) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <limits>
#include <set>
using namespace std;
int n;
map<int, vector<int>> arr;
int test(int start) {
vector<bool> visited(n, false);
vector<vector<int>> levels;
queue<int> q;
q.push(start);
visited[start] = true;
levels.push_back({start});
int salary = 0;
int lvl = 0;
while (!q.empty()) {
int levelSize = q.size();
salary += levelSize * (lvl + 1);
for (int i = 0; i < levelSize; i++) {
int current = q.front();
q.pop();
if (arr.find(current) != arr.end()) {
for (int neighbor : arr[current]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
}
lvl++;
}
// Check if all nodes were visited
if (find(visited.begin(), visited.end(), false) != visited.end()) {
return numeric_limits<int>::max(); // Use a large value to indicate not all nodes were visited
}
return salary;
}
int main() {
int salary = numeric_limits<int>::max(); // Initialize to a large value
cin >> n;
for (int i = 0; i < n; i++) {
int len;
cin >> len;
vector<int> temp(len);
for (int j = 0; j < len; j++) {
cin >> temp[j];
temp[j]--; // Adjust for 0-based index
}
for (int x : temp) {
if (arr.find(x) == arr.end()) {
arr[x] = {i};
} else {
arr[x].push_back(i);
}
}
}
for (const auto& [key, _] : arr) {
salary = min(salary, test(key));
}
cout << salary;
}