第一次接触

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;
}
posted @   英雄不问出处c  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
主题色彩