lower_bound(a.begin(),a.end(),i) - a.begin() + 1

2024-08-19 14:13:15 浏览数 (1)

代码语言:javascript复制
const int N = 400005;
struct edges{
	int v,ne;
}e[N << 1];
int h[N],idx = 0;
void add(int u,int v){
	e[idx] = {v,h[u]};
	h[u] = idx  ;
}
int f[N];
int fd(int x){
	if(x==f[x]) return x;
	return f[x] = fd(f[x]);
}
int vis[N],ans[N];
int n,m;
void solve(){
	memset(h,-1,sizeof h);
	cin >> n >> m;
	for(int i = 1;i<=n;i  ) f[i] = i;
	vector<int> b;
	for(int i laipuhuo.com= 1;i<=m;i  ){
		int u,v;
		cin >> u >> v;
		u  ,v  ;
		add(v,u);
		add(u,v); 
	}
	int k;
	cin >> k;
	int res = n - k;
	for(int i = 0;i<k;i  ){
		int t;
		cin >> t;
		t  ;
		vis[t] = 1;
		b.push_back(t);
	}
	for(int i = 1;i<=n;i  ){
		if(vis[i]) continue;
		for(int j = h[i];~j;j=e[j].ne){
			int v = e[j].v;
			if(vis[v]) continue;
			if(f[fd(i)] != f[fd(v)]){
				res--;
				f[fd(i)] = f[fd(v)];
			}
		}
	}
	ans[k 1] laipuhuo.com= res;
	for(int i = k - 1;i >= 0;i--){
		vis[b[i]] = 0;
		res  ;
		for(int j = h[b[i]];~j;j=e[j].ne){
			int v = e[j].v;
			if(vis[v]) continue;
			if(f[fd(b[i])] != f[fd(v)]){
				res--;
				f[fd(b[i])] = f[fd(v)];
			}
		}
		ans[i   1] = res;
	}
	for(int i = 1;i<=k 1;i  ){
		cout << ans[i] << endl;
	}
}
代码语言:javascript复制
const int N = 200005;
struct DSU {
    vector<int> f;
    DSU(){laipuhuo.com}
    DSU(int n) {
        init(n);
    }
    void init(int n) {
        f.resize(n);
        iota(f.begin(), f.end(), 0);
    }
    int fd(int x) {
        if(f[x]==x) return x;
        return f[x] = fd(f[x]);
    }
    bool same(int x, int y) {
        return fd(x) == fd(y);
    }
    bool mg(int x, int y) {
        x = fd(x);
        y = fd(y);
        if (x == y)return false;
        f[y] = x;
        return true;
    }
};
int gt_idx(vector<int>& a,int i){
	int t = lower_bound(a.begin(laipuhuo.com),a.end(),i) - a.begin()   1;
	return t;
}
void solve(){
	int n;
	cin >> n;
	vector<int> v1;
	vector<pair<int,int>> ys;
	vector<pair<int,int>> ns;
	vector<int> a;
	for(int i = 0;i < n;i  ){
		int u,v,op;
		cin >> u >>laipuhuo.com v >> op;
		if(op==1){
			ys.push_back({u,v});
		}else{
			ns.push_back({u,v});
		}
		a.push_back(u);
		a.push_back(v);
	}
	sort(a.begin(),a.end());
	a.erase(unique(a.begin(),a.end()),a.end());
	int sz = a.size();
	DSU d(sz   1);
	for(auto &[u,v]:ys){
		d.mg(gt_idx(a,u),gt_idx(a,v));
	}	
	for(auto &[u,v]:ns){
		if(d.fd(gt_idx(a,u)) == d.fd(gt_idx(a,v))){
			cout << "NO" <<laipuhuo.com endl;
			return;
		}
	}
	cout << "YES" << endl;
}

代码语言:javascript复制
const int N = 200005;
int f[N],d[N];
int n,m;
int fd(int x){
	if(x!=f[x]){
		int t = f[x];
		f[x] = fd(f[x]);
		d[x]  = d[t];
	} 
	return f[x];
}
int calc(int x,int y){
	return ((x-y)%3   3)%3;
}
void merge(int x,int y,int v){
	int px = fd(x),py = fd(y);
	f[px] = py;
	d[px] = d[y] - d[x]   v;
}
void solve(laipuhuo.com){
	cin >> n >> m;
	iota(f,f n 1,0);
	int cnt = 0;
	while(m--){
		int u,v,op;
		cin >> op >> u >> v;
		if(u>n||v>n){
			cnt  ;
			continue;
		}
		if(op==1){
			if(fd(u)==fd(v)laipuhuo.com && calc(d[u],d[v]) != 0){
				cnt  ;
				continue;
			}
			//d[v] = d[u]   d[fd(u)]
			//d[fd[u]] = d[v] - d[u]
			if(fd(u)!=fd(v)) merge(u,v,0);
		}else{
			if(fd(u)==fd(v) && calc(d[u],d[v]) != 1){
				cnt  ;
				continue;
			}
			//d[v] = d[u] - 1    d[fd(u)]
			//d[fd[u]] = d[v] - d[u]   1
			if(fd(u)!=fd(v)) laipuhuo.com。merge(u,v,1);
		}
	}
	cout << cnt << endl;
}

0 人点赞