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; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步