Submission #567876

#TimeUsernameProblemLanguageResultExecution timeMemory
5678768e7Bridges (APIO19_bridges)C++17
46 / 100
2116 ms54760 KiB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T,class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
const int bs = 500;

vector<pii> adj[maxn], vt[maxn];
vector<int> f[maxn];
int wei[maxn];
pii ed[maxn];

bool rel[maxn], bridge[maxn];
vector<int> vert, bridges;
pii getvtree(int n, int par) {
	pii ret = {0, maxn - 1};
	vector<pii> chi;
	for (auto [v, w]:adj[n]) {
		if (v != par) {
			pii tmp = getvtree(v, n);
			if (wei[w] < wei[tmp.ss]) tmp.ss = w; 
			if (tmp.ff) chi.push_back(tmp);	
		}
	}
	if (rel[n] || chi.size() > 1) {
		vert.push_back(n);
		rel[n] = 1;
		ret.ff = n;
		for (auto p:chi) {
			bridge[p.ss] = 1;
			bridges.push_back(p.ss);
			vt[n].push_back({p.ff, p.ss});
			vt[p.ff].push_back({n, p.ss});
		}
	} else if (chi.size() == 1) {
		ret = chi[0];
	}
	return ret;
}
void getdis(int n, int par, vector<int> &vec, int mi) {
	vec.push_back(mi);
	for (auto [v, w]:adj[n]) {
		if (v != par && !bridge[w]) {
			getdis(v, n, vec, min(mi, wei[w]));
		}
	}
}
void getans(int n, int par, int wi, int &ans) {
	ans += f[n].size() - (lower_bound(f[n].begin(), f[n].end(), wi) - f[n].begin());
	for (auto [v, w]:vt[n]) {
		if (v != par && wei[w] >= wi) {
			getans(v, n, wi, ans);
		}
	}
}

struct obj{
	int type, x, y;
	obj(){type = x = y = 0;}
	obj(int a, int b, int c){type = a, x = b, y = c;}
};
int main() {
	io
	wei[maxn - 1] = inf;
	int n, m;
	cin >> n >> m;
	if (m >= n) return 0;
	for (int i = 0;i < m;i++) {
		int u, v, w;
		cin >> u >> v >> w;
		ed[i] = {u, v};
		adj[u].push_back({v, i});
		adj[v].push_back({u, i});
		wei[i] = w;
	}
	
	int q;
	cin >> q;
	vector<obj> bat;
	auto solve = [&] () {
		for (auto [type, x, y]:bat) {
			if (type == 1) {
				rel[ed[x].ff] = 1;
				rel[ed[x].ss] = 1;
			} else {
				rel[x] = 1;
			}
		}
		getvtree(1, 0);
		for (int i:vert) {
			f[i].clear();
			getdis(i, 0, f[i], inf);
			sort(f[i].begin(), f[i].end());
		}
		for (auto [type, x, y]:bat) {
			if (type == 1) {
				wei[x] = y;
			} else {
				int ans = 0;
				getans(x, 0, y, ans);
				cout << ans << "\n";
			}
		}
		for (int i:vert) {
			vt[i].clear();
			rel[i] = 0;
		}
		vert.clear();
		for (int i:bridges) {
			bridge[i] = 0;
		}
		bridges.clear();
		bat.clear();
	};
	while (q--) {
		int type; cin >> type;
		if (type == 1) {
			int bi, ri;
			cin >> bi >> ri;
			bi--;
			bat.push_back(obj(type, bi, ri));
		} else {
			int s, w;
			cin >> s >> w;
			bat.push_back(obj(type, s, w));
		}
		if (bat.size() >= bs) {
			solve();
		}
	}
	solve();
}
/*
7 6
1 2 4
1 3 5
2 4 2
2 5 1
3 6 7
3 7 3
5
2 3 3
1 4 3
2 3 5
2 2 2
2 1 6
*/
#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...