Advertisement
Guest User

CAPCITY - Capital City

a guest
Jul 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. # include <iostream>
  2. # include <vector>
  3. # include <algorithm>
  4. # define ll long long
  5.   using namespace std;
  6.   ll index, kol_compo;
  7.   ll color[100100], low_link[100100], id[100100], in_srr[100100], pred[100100];
  8.   vector< ll > srr, tops;
  9.   vector< ll > Gr[100100], new_Gr[100100], from[100100], compo[100100];
  10.   void way(ll v)
  11.   {
  12.       color[v] = 1;
  13.       for(ll x : Gr[v]){
  14.         if(color[x] == 0) way(x);
  15.       }
  16.       for(ll x : from[v]){
  17.         if(color[x] == 0) way(x);
  18.       }
  19.   }
  20.   void Tarjan(ll v)
  21.   {
  22.       color[v] = 1;
  23.       low_link[v] = index;
  24.       id[v] = index;
  25.       index++;
  26.       srr.push_back(v);
  27.       in_srr[v] = 1;
  28.       for(ll x : Gr[v]){
  29.         if(color[x] == 0){
  30.             Tarjan(x);
  31.             low_link[v] = min(low_link[v], low_link[x]);
  32.         }
  33.         else if(in_srr[x] == 1){
  34.             low_link[v] = min(low_link[v], id[x]);
  35.         }
  36.       }
  37.       if(id[v] == low_link[v]){
  38.         kol_compo++;
  39.         int x;
  40.         do {
  41.             x = srr.back();
  42.             srr.pop_back();
  43.             in_srr[x] = 0;
  44.             compo[kol_compo].push_back(x);
  45.             pred[x] = kol_compo;
  46.         } while(x != v);
  47.       }
  48.   }
  49.   void top_sort(ll v)
  50.   {
  51.       color[v] = 1;
  52.       for(ll x : new_Gr[v]){
  53.         if(color[x] == 0) top_sort(x);
  54.       }
  55.       tops.push_back(v);
  56.   }
  57.   int main()
  58.   {
  59.       ios_base::sync_with_stdio(0);
  60.       cin.tie(NULL);
  61.       cout.tie(NULL);
  62.       ll n, m, a, b, i;
  63.       cin >> n >> m;
  64.       for(i = 1; i <= m; i++){
  65.         cin >> a >> b;
  66.         Gr[a].push_back(b);
  67.         from[b].push_back(a);
  68.       }
  69.  
  70.       /// Step 1 : Chek all nodes - in one component unorder Graph
  71.       fill(color + 1, color + 1 + n, 0);
  72.       way(1);
  73.       for(i = 1; i <= n; i++){
  74.         if(color[i] == 0){
  75.             cout << "0";
  76.             return 0;
  77.         }
  78.       }
  79.  
  80.       /// Step 2 : Find all strong components
  81.       /// Step 3 : Fill pred[ ] where pred[x] = y, mean x - in strong component number y
  82.       index = 0;
  83.       fill(color + 1, color + 1 + n, 0);
  84.       for(i = 1; i <= n; i++){
  85.         if(color[i] == 0) Tarjan(i);
  86.       }
  87.  
  88.       /// Step 4 : Make new Graph
  89.       for(i = 1; i <= kol_compo; i++){
  90.         for(ll v : compo[i]){
  91.             for(ll x : Gr[v]){
  92.                 if(pred[x] != i) new_Gr[i].push_back(pred[x]);
  93.             }
  94.         }
  95.       }
  96.  
  97.       /// Step 5 : Make nodes in new_Graph topological sort
  98.       fill(color + 1, color + 1 + n, 0);
  99.       for(i = 1; i <= n; i++){
  100.         if(color[i] == 0) top_sort(i);
  101.       }
  102.       ///reverse(tops.begin(), tops.end());
  103.  
  104.       /// Step 6 : sort Answer is first node in tops
  105.       sort(compo[tops[0]].begin(), compo[tops[0]].end());
  106.  
  107.       /// Step 7 : Answer is first node in tops
  108.       cout << compo[tops[0]].size() << '\n';
  109.       for(ll v : compo[tops[0]]){
  110.         cout << v << " ";
  111.       }
  112.  
  113.       return 0;
  114.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement