Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <stdlib.h>
- #include <cstring>
- using namespace std;
- using ll = long long;
- using ull = unsigned long long;
- int cur_component;
- int number_of_ribs(vector<vector<int>> &graph) {
- int result = 0;
- for (int i = 0; i < graph.size(); i++) {
- result += graph[i].size();
- }
- return result;
- }
- void dfs(vector<int> &component, vector<vector<int>> &graph, int v) {
- component[v] = cur_component;
- for (int i = 0; i < graph[v].size(); i++) {
- if (component[graph[v][i]] == 0) {
- dfs(component, graph, graph[v][i]);
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int N, M;
- cin >> N >> M;
- vector<vector<int>> graph(N);
- for (int i = 0; i < M; i++) {
- char part[] = "";
- cin >> part[0];
- int beg, end;
- cin >> beg >> end;
- if (strcmp("F", part) == 0) {
- graph[beg - 1].push_back(end - 1);
- graph[end - 1].push_back(beg - 1);
- }
- else {
- for (int j = 0; j < graph[beg - 1].size(); j++) {
- if (graph[beg - 1][j] == end - 1) {
- graph[beg - 1].erase(graph[beg - 1].begin() + j);
- }
- }
- for (int j = 0; j < graph[end - 1].size(); j++) {
- if (graph[end - 1][j] == beg - 1) {
- graph[end - 1].erase(graph[end - 1].begin() + j);
- }
- }
- }
- vector<int> visited(N, 0);
- vector<int> component(N, 0);
- cur_component = 1;
- for (int j = 0; j < N; j++) {
- if (component[j] != 0) continue;
- dfs(component, graph, j);
- cur_component++;
- }
- if ((cur_component - 1 == 1) && (number_of_ribs(graph) / 2 == N - 1)) {
- cout << "YES\n";
- }
- else {
- cout << "NO\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement