|
| 1 | +#include<bits/stdc++.h> |
| 2 | + |
| 3 | +#define watch(x) cout<<(#x)<<" is "<<(x)<<"\n" |
| 4 | +#define print(x) cout<<x<<"\n" |
| 5 | +#define For(i, x, y) for(i = x; i < y; i++) |
| 6 | +typedef std::vector<int> vi; |
| 7 | +typedef std::vector<std::vector<int>> vvi; |
| 8 | +typedef long long ll; |
| 9 | +int INF = INT32_MIN; |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +void fast() { |
| 13 | + ios::sync_with_stdio(false); |
| 14 | + cin.tie(NULL); |
| 15 | + cout.tie(NULL); |
| 16 | +} |
| 17 | + |
| 18 | +struct Node { |
| 19 | + int v; |
| 20 | + float x, y; |
| 21 | +}; |
| 22 | +struct Edge { |
| 23 | + Node a, b; |
| 24 | + float w; |
| 25 | +}; |
| 26 | + |
| 27 | +struct United { |
| 28 | + int rank = 1; |
| 29 | + int parent; |
| 30 | + string name; |
| 31 | + int index; |
| 32 | + int members = 1; |
| 33 | +}; |
| 34 | + |
| 35 | +void doIt(United united, United united1); |
| 36 | + |
| 37 | +vector<United> uniteds; |
| 38 | + |
| 39 | +float diss(Edge node) { |
| 40 | + return sqrt((node.a.x - |
| 41 | + node.b.x) * (node.a.x - node.b.x) |
| 42 | + + (node.a.y - node.b.y) * (node.a.y - node.b.y)); |
| 43 | +} |
| 44 | + |
| 45 | +bool dis(Edge node, Edge node1) { |
| 46 | + return sqrt((node.a.x - |
| 47 | + node.b.x) * (node.a.x - node.b.x) |
| 48 | + + (node.a.y - node.b.y) * (node.a.y - node.b.y)) |
| 49 | + < sqrt((node1.a.x - |
| 50 | + node1.b.x) * (node1.a.x - node1.b.x) |
| 51 | + + (node1.a.y - node1.b.y) * |
| 52 | + (node1.a.y - node1.b.y)); |
| 53 | +} |
| 54 | + |
| 55 | +bool edge_cmp(Edge e1, Edge e2) { |
| 56 | + return dis(e1, e2); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +int find(int a) { |
| 61 | + if (a != uniteds[a].parent) |
| 62 | + uniteds[a].parent = find(uniteds[a].parent); |
| 63 | + return uniteds[a].parent; |
| 64 | +} |
| 65 | + |
| 66 | +int _union(int a, int b) { |
| 67 | + int a_root = find(a); |
| 68 | + int b_root = find(b); |
| 69 | + if(a_root == b_root){ |
| 70 | + return uniteds[a_root].members; |
| 71 | + } |
| 72 | + if (uniteds[a_root].rank < uniteds[b_root].rank) { |
| 73 | + uniteds[a_root].parent = b_root; |
| 74 | + return uniteds[b_root].members += uniteds[a_root].members; |
| 75 | + } else if (uniteds[a_root].rank > uniteds[b_root].rank) { |
| 76 | + uniteds[b_root].parent = a_root; |
| 77 | + return uniteds[a_root].members += uniteds[b_root].members; |
| 78 | + } else { |
| 79 | + uniteds[b_root].parent = a_root; |
| 80 | + uniteds[a_root].rank++; |
| 81 | + return uniteds[a_root].members += uniteds[b_root].members; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +int main() { |
| 86 | + fast(); |
| 87 | + int t, n; |
| 88 | + cin >> t; |
| 89 | + while (t--) { |
| 90 | + cin >> n; |
| 91 | + uniteds.clear(); |
| 92 | + set<string> names; |
| 93 | + map<string,United> map; |
| 94 | + vector<pair<string,string>> pairs; |
| 95 | + for (int i = 0; i < n; ++i) { |
| 96 | + string name1, name2; |
| 97 | + cin>>name1>>name2; |
| 98 | + pairs.emplace_back(name1,name2); |
| 99 | + names.insert(name1); |
| 100 | + names.insert(name2); |
| 101 | + } |
| 102 | + int x= 0; |
| 103 | + for (const auto& j : names) { |
| 104 | + United united1; |
| 105 | + united1.name = j; |
| 106 | + united1.index = x; |
| 107 | + united1.parent = x; |
| 108 | + x++; |
| 109 | + uniteds.emplace_back(united1); |
| 110 | + map.insert(make_pair(j, united1)); |
| 111 | + } |
| 112 | + for (int k = 0; k < pairs.size(); ++k) { |
| 113 | + string name1 = pairs.at(k).first; |
| 114 | + string name2 = pairs.at(k).second; |
| 115 | + United united1 = map.at(name1); |
| 116 | + United united2 = map.at(name2); |
| 117 | + cout<<_union(united1.index, united2.index)<<"\n"; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments