Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. /* Gabriel Oliveira Taumaturgo
  2.    14/0140522
  3.    */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. vector<int> tempos;
  12. int tempo = 1;
  13. int visited[10000];
  14. int tempo_de_visita[10000];
  15. int tempo_de_saida[10000];
  16. int start = 5;
  17. map <int,vector<int>> graph;
  18. map <int,vector<int>> grapht;
  19.  
  20.  
  21. void dfs(int u){
  22.  
  23.     tempo_de_visita[u] = tempo;
  24.     tempo++;
  25.  
  26.     visited[u] = 0;
  27.  
  28.     for(int v: graph[u]){
  29.         grapht[v].push_back(u);
  30.         if(visited[v]){
  31.             dfs(v);
  32.         }
  33.  
  34.     }
  35.     tempo_de_saida[u] = tempo;
  36.     tempo++;
  37. }
  38. void dfst(int u){
  39.  
  40.     printf("%d ",u );
  41.     visited[u] = 0;
  42.  
  43.     for(int v: grapht[u]){
  44.         if(visited[v]){
  45.             dfst(v);
  46.         }
  47.  
  48.     }
  49.  
  50. }
  51.  
  52. int main(){
  53.  
  54.     graph[1].push_back(2);
  55.     graph[2].push_back(3);
  56.     graph[2].push_back(5);
  57.     graph[2].push_back(6);
  58.     graph[3].push_back(4);
  59.     graph[3].push_back(7);
  60.     graph[4].push_back(3);
  61.     graph[4].push_back(8);
  62.     graph[5].push_back(1);
  63.     graph[5].push_back(6);
  64.     graph[6].push_back(7);
  65.     graph[7].push_back(6);
  66.     graph[7].push_back(8);
  67.     graph[8].push_back(8);
  68.  
  69.     for (int i = 0; i < 10000; ++i)
  70.     {
  71.         visited[i] = 1;
  72.     }
  73.     for(pair<const int,vector<int>>& p: graph){
  74.         if (visited[p.first])
  75.         {
  76.             dfs(p.first);
  77.         }
  78.     }
  79.     vector<pair<int,int>> indicetempo;
  80.     for (int i = 1; i < 9; ++i)
  81.         indicetempo.push_back(make_pair(tempo_de_saida[i],i));
  82.  
  83.    
  84.     sort(indicetempo.begin(),indicetempo.end(), greater<pair<int,int>>());
  85.  
  86.    
  87.  
  88.     for (int i = 0; i < 10000; ++i)
  89.     {
  90.         visited[i] = 1;
  91.     }
  92.  
  93.     for(pair<int,int>& p: indicetempo){
  94.         if (visited[p.second])
  95.         {
  96.             dfst(p.second);
  97.             printf("\n");
  98.         }
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement