1549C - Web of Lies - CodeForces Solution


graphs greedy *1400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ordered_set tree<pair<int, int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
#define all(v) (v).begin(),(v).end()
#define r_all(v) (v).rbegin(),(v).rend()
#define s(v) (v).size()
#define endl "\n";
#define NO cout << "NO\n";
#define YES cout << "YES\n";
#define No cout << "No\n";
#define Yes cout << "Yes\n";
#define PF push_front
#define PB push_back
#define MP make_pair
#define F first
#define S second

const int MOD = 1e9 + 7;
const int N = 1e6 + 5;

//_____________________________________________________Mostly_used______________________________________________________

void print_vec(vector<ll>& v){
    for(ll i=0; i<s(v); i++) cout << v[i] << " ";
    cout << endl
}
bool is_sorted(vector<ll>& v){
    bool x = true;
    for(ll i=0; i<s(v)-1; i++) x &= (v[i] <= v[i+1]);
    return x;
}
bool is_palindrome_s(string s) {
    string p = s;
    reverse(all(p));
    if (s == p) return true;
    else return false;
}
bool is_palindrome_n(ll n) {
    ll temp = n, sum =0 ,r;
    while(n>0)
    {
        r=n%10;
        sum = (sum*10)+r;
        n/=10;
    }
    if (temp == sum) return true;
    else return false;
}

//_____________________________________________________/Mostly_used_____________________________________________________



int main() {
    fast
    ll n, m; cin >> n >> m;
    ll ans = n;
    vector<ll> deg(n+1, 0);
    while(m--){
        ll u, v; cin >> u >> v;
        if(u < v) swap(u, v);
        if(!deg[v]) ans--;
        deg[v]++;
    }
    ll q; cin >> q;
    while(q--){
        ll type, u, v; cin >> type;
        if(type == 1){
            cin >> u >> v;
            if(u < v) swap(u, v);
            if(!deg[v]) ans--;
            deg[v]++;
        }
        else if(type == 2){
            cin >> u >> v;
            if(u < v) swap(u, v);
            deg[v]--;
            if(deg[v] == 0) ans++;
        }
        else if(type == 3){
            cout << ans << endl
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff