제출 #792881

#제출 시각아이디문제언어결과실행 시간메모리
792881phoenix0423다리 (APIO19_bridges)C++17
100 / 100
1687 ms228112 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
const int maxn = 1e5 + 5;
ll u[maxn], v[maxn], w[maxn];
ll t[maxn], x[maxn], y[maxn], ch[maxn]; // [bridge, new weight] or [start, car weight]
ll ans[maxn];
vector<int> to_join[maxn];
int n, m, q;
const int b = 1200;

struct dsu_save{
	int u, rku, v, rkv;
	dsu_save(){}
	dsu_save(int _u, int _rku, int _v, int _rkv) : u(_u), rku(_rku), v(_v), rkv(_rkv){}
};
struct DSU{
	vector<int> par, siz;
	int n;
	stack<int> stk;
	DSU(){}
	DSU(int _n) : n(_n){}
	void init(){
		siz = vector<int>(n, 1);
		par.resize(n);
		for(int i = 0; i < n; i++) par[i] = i;
	}
	int root(int x){
		return x == par[x] ? x : root(par[x]);
	}
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if(x == y) return;
		if(siz[x] > siz[y]) swap(x, y);
		stk.push(x);
		par[x] = y;
		siz[y] += siz[x];
	}
	void roll_back(int sz){
		while(stk.size() > sz){
			auto u = stk.top(); stk.pop();
			siz[par[u]] -= siz[u];
			par[u] = u;
		}
	}
};
int main(void){

	fastio;
	cin>>n>>m;
	DSU dsu(n + 1);
	for(int i = 1; i <= m; i++){
		cin >> u[i] >> v[i] >> w[i];
	}
	cin>>q;
	for(int i = 1; i <= q; i++){
		cin >> t[i] >> x[i] >> y[i];
	}
	for(int l = 1; l <= q; l += b){
		int r = min(q + 1, l + b);
		fill(ch, ch + maxn, false);
		dsu.init();
		vector<int> ask, upd, unchanged;

		for(int i = l; i < r; i++){
			if(t[i] == 1){
				ch[x[i]] = true;
				upd.pb(i);
			}
			else ask.pb(i);
		}
		for(int i = 1; i <= m; i++){
			if(!ch[i]) unchanged.pb(i);
		}
		for(int i = l; i < r; i++){
			if(t[i] == 1) w[x[i]] = y[i];
			else{
				to_join[i].clear();
				for(auto j : upd){
					if(w[x[j]] >= y[i]) to_join[i].pb(x[j]);
				}
			}
		}
		sort(ask.begin(), ask.end(), [](int a, int b){ return y[a] > y[b];});
		sort(unchanged.begin(), unchanged.end(), [](int a, int b){ return w[a] > w[b];});
		// processing queries
		int unptr = 0;
		for(auto i : ask){
			while(unptr < unchanged.size() && w[unchanged[unptr]] >= y[i]){ // no need to roll back
				dsu.unite(u[unchanged[unptr]], v[unchanged[unptr]]);
				unptr++;
			}
			int prev_sz = dsu.stk.size();
			for(auto j : to_join[i]){
				dsu.unite(u[j], v[j]);
			}
			ans[i] = dsu.siz[dsu.root(x[i])];
			dsu.roll_back(prev_sz);
		}
	}
	for(int i = 1; i <= q; i++) if(t[i] == 2) cout << ans[i] << "\n";
}

컴파일 시 표준 에러 (stderr) 메시지

bridges.cpp: In member function 'void DSU::roll_back(int)':
bridges.cpp:44:20: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   44 |   while(stk.size() > sz){
      |         ~~~~~~~~~~~^~~~
bridges.cpp: In function 'int main()':
bridges.cpp:93:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |    while(unptr < unchanged.size() && w[unchanged[unptr]] >= y[i]){ // no need to roll back
      |          ~~~~~~^~~~~~~~~~~~~~~~~~
#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...