第一次接触
https://www.acwing.com/problem/content/1634/

思路:
枚举,但要注意一些细节。
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 310;
int n, m;
unordered_map<string, int> mp;
string num[N];
int id;
bool g[N][N];
vector<int> boys, girls;
int main()
{
scanf("%d%d", &n, &m);
char as[N], bs[N];
while (m -- )
{
string a, b;
scanf("%s%s", as, bs);
a = as, b = bs;
string x = a, y = b;
if (x.size() == 5) x = x.substr(1);
if (y.size() == 5) y = y.substr(1);
if (mp.count(x) == 0) mp[x] = ++ id, num[id] = x;
if (mp.count(y) == 0) mp[y] = ++ id, num[id] = y;
int px = mp[x], py = mp[y];
g[px][py] = g[py][px] = true;
if (a[0] != '-') boys.push_back(px);
else girls.push_back(px);
if (b[0] != '-') boys.push_back(py);
else girls.push_back(py);
}
sort(boys.begin(), boys.end());
boys.erase(unique(boys.begin(), boys.end()), boys.end());
sort(girls.begin(), girls.end());
girls.erase(unique(girls.begin(), girls.end()), girls.end());
int k;
scanf("%d", &k);
while (k -- )
{
vector<pair<string, string>> res;
string x, y;
scanf("%s%s", as, bs);
x = as, y = bs;
vector<int> p = boys, q = boys;
if (x[0] == '-') p = girls, x = x.substr(1);
if (y[0] == '-') q = girls, y = y.substr(1);
int a = mp[x], b = mp[y];
for (int c : p)
for (int d : q)
{
if (c != a && c != b && d != a && d != b && g[a][c] && g[c][d] && g[d][b])
res.push_back({num[c], num[d]});
}
sort(res.begin(), res.end());
printf("%d\n", res.size());
for (auto p : res)
printf("%s %s\n", p.first.c_str(), p.second.c_str());
}
return 0;
}

浙公网安备 33010602011771号