Codeforces Gym 100342J Problem J. Triatrip 三元环
题目链接:
http://codeforces.com/gym/100342
题意:
求三元环的个数
题解:
用bitset分别统计每个点的出度的边和入度的边。
枚举每一条边(a,b),计算以b为出度的边的终点构成的点集和以a为入度的边的起点够成的点集的交集,更新答案。
代码:
#include<iostream> #include<cstring> #include<cstdio> #include<bitset> using namespace std; const int maxn = 1555; typedef __int64 LL; bitset<maxn> in[maxn], out[maxn], And; char str[maxn][maxn]; int main() { freopen("triatrip.in", "r", stdin); freopen("triatrip.out", "w", stdout); int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%s", str[i]); LL ans = 0; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++) { if (str[i][j] == '+') { in[j][i] = true; out[i][j] = true; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (str[i][j] == '+') { And = (in[i]) & (out[j]); ans += And.count(); } } } printf("%I64d\n", ans / 3); return 0; }