Submission #997698

#TimeUsernameProblemLanguageResultExecution timeMemory
997698SN0WM4NBridges (APIO19_bridges)C++14
0 / 100
877 ms524288 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sort undefined_function // To use stable_sort instead sort 
#define bpc __builtin_popcount
#define ull unsigned long long
#define ld double
#define ll long long
#define mp make_pair
#define F first
#define S second

#pragma GCC optimize("O3")

#ifdef LOCAL 
	#include "debug.h"
#else 
	#define dbg(...) 0
#endif

using namespace __gnu_pbds;
using namespace std;

typedef tree<long long, null_type, less_equal<long long>,
    rb_tree_tag, tree_order_statistics_node_update> Tree;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll INF = 9223372036854775807LL;
const ll inf = 2147483647;
const ll MOD = 1e9 + 7; //[998244353, 1e9 + 7, 1e9 + 13]
const ld PI = acos(-1);
const ll NROOT = 500;

ll binpow(ll a, ll b, ll _MOD = -1) {
	if (_MOD == -1)
		_MOD = MOD;
	ll res = 1;
	for (; b; b /= 2, a *= a, a %= _MOD)
		if (b & 1) res *= a, res %= _MOD;
	return res;
}

void set_IO(string s) {
#ifndef LOCAL
	string in  = s +  ".in";
	string out = s + ".out";
	freopen(in.c_str(),  "r",  stdin);
	freopen(out.c_str(), "w", stdout);
#endif
}

bool dataOverflow(ll a, ll b) {return (log10(a) + log10(b) >= 18);}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}
ll ceil(ll a, ll b) {return (a + b - 1) / b;}
ll invmod(ll a) {return binpow(a, MOD - 2);}

class DSU{
	private:
		int n;
		vector<int> uf, sz;
		stack<int> s;

	public:
		DSU (int _n) {
			n = _n;
			sz.resize(n + 1, 1);
			uf.resize(n + 1, -1);
		}

		int uf_find(int x) {
			return uf[x] == -1 ? x : uf[x] = uf_find(uf[x]);
		}

		bool uf_join(int x, int y) {
			x = uf_find(x);
			y = uf_find(y);

			if (x == y) 
				return 0;

			s.push(y);
			sz[x] += sz[y];
			uf[y] = x;
			return 1;
		}

		void rollBack(int k) {
			while (k -- && !s.empty()) {
				int x = s.top();
				s.pop();
				sz[uf_find(x)] -= sz[x];
				uf[x] = -1;
			}	
		}

		int get_size(int x) {
			return sz[uf_find(x)];
		}
};

class edge {
public:
	int w, a, b;
	bool operator< (edge other) {return w < other.w;}
};

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int n, m;
	cin >> n >> m;

	vector<edge> edges(m + 1);

	for (int i = 1; i <= m; i ++) {
		int a, b, c;
		cin >> a >> b >> c;
		edges[i] = {c, a, b};
	}

	int q;
	cin >> q;

	using T = pair<int, pair<int, int>>;
	vector<vector<T>> qrys(1000);

	int block = -1;
	for (int i = 0; i < q; i ++) {
		if (i % q == 0)
			block ++;

		int tq, a, b;
		cin >> tq >> a >> b;
		qrys[block].push_back({tq, {a, b}});
	}

	for (auto &qr : qrys) {
		// In the block I need some things
		// First lets separate operation between UPDATES and QUERIES
		// Then lets separate edges in CHANGED and UNCHANGES

		if (qr.empty())
			break;

	       	vector<bool> chg(n + 1);	
		vector<int> unchg, chgd;

		vector<pair<pair<int, int>, int>> ask;
		vector<vector<pair<int, int>>> d;

		for (auto &x : qr) {
			int tq = x.F;
			auto [a, b] = x.S;
			if (tq == 1) {	
				chg[a] = 1;
				chgd.push_back(a);
			}
		}

		for (auto &x : qr) {
			int tq = x.F;
			auto [a, b] = x.S;

			if (tq == 1) {
				edges[a].w = b;
			} else {
				ask.push_back(make_pair(make_pair(b, a), (int)d.size()));
				d.push_back({});
				for (auto &c : chgd) {
					if (edges[c].w >= b)
						d.back().push_back({edges[c].a, edges[c].b});		
				}
			}
		}

		vector<pair<int, int>> oedges;
		for (int i = 1; i <= m; i ++) 
			if (!chg[i])
				oedges.push_back({edges[i].w, i});
		stable_sort(ask.rbegin(), ask.rend());
		stable_sort(oedges.rbegin(), oedges.rend());

		vector<int> ans(d.size());
		DSU aux(n + 1);

		int i = 0;
		for (auto &r : ask) {
			int index = r.S;
			auto [w, x] = r.F;

			while (i < oedges.size() && oedges[i].F >= w) {
				aux.uf_join(edges[oedges[i].S].a, edges[oedges[i].S].b);
				i ++;
			} 

			int K = 0;
			for (auto &e : d[index]) {
				if (aux.uf_join(e.F, e.S)) 
					K ++;
			}

			ans[index] = aux.get_size(x);
			aux.rollBack(K);
		}

		for (auto &x : ans)
			cout << x << "\n";
	}

	return 0;
}

Compilation message (stderr)

bridges.cpp: In function 'int32_t main()':
bridges.cpp:155:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  155 |    auto [a, b] = x.S;
      |         ^
bridges.cpp:164:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  164 |    auto [a, b] = x.S;
      |         ^
bridges.cpp:191:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  191 |    auto [w, x] = r.F;
      |         ^
bridges.cpp:193:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  193 |    while (i < oedges.size() && oedges[i].F >= w) {
      |           ~~^~~~~~~~~~~~~~~
bridges.cpp: In function 'void set_IO(std::string)':
bridges.cpp:47:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  freopen(in.c_str(),  "r",  stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:48:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |  freopen(out.c_str(), "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...