abc258(G)


G – Triangle

题意:给定一个邻接矩阵,问有多少个三元组(x, y, z)满足两两顶点之间有一条边直接相连。

该题使用bitset可以快速解决。
首先预处理 bitset b[i], b[i][j] = 1表示有边,否则无边。
然后选中两个点(x, y),且(x, y)之间有边。
b[x] & b[y] 为一个新的bitset,当bitset[z] 表示 x 和 y 同时与 z 有边。
因此最终的答案为:/((/sum_{i = 1}^{n}/sum_{j = 1}^{n}(b[i]/&b[j]).count() * b[i][j]) / 6/)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;
typedef long long ll;
const int N = 3000 + 5;
bitset<N> d[N];
char s[N];
int main() {
    int T = 1;
//    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%s", s + 1);
            for (int j = 1; j <= n; j++) {
                d[i][j] = s[j] - '0';
            }
        }
        ll res = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                res += (d[i] & d[j]).count() * d[i][j];
            }
        }
        printf("%lld/n", res / 6);
    }
    return 0;
}

原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/271087.html

(0)
上一篇 2022年7月3日
下一篇 2022年7月3日

相关推荐

发表回复

登录后才能评论