#### Topic background

Xiao Ming works in company A and Xiao Hong works in company B.

#### Title Description

The employees of the two companies have one characteristic: the employees of one company are of the same sex.

Company a has N employees, including P pairs of friends. Company B has M employees, including Q pairs of friends. A friend's friend must still be a friend.

Each pair of friends is composed of two integers (Xi,Yi), indicating that the numbers of friends are Xi and Yi respectively. Men's numbers are positive and women's numbers are negative. Xiao Ming's number is 1 and Xiao Hong's number is - 1.

As we all know, Xiao Ming and Xiao Hong are friends. Then, please write a program to find out how many couples (including themselves) can be matched by the people known by Xiao Ming and Xiao Hong between the two companies.

#### Input format

The first line of input contains positive integers N,M,P,Q separated by 4 spaces.

Then line P, with two positive integers Xi and Yi in each line.

After the QQ line, there are two negative integers Xi and Yi in each line.

#### Output format

Output a positive integer on each line, indicating the maximum number of couples (including themselves) that can be matched by the people known by Xiao Ming and Xiao Hong.

Almost another parallel query template.

When I first read the question, I felt that there was one place that could cause ambiguity (after all, it was not the official question, and the description was not so perfect. It was normal): please write a program to find out how many couples could be matched between the two companies through the people known by Xiao Ming and Xiao Hong.

Two companies? Can a company digest it internally?

Later, I found that it was my own problem

All employees in a company are of the same sex.

In that case, it's okay.

Algorithm analysis: query the set template and use STL map to complete the negative subscript implementation of the array.

code:

#include <bits/stdc++.h> using namespace std; map<int, int> f; int find(int a) { if (f[a] != a) return f[a] = find(f[a]); else return a; } int hb(int a, int b) { f[find(b)] = find(a); } int main() { int n, m, p, q; cin >> n >> m >> p >> q; for (int i = -m; i <= n; i++) { if (i == 0) continue; f[i] = i; } f[-1] = f[1]; int x, y; while (p--) { cin >> x >> y; hb(x, y); } while (q--) { cin >> x >> y; hb(x, y); } int girl = 0, boy = 0; for (int i = -m; i < 0; i++) { if (find(i) == 1) girl++; } for (int i = 1; i <= n; i++) { if (find(i) == 1) boy++; } cout << min(boy, girl); return 0; }

The final statistics is to output the smaller total number of men and women among all Xiaoming and Xiaohong's friends.

Example OK, remote

#### There is a problem!!!

There is no problem with this implementation (at least I don't find it now. If it is solved in the future, the solution also has such a solution:

Solution P2078 [friend] - SSL_XXY_BlackCloud blog - Valley blog

Trumpet test, problem solving, this code can really AC

Well, in this case, let's change our thinking: replace the map with an array. The boys' storage location remains unchanged, and the girls' location becomes the absolute value of the number (because the number is negative, it is the opposite number) plus n

AC Code:

#include <bits/stdc++.h> using namespace std; int f[1000001]; int find(int a) { if (f[a] != a) { return f[a] = find(f[a]); } else { return a; } } int hb(int a, int b) { f[find(b)] = find(a); } int main() { int n, m, p, q; cin >> n >> m >> p >> q; for (int i = 1; i <= n + m; i++) { f[i] = i; } f[n + 1] = f[1]; int x, y; while (p--) { cin >> x >> y; hb(x, y); } while (q--) { cin >> x >> y; hb(-x + n, -y + n); } int girl = 0, boy = 0; for (int i = 1; i <= n; i++) { if (find(i) == find(1)) boy++; } for (int i = n + 1; i <= n + m; i++) { if (find(i) == find(1)) girl++; } cout << min(boy, girl); return 0; }

Comparing the three times, we can see that in fact, map has no advantages in time and space... So maybe the problem solution optimization is a little better, and then my timeout / super memory?

So in the future, unless you encounter the mapping of string to other types, you'd better be honest with the array