Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

Problem J. Triatrip

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100342/attachments

Description

The travel agency “Four Russians” is offering the new service for their clients. Unlike other agencies that only suggest one-way or roundtrip for airline tickets to their customers, “Four Russians” offers the brand new idea — triatrip. Triatrip traveler starts in some city A, flies to some city B, then flies to some city C, and returns to the city A.
Now the managers of the agency started to wonder, how many different triatrips they can offer to their customers. Given a map of all possible flights, help them to find that out.

Input

The first line of the input file contains two integer numbers n — the number of cities that are served by airlines that agree to sell their tickets via the agency (3 ≤ n ≤ 1500). The following n lines contain a sequence of n characters each — the j-th character of the i-th line is ‘+’ if it is possible to fly from the i-th city to the j-th one, and ‘-’ if it is not. The i-th character of the i-th line is ‘-’.

Output

Output one integer number — the number of triatrips that the agency can offer to its customers.

Sample Input

4
--+-
+--+
-+--
--+-

Sample Output

2

HINT

题意

给出邻接矩阵,有向图,找出三元环的个数

题解

bitset 暴力枚举一条边,出度入度的集合用到bitset

代码:

 1 //作者:1085422276
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <ctime>
 6 #include <iostream>
 7 #include <algorithm>
 8 #include <set>
 9 #include <vector>
10 #include <sstream>
11 #include <queue>
12 #include <typeinfo>
13 #include <fstream>
14 #include<bits/stdc++.h>
15 #include <map>
16 #include <stack>
17 typedef long long ll;
18 using namespace std;
19 const int inf = 10000000;
20 inline ll read()
21 {
22     ll x=0,f=1;
23     char ch=getchar();
24     while(ch<‘0‘||ch>‘9‘)
25     {
26         if(ch==‘-‘)f=-1;
27         ch=getchar();
28     }
29     while(ch>=‘0‘&&ch<=‘9‘)
30     {
31         x=x*10+ch-‘0‘;
32         ch=getchar();
33     }
34     return x*f;
35 }
36 ll exgcd(ll a,ll b,ll &x,ll &y)
37 {
38     ll temp,p;
39     if(b==0){
40         x=1;y=0;
41         return a;
42     }
43     p=exgcd(b,a%b,x,y);
44     temp=x;x=y;y=temp-(a/b)*y;
45     return p;
46 }
47 //*******************************
48 #define N 1510
49 bitset<N> in[N],out[N],he;
50   char mp[1501][1501];
51 int main()
52 {
53
54     freopen("triatrip.in","r",stdin);
55     freopen("triatrip.out","w",stdout);
56      int n;
57      n=read();
58     for(int i=1;i<=n;i++)
59         scanf("%s",mp[i]);
60     for(int i=1;i<=n;i++)
61     {
62         for(int j=1;j<=n;j++)
63         {
64             if(i==j)continue;
65             if(mp[i][j-1]==‘+‘)
66             {
67                 in[i][j]=true;
68                 out[j][i]=true;
69             }
70         }
71     }
72     ll ans=0;
73     for(int i=1;i<=n;i++)
74     {
75         for(int j=1;j<=n;j++)
76         {
77             if(i==j)continue;
78             if(mp[i][j-1]==‘+‘)
79                 he=(in[j])&(out[i]),ans+=he.count();
80         }
81     }
82     cout<<ans/3<<endl;
83     return 0;
84 }
时间: 2024-11-05 20:35:03

Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset的相关文章

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;

Gym - 100342J:Triatrip(Bitset加速求三元环的数量)

题意:求有向图里面有多少个三元环. 思路:枚举起点A,遍历A可以到的B,然后求C的数量,C的数量位B可以到是地方X集合,和可以到A的地方Y集合的交集(X&Y). B点可以枚举,也可以遍历.(两种都试过,区别不大.) 枚举代码: #include<cstdio> #include<bitset> #include<cstdlib> #include<cstring> #include<iostream> #include<algori

Gym - 100342J Triatrip (bitset求三元环个数)

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 #incl

Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The new cottage settlement is organized near the capital of Flatland. The construction company that is building the settl

Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造

Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Andrea is a famous science fiction writer, who runs masterclasses for her beloved readers. The most popular one is the

Codeforces Gym 100610 Problem E. Explicit Formula 水题

Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Consider 10 Boolean variables x1, x2, x3, x4, x5, x6, x7, x8, x9, and x10. Consider all pairs and triplets of distinct variables amon

Codeforces Gym 100342C Problem C. Painting Cottages 暴力

Problem C. Painting CottagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The new cottage settlement is organized near the capital of Flatland. The construction company that is building the sett

Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description A group of k students from Cooking University living in the campus decided that each day of the semester one of them will p

Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description Andrew is having a hard time preparing his 239-th contest for Petrozavodsk. This time the solution to the problem is based on Di