Advertisement
Manioc

vertex cover

Jun 13th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 100007
  3.  
  4. using namespace std;
  5.  
  6. vector<int> graph[MAX], is_leaf;
  7. int vis[MAX], parent[MAX], ans[MAX], n, m;
  8.  
  9. int dfs(int no){
  10.     int leaf = true;
  11.     //cout << no << endl;
  12.     int cont = 0;
  13.     for(int i = 0; i < graph[no].size(); i++){
  14.         int filho = graph[no][i];
  15.         if(!vis[filho]){
  16.             leaf = false;
  17.             vis[filho] = true;
  18.             cont = cont || dfs(filho);
  19.         }
  20.     }
  21.     cout << no << " " << cont << endl;
  22.     ans[no] = cont;
  23.     if(leaf) return 1;
  24.     return !cont;
  25. }
  26.  
  27.  
  28. int main(){
  29.     scanf("%d %d", &n, &m);
  30.     for(int i = 0; i < m; i++){
  31.         int a, b; scanf("%d %d", &a, &b);
  32.         graph[a].push_back(b);
  33.         graph[b].push_back(a);
  34.     }
  35.  
  36.     // cout << "-----------\n";
  37.     // for(int i = 1; i <= n; i++){
  38.     //     for(int j = 0; j < graph[i].size(); j++){
  39.     //         cout << graph[i][j] << " ";
  40.     //     }
  41.     //     cout << endl;
  42.     // }
  43.     // cout << "-----------\n";
  44.     vis[1] = true;
  45.     dfs(1);
  46.  
  47.  
  48.     for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  49.     cout << endl;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement