vortxc's blog

By vortxc, history, 3 years ago, In English

The code below works for me in VScode for 1548A - Паутина лжи, but it doesn't work in codeforces. The output is completely different. Does anyone have any idea on how to get it working again?

// ========== HEADER ==========
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
 
using namespace std;
// ========== HEADER ==========

// ========== SHORTCUTS ==========
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define sorted is_sorted
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
// ========== SHORTCUTS ==========

// ========== HELPERS ==========
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

template<class T> vector<T> combine(vector<T> a, vector<T> b) {
    vector<T> combined;
    trav(i, a) combined.pb(i);
    trav(i, b) combined.pb(i);
    return combined;
}

map<char, int> freq(string s) {
    map<char, int> m;
    trav(c, s) {
        if(c != ' ') ++m[c];
    }
    return m;
}

string join(const vs& parts, const string& delim = "") {
    string res;
    trav(str, parts) {
        res += str+delim;
    }
    return res.substr(0, res.length()-2);
}

vs split(string s, string del = " ") {
    int start = 0;
    int end = s.find(del);
    vs res;
    while (end != -1) {
        res.pb(s.substr(start, end-start));
        start = end + del.size();
        end = s.find(del, start);
    }
    res.pb(s.substr(start, end-start));
    return res;
}

string sliceof(string s, int start, int end) { return s.substr(start, end-start+1); }
// ========== HELPERS ==========

// ========== DEBUGGER ==========
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
// ========== DEBUGGER ==========

// ========== SOLVING ==========
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int MOD = 1000000007;
const char nl = '\n';
const int MXN = 100001;
const int d4i[4] = {1, -1, 0, 0}, d4j[4] = {0, 0, -1, 1};
const int d8i[8] = {1, -1, 0, 0, 1, -1, 1, -1}, d8j[8] = {0, 0, -1, 1, 1, -1, -1, 1};
bool swtch = 1;

vector<int> allVulnerable(map<int, vector<int>> friends) {
    vector<int> vulnerable;
    trav(a, friends) {
        bool is_vulnerable = 1;
        if(a.second.empty()) is_vulnerable = 0;
        trav(b, a.second) {
            if(b <= a.first) {
                is_vulnerable = 0;
                break;
            }
        }
        if(is_vulnerable) vulnerable.pb(a.first);
    }
    return vulnerable;
}

map<int, vector<int>> doProcess(map<int, vector<int>> friends, vector<int> vulnerable) {
    trav(a, friends) {
        trav(b, a.second) {
            if(find(all(vulnerable), b)!=vulnerable.end()) {
                auto it = find(all(a.second), b);
                friends[a.first].erase(it);
            }
        }
    }
    trav(p, vulnerable) friends.erase(p);
    return friends;
}

void solve() {
    int n, m;
    cin >> n >> m;
    map<int, vector<int>> friends;
    FOR(i, 1, n+1) friends[i] = {};
    while(m--) {
        int a, b;
        cin >> a >> b;
        friends[a].pb(b);
        friends[b].pb(a);
    }
    int q;
    cin >> q;
    map<int, vector<int>> fp = friends;
    while(q--) {
        int a, b = 0, c = 0;
        scanf("%d",&a);
        if(a == 1 || a == 2) {
            scanf("%d %d", &b, &c);
            if(a == 1) {
                fp[b].pb(c);
                fp[c].pb(b);
            } else {
                auto pib = find(all(fp[b]), c);
                auto pic = find(all(fp[c]), b);
                fp[b].erase(pib);
                fp[c].erase(pic);
            }
        } else {
            vector<int> vulnerable = allVulnerable(fp);
            while((int) vulnerable.size() > 0) {
                fp = doProcess(fp, vulnerable);
                vulnerable = allVulnerable(fp);
            }
            int remaining = 0;
            trav(a, fp) ++remaining;
            cout << remaining << nl;
            fp = friends;
        }
    }
}
 
int main() {
    cin.tie(0)->sync_with_stdio(0); 
    cin.exceptions(cin.failbit);
    int t = 1;
    // cin >> t;
    while(t--) {
        solve();
    }
	return 0;
}
// ========== SOLVING ==========

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By vortxc, history, 3 years ago, In English

Dear fellow coders, I have solved a few hundred problems on other sites such as HackerRank and LeetCode, but is still don't seem to be improving, even after practicing for a few months now. I still struggle on problems rated 1000 to 1400 so are there any suggestions about improving?

Full text and comments »

  • Vote: I like it
  • -7
  • Vote: I do not like it