Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <utility>
- using namespace std;
- struct vrcholInfo {
- uint32_t id;
- string str;
- };
- struct vrcholComparator {
- bool operator() (const vrcholInfo& lhs, const vrcholInfo& rhs) const {
- return lhs.str.compare(rhs.str) < 0;
- }
- };
- set<vrcholInfo, vrcholComparator> strings;
- uint32_t* components;
- vector<uint32_t>* componentElements;
- uint32_t vertexID = 0;
- auto citajMesto(vrcholInfo &mesto) {
- cin >> mesto.str;
- mesto.id = vertexID;
- auto resultPair = strings.insert(mesto);
- if (resultPair.second)
- ++vertexID;
- return resultPair;
- }
- void presunComponentList(uint32_t lhs, uint32_t rhs) {
- for (uint32_t i = 0; i < componentElements[rhs].size(); ++i) {
- components[componentElements[rhs][i]] = lhs;
- componentElements[lhs].push_back(componentElements[rhs][i]);
- }
- }
- int main() {
- uint32_t n;
- cin >> n;
- components = new uint32_t[n];
- componentElements = new vector<uint32_t>[n];
- uint32_t i = 0;
- while (i < n) {
- components[i] = i;
- componentElements[i].push_back(i);
- ++i;
- }
- vrcholInfo city1, city2;
- while (n--) {
- uint32_t city1ID = citajMesto(city1).first->id, city2ID = citajMesto(city2).first->id;
- if (components[city1ID] == components[city2ID])
- cout << "0" << endl;
- else {
- cout << (componentElements[components[city1ID]].size() * componentElements[components[city2ID]].size()) << endl;
- if (componentElements[components[city1ID]].size() < componentElements[components[city2ID]].size()) presunComponentList(components[city2ID], components[city1ID]);
- else presunComponentList(components[city1ID], components[city2ID]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement