跳到正文

题解

题解:P10996 【MX-J3-T3】Tuple

#SUSPENDED#约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P10996

版本

  1. v1当前

为了解决这个问题,我们需要找到所有满足条件的四元组 (a,b,c,d)​,其中 1≤a<b<c<d≤n​,并且在给定的 m​ 个三元组中存在以下四个三元组:(a,b,c)​、(a,b,d)​、(a,c,d)​、(b,c,d)​。

我们可以使用以下步骤来解决这个问题:

读取输入数据:读取 n​ 和 m​,以及 m​ 个三元组。 存储三元组:使用一个数据结构(如 ​std::unordered_set​ 或 ​std::vector​)来存储所有的三元组。 枚举四元组:枚举所有可能的四元组 (a,b,c,d)​,并检查是否存在所需的四个三元组。 计数符合条件的四元组:如果一个四元组满足条件,则计数加一。 输出结果:输出符合条件的四元组的数量

代码如下

C++
#include <iostream>
#include <vector>
#include <unordered_set>
#include <tuple>

using namespace std;

using Triple = tuple<int, int, int>;

int main() {
    int n, m;
    cin >> n >> m;

    vector<Triple> triples;
    for (int i = 0; i < m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        triples.emplace_back(u, v, w);
    }

    unordered_set<Triple> tripleSet(triples.begin(), triples.end());

    int count = 0;
    for (int a = 1; a <= n; ++a) {
        for (int b = a + 1; b <= n; ++b) {
            for (int c = b + 1; c <= n; ++c) {
                for (int d = c + 1; d <= n; ++d) {
                    if (tripleSet.count(make_tuple(a, b, c)) &&
                        tripleSet.count(make_tuple(a, b, d)) &&
                        tripleSet.count(make_tuple(a, c, d)) &&
                        tripleSet.count(make_tuple(b, c, d))) {
                        ++count;
                    }
                }
            }
        }
    }

    cout << count << endl;

    return 0;
}
原文