【JZOJ4857】Tourist Attractions(Bitset)
题意:给定一个n个点的无向图,求这个图中有多少条长度为4的简单路径。
n<=1500
思路:
1 #include<map> 2 #include<set> 3 #include<cmath> 4 #include<cstdio> 5 #include<vector> 6 #include<cstring> 7 #include<cstdlib> 8 #include<iostream> 9 #include<algorithm> 10 #include<bitset> 11 #define MAXN 1510 12 #define MAXM 4100000 13 using namespace std; 14 15 typedef long long ll; 16 bitset<MAXN> a[MAXN],tmp; 17 18 int head[MAXM],vet[MAXM],next[MAXM],n,i,j,tot; 19 ll size[MAXN]; 20 ll ans; 21 22 void add(int a,int b) 23 { 24 next[++tot]=head[a]; 25 vet[tot]=b; 26 head[a]=tot; 27 } 28 29 int main() 30 { 31 //freopen("jzoj4857.in","r",stdin); 32 //freopen("jzoj4857.out","w",stdout); 33 scanf("%d",&n); 34 for(int i=1;i<=n;i++) 35 { 36 scanf("\n"); 37 for(int j=1;j<=n;j++) 38 { 39 char ch; 40 scanf("%c",&ch); 41 if(ch=='1') 42 { 43 add(i,j); 44 size[i]++; 45 a[i][j]=1; 46 } 47 48 } 49 } 50 for(int i=1;i<=n;i++) 51 for(int e=head[i];e;e=next[e]) 52 { 53 int v=vet[e]; 54 ans+=(size[i]-1)*(size[v]-1); 55 tmp=a[i]&a[v]; 56 ans-=tmp.count(); 57 } 58 printf("%lld\n",ans); 59 return 0; 60 }
null