Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <stdlib.h>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8. using ll = long long;
  9. using ull = unsigned long long;
  10. int cur_component;
  11.  
  12.  
  13.  
  14.  
  15. int number_of_ribs(vector<vector<int>> &graph) {
  16.     int result = 0;
  17.     for (int i = 0; i < graph.size(); i++) {
  18.         result += graph[i].size();
  19.     }
  20.     return result;
  21. }
  22.  
  23. void dfs(vector<int> &component, vector<vector<int>> &graph, int v) {
  24.     component[v] = cur_component;
  25.     for (int i = 0; i < graph[v].size(); i++) {
  26.         if (component[graph[v][i]] == 0) {
  27.             dfs(component, graph, graph[v][i]);
  28.         }
  29.     }
  30. }
  31.  
  32. int main() {
  33.     ios_base::sync_with_stdio(false);
  34.     cin.tie(nullptr);
  35.  
  36.     int N, M;
  37.     cin >> N >> M;
  38.     vector<vector<int>> graph(N);
  39.  
  40.     for (int i = 0; i < M; i++) {
  41.         char part;
  42.         cin >> part;
  43.         int beg, end;
  44.         cin >> beg >> end;
  45.  
  46.         if (part == 'F') {
  47.             graph[beg - 1].push_back(end - 1);
  48.             graph[end - 1].push_back(beg - 1);
  49.         }
  50.         else {
  51.             for (int j = 0; j < graph[beg - 1].size(); j++) {
  52.                 if (graph[beg - 1][j] == end - 1) {
  53.                     graph[beg - 1].erase(graph[beg - 1].begin() + j);
  54.                 }
  55.             }
  56.             for (int j = 0; j < graph[end - 1].size(); j++) {
  57.                 if (graph[end - 1][j] == beg - 1) {
  58.                     graph[end - 1].erase(graph[end - 1].begin() + j);
  59.                 }
  60.             }
  61.         }
  62.  
  63.         vector<int> visited(N, 0);
  64.         vector<int> component(N, 0);
  65.         cur_component = 1;
  66.        
  67.         for (int j = 0; j < N; j++) {
  68.             if (component[j] != 0) continue;
  69.             dfs(component, graph, j);
  70.             cur_component++;
  71.         }
  72.  
  73.         if ((cur_component - 1 == 1) && (number_of_ribs(graph) / 2 == N - 1)) {
  74.             cout << "YES\n";
  75.         }
  76.         else {
  77.             cout << "NO\n";
  78.         }
  79.     }
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement