Advertisement
Guest User

Untitled

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