标签:get algorithm href stack net ack freopen printf include
https://vjudge.net/problem/Gym-100342J
题意:
给出一个邻接矩阵有向图,求图中的三元环的个数。
思路:
利用bitset暴力求解,记得最后需要/3。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #include<stack> 7 #include<queue> 8 #include<cmath> 9 #include<map> 10 #include<set> 11 #include<bitset> 12 using namespace std; 13 typedef long long ll; 14 typedef pair<int,int> pll; 15 const int INF = 0x3f3f3f3f; 16 const int maxn=1500+5; 17 18 char s[maxn]; 19 bitset<maxn> bit1[maxn],bit2[maxn]; 20 21 int main() 22 { 23 //freopen("in.txt","r",stdin); 24 freopen("triatrip.in","r",stdin); 25 freopen("triatrip.out","w",stdout); 26 int n; 27 scanf("%d",&n); 28 for(int i=0;i<n;i++) 29 { 30 scanf("%s",s); 31 for(int j=0;j<n;j++) 32 { 33 if(s[j]==‘+‘) bit1[i].set(j),bit2[j].set(i); 34 //bit1记录i能够到达的点,bit2记录能够到达j的点 35 } 36 } 37 ll ans = 0; 38 for(int i=0;i<n;i++) 39 { 40 for(int j=0;j<n;j++) 41 { 42 if(bit1[i][j]) 43 { 44 ans+=(bit1[j]&bit2[i]).count(); 45 } 46 } 47 } 48 printf("%lld\n",ans/3); 49 return 0; 50 }
Gym - 100342J Triatrip (bitset求三元环个数)
标签:get algorithm href stack net ack freopen printf include
原文地址:http://www.cnblogs.com/zyb993963526/p/7482086.html