Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <cstring>
  4. #include <queue>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <algorithm>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. int nextInt() {
  13.     int x;
  14.     scanf("%d", &x);
  15.     return x;
  16. }
  17.  
  18. char inp[10000];
  19. string nextString() {
  20.     scanf("%s", inp);
  21.     return inp;
  22. }
  23.  
  24. double nextDouble() {
  25.     double x;
  26.     scanf("%lf", &x);
  27.     return x;
  28. }
  29.  
  30. const int N = 1100;
  31.  
  32. void dfs1(int v, vector<vector<bool> > &adj, vector<bool> &used, vector<int> &order) {
  33.     used[v] = true;
  34.     for (int i = 0; i < adj[v].size(); ++i) {
  35.         if (adj[v][i] && !used[i]) {
  36.             dfs1(i, adj, used, order);
  37.         }
  38.     }
  39.     order.push_back(v);
  40. }
  41.  
  42. void dfs2(int v, vector<vector<bool> > &adj, vector<bool> &used, vector<int> &comp, int curComp) {
  43.     used[v] = true;
  44.     comp[v] = curComp;
  45.     for (int i = 0; i < adj[v].size(); ++i) {
  46.         if (adj[v][i] && !used[i]) {
  47.             dfs2(i, adj, used, comp, curComp);
  48.         }
  49.     }
  50. }
  51.  
  52. int main() {
  53.     int n = nextInt();
  54.     int m = nextInt();
  55.  
  56.     vector<vector<bool> > adj(n, vector<bool>(n, false));
  57.     vector<vector<bool> > badj(n, vector<bool>(n, false));
  58.  
  59.     for (int i = 0; i < m; ++i) {
  60.         int x = nextInt() - 1;
  61.         int y = nextInt() - 1;
  62.         adj[x][y] = true;
  63.         badj[y][x] = true;
  64.     }
  65.  
  66.     vector<int> order;
  67.     vector<bool> used(n, false);
  68.     for (int i = 0; i < n; ++i) {
  69.         if (!used[i]) {
  70.             dfs1(i, adj, used, order);
  71.         }
  72.     }
  73.  
  74.     fill(used.begin(), used.end(), false);
  75.     vector<int> comp(n, -1);
  76.     int compCount = 0;
  77.     for (int t = n - 1; t >= 0; --t) {
  78.         int i = order[t];
  79.         if (!used[i]) {
  80.             dfs2(i, badj, used, comp, ++compCount);
  81.         }
  82.     }
  83.  
  84.     vector<int> s(compCount, 0);
  85.     for (int i = 0; i < n; ++i) {
  86.         ++s[comp[i]];
  87.     }
  88.     vector<vector<int> > a(compCount, vector<int>(compCount, 0));
  89.     for (int i = 0; i < n; ++i) {
  90.         for (int j = 0; j < n; ++j) {
  91.             if (adj[i][j] && comp[i] != comp[j]) {
  92.                 a[comp[i]][comp[j]] = s[comp[i]];
  93.             }
  94.         }
  95.     }
  96.  
  97.     vector<vector<int> > d(compCount, vector<int>(compCount, 1000000000));
  98.     for (int i = 0; i < a.size(); ++i) {
  99.         dij(i, a, d[i]);
  100.     }
  101.  
  102.     int res = 0;
  103.     for (int i = 0; i < n; ++i) {
  104.         for (int j = 0; j < n; ++j) {
  105.             if (adj[i][j] && d[comp[j]][comp[i]] + s[comp[i]] == w) {
  106.                 ++res;
  107.             }
  108.         }
  109.     }
  110.  
  111.  
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement