当前位置:   article > 正文

Gym-100342J Triatrip(求有向图三元环个数+bitset应用)_有向图环路个数怎么计算

有向图环路个数怎么计算

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

大致题意:

给出一个邻接矩阵表示的有向图,求三元环个数。

思路:

枚举每个点A,然后再枚举当前点能够到达的点B,将所有能够到达点A的点组成一个集合,B点能够到达的所有的点组成另一个集合,然后两个集合取交集,集合大小就是必走A->B边的三元环个数,最后ans/3去除重复统计的便是答案。在存储两个集合时用到bitset,求交集时可以进行一个常数优化。时间复杂度大致为O(n^3),但网上有人说bitset之间位运算优化了大致为机器位数(32)的常数,所以这个复杂度能够解决当前问题。


代码:

  1. #include <bits/stdc++.h>
  2. #define LL long long
  3. using namespace std;
  4. const int maxn = 1505;
  5. bitset<maxn> bt[maxn][2];
  6. vector<int> vt[maxn];
  7. char s[maxn];
  8. int main()
  9. {
  10. freopen("triatrip.in", "r", stdin);
  11. freopen("triatrip.out", "w", stdout);
  12. int n;
  13. scanf("%d", &n);
  14. for(int i = 1; i <= n; ++i)
  15. bt[i][0].reset(), bt[i][1].reset(), vt[i].clear();
  16. for(int i = 1; i <= n; ++i)
  17. {
  18. scanf("%s", s+1);
  19. for(int j = 1; j <= n; ++j)
  20. if(s[j] == '+')
  21. {
  22. bt[i][1].set(j), bt[j][0].set(i);
  23. vt[i].push_back(j);
  24. }
  25. }
  26. LL ans = 0;
  27. for(int i = 1; i <= n; ++i)
  28. for(int j = 0; j < vt[i].size(); ++j)
  29. ans += 1ll*(bt[i][0] & bt[vt[i][j]][1]).count();
  30. printf("%lld\n", ans/3);
  31. return 0;
  32. }


补充:

bitset()

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int inf = 0x3f3f3f3f;
  4. const int _inf = 0x7fffffff;
  5. int main()
  6. {
  7. cout << inf << endl;
  8. cout << _inf << endl;
  9. cout << "--------------------------------" << endl;
  10. string str = "1111111000000011001101";
  11. //从string对象读入位集的顺序是从右向左
  12. //从string中下标从5开始的6个字符初始化,整个字符串初始化
  13. bitset<32> bt1(str, 5, 6), _bt1(str);
  14. //跟二进制类似,从右向左即从低位向高位
  15. cout << bt1 << endl;
  16. cout << _bt1 << endl;
  17. //从低位开始输出
  18. for(int i = 0; i <= 31; ++i)
  19. cout << bt1[i];
  20. cout << endl;
  21. bitset<16> bt2(16);
  22. bitset<32> bt3(0xffff);
  23. cout << bt2 << endl;
  24. cout << bt3 << endl;
  25. cout << "--------------------------------" << endl;
  26. bitset<32> bt4((string)"1101");
  27. bitset<32> bt5((string)"1111");
  28. cout << bt4 << endl;
  29. cout << bt4.count() << endl;
  30. //可以像二进制一样进行位运算。优化了常数(32)
  31. cout << (~bt4) << endl;
  32. cout << (bt4 & bt5) << endl;
  33. cout << (bt4 | bt5) << endl;
  34. cout << (bt4 ^ bt5) << endl;
  35. cout << (bt4 << 1) << endl;
  36. cout << (bt4 >> 1) << endl;
  37. return 0;
  38. }

其它操作:



继续加油~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/64595
推荐阅读
相关标签
  

闽ICP备14008679号