| Task: | Connect cities |
| Sender: | ileska |
| Submission time: | 2025-09-07 23:09:07 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.48 s | details |
| #7 | ACCEPTED | 0.53 s | details |
| #8 | ACCEPTED | 0.53 s | details |
| #9 | ACCEPTED | 0.53 s | details |
| #10 | ACCEPTED | 0.49 s | details |
| #11 | ACCEPTED | 0.18 s | details |
| #12 | ACCEPTED | 0.00 s | details |
Code
#include <algorithm>
#include <iostream>
#include <set>
#include <unordered_map>
#include <vector>
// def addSubItems(curItem, listIndex, roadsSetList, roadDict, checkedDict):
// #print("Checking", curItem)
// if checkedDict[curItem] != -1:
// return 0
// checkedDict[curItem] = listIndex
// if len(roadsSetList) <= listIndex:
// #roadsSetList += [set() for _ in range(10)]
// roadsSetList.append(set())
// if curItem in roadDict:
// roadsSetList[checkedDict[curItem]].update(roadDict[curItem])
// else:
// roadsSetList[checkedDict[curItem]] = set([curItem])
// return 1
// for oneRoad in roadDict[curItem]:
// if checkedDict[oneRoad] != -1:
// continue
// addSubItems(oneRoad, listIndex, roadsSetList, roadDict, checkedDict)
// return 1
int addSubItems(int curItem, int listIndex,
std::vector<std::set<int>> &roadsSetList,
std::unordered_map<int, std::set<int>> &roadDict,
std::vector<int> &checkedDict) {
if (checkedDict[curItem] != -1) {
return 0;
}
checkedDict[curItem] = listIndex;
if (roadsSetList.size() <= (long unsigned int)listIndex) {
std::set<int> kk;
roadsSetList.push_back(kk);
}
if (roadDict.contains(curItem)) {
// roadsSetList[checkedDict[curItem]].update(roadDict[curItem]);
roadsSetList[checkedDict[curItem]].insert(roadDict[curItem].begin(),
roadDict[curItem].end());
} else {
roadsSetList[checkedDict[curItem]] = std::set({curItem});
return 1;
}
for (int oneRoad : roadDict[curItem]) {
if (checkedDict[oneRoad] != -1) {
continue;
}
addSubItems(oneRoad, listIndex, roadsSetList, roadDict, checkedDict);
}
return 1;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::set<T> &set) {
if (set.empty())
return out << "{}";
out << "{ " << *set.begin();
std::for_each(std::next(set.begin()), set.end(),
[&out](const T &element) { out << ", " << element; });
return out << " }";
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &set) {
if (set.empty())
return out << "[]";
out << "[ " << *set.begin();
std::for_each(std::next(set.begin()), set.end(),
[&out](const T &element) { out << ", " << element; });
return out << " ]";
}
template <typename T>
std::ostream &operator<<(std::ostream &out,
const std::unordered_map<T, std::set<int>> &set) {
if (set.empty())
return out << "{}";
out << "{ ";
for (const auto &[key1, value1] : set) {
std::cout << key1 << ": " << value1 << std::endl;
}
return out << " }";
}
int main() {
char debug = 0;
int cities = 0;
int roads = 0;
std::cin >> cities;
std::cin >> roads;
// std::cout << count << " " << carCount << std::endl;
// std::cout << count << std::endl;
// std::cout << cityCount << " " << roadCount << std::endl;
std::vector<int> alreadyRoads(roads * 2);
for (int ii = 0; ii < roads * 2; ii += 2) {
int city1;
std::cin >> city1;
alreadyRoads[ii] = city1 - 1;
int city2;
std::cin >> city2;
alreadyRoads[ii + 1] = city2 - 1;
}
if (debug)
std::cout << alreadyRoads << std::endl;
std::unordered_map<int, std::set<int>> roadDict;
for (long unsigned int ii = 0; ii < alreadyRoads.size(); ii += 2) {
int start = alreadyRoads[ii];
int end = alreadyRoads[ii + 1];
if (roadDict.contains(start)) {
roadDict[start].insert(end);
} else {
roadDict[start] = std::set<int>{end};
}
if (roadDict.contains(end)) {
roadDict[end].insert(start);
} else {
roadDict[end] = std::set<int>{start};
}
}
if (debug)
std::cout << roadDict << std::endl;
std::vector<int> checkedDict(cities, -1);
std::vector<std::set<int>> roadsSetList;
int counter = 0;
for (int ii = 0; ii < cities; ii++) {
int ids = checkedDict[ii];
if (ids != -1) {
continue;
}
counter += addSubItems(ii, counter, roadsSetList, roadDict, checkedDict);
}
if (debug)
std::cout << checkedDict << std::endl;
if (debug)
std::cout << roadsSetList << std::endl;
// for ii in range(1, len(roadsSetList)):
// cur = 0
// for aa in roadsSetList[ii]:
// cur = aa
// break
// print(zeroth + 1, cur + 1)
std::cout << roadsSetList.size() - 1 << std::endl;
std::set<int>::iterator firstIt = roadsSetList[0].begin();
int first = *(firstIt);
for (long unsigned int ii = 1; ii < roadsSetList.size(); ii++) {
std::set<int>::iterator it = roadsSetList[ii].begin();
std::cout << first + 1 << " " << *(it) + 1 << std::endl;
}
return 0;
}Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 10 2 5 5 6 1 4 6 8 ... |
| correct output |
|---|
| 2 1 2 2 7 |
| user output |
|---|
| 2 1 2 1 7 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 10 10 3 9 6 8 9 10 7 8 ... |
| correct output |
|---|
| 2 1 4 4 5 |
| user output |
|---|
| 2 1 4 1 5 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 10 7 9 1 7 1 3 3 4 ... |
| correct output |
|---|
| 0 |
| user output |
|---|
| 0 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 10 4 8 5 9 4 9 2 7 ... |
| correct output |
|---|
| 1 1 3 |
| user output |
|---|
| 1 1 3 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 10 4 9 2 4 7 10 1 8 ... |
| correct output |
|---|
| 0 |
| user output |
|---|
| 0 |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 7233 22146 94937 96203 6133 10731 98737 99193 ... |
| correct output |
|---|
| 4785 1 2 2 3 3 4 4 5 ... |
| user output |
|---|
| 4785 1 2 1 3 1 4 1 5 ... Truncated |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 92950 93575 24401 88897 41796 99364 47106 50330 ... |
| correct output |
|---|
| 4868 1 2 2 7 7 9 9 15 ... |
| user output |
|---|
| 4868 1 2 1 7 1 9 1 15 ... Truncated |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 15637 76736 79169 98809 4382 86557 73383 77029 ... |
| correct output |
|---|
| 4683 1 9 9 20 20 27 27 28 ... |
| user output |
|---|
| 4683 1 9 1 20 1 27 1 28 ... Truncated |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 47932 66981 86401 99942 4353 27841 60492 67345 ... |
| correct output |
|---|
| 4807 1 6 6 7 7 11 11 12 ... |
| user output |
|---|
| 4807 1 6 1 7 1 11 1 12 ... Truncated |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 6554 44548 76413 98555 5447 59589 70166 74434 ... |
| correct output |
|---|
| 4786 1 2 2 18 18 21 21 27 ... |
| user output |
|---|
| 4786 1 2 1 18 1 21 1 27 ... Truncated |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 100000 1 1 2 |
| correct output |
|---|
| 99998 1 3 3 4 4 5 5 6 ... |
| user output |
|---|
| 99998 1 3 1 4 1 5 1 6 ... Truncated |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 10 9 2 5 5 6 1 4 6 8 ... |
| correct output |
|---|
| 2 1 2 2 7 |
| user output |
|---|
| 2 1 2 1 7 |
