Advertisement
Guest User

xxxx

a guest
Nov 21st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. vector<int> grafo[10050];
  7. int vis[10050];
  8. int tot;
  9.  
  10. void dfs(int v){
  11.        
  12.     vis[v] = 1;
  13.  
  14.     //cout << "Visitando os filhos de " << v << "\n";
  15.     for(int i = 0; i < grafo[v].size(); i++){
  16.         int atual = grafo[v][i];
  17.  
  18.         if(vis[atual] == 0){
  19.             //cout << "Atual " << atual << "\n";
  20.             dfs(atual);
  21.             tot += 2;
  22.         }
  23.     }  
  24. }
  25.  
  26. int main(){
  27.  
  28.     int t, n, a, v;
  29.  
  30.     cin >> t;
  31.  
  32.     while(t--){
  33.          
  34.          vis[10050] = {0}, tot = 0;
  35.          cin >> n >> a >> v;
  36.            
  37.          for(int i = 0; i < v; i++){
  38.             int o, d;            
  39.             cin >> o >> d;
  40.  
  41.             grafo[o].push_back(d);
  42.             grafo[d].push_back(o);
  43.          }  
  44.  
  45.          dfs(n);
  46.        
  47.          cout << tot << "\n";          
  48.     }
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement