首页 > 其他分享 >P9565 [SDCPC2023] Not Another Path Query Problem

P9565 [SDCPC2023] Not Another Path Query Problem

时间:2024-07-05 20:52:17浏览次数:14  
标签:std P9565 int SDCPC2023 long i64 二进制 Another define

P9565 [SDCPC2023] Not Another Path Query Problem

位运算+并查集

从价值至少为 \(V\) 入手,枚举一段二进制上长为 \(i\) 的前缀,第 \(i+1\) 位取 \(1\),并且比 \(V\) 要大,这样 \(i+1\) 之后的位就可以任意取了(不妨现在都先为 \(0\)),设这样构成的二进制串为 \(s\)。

考虑按位与的性质,随着路径增加,价值不增。并且如果想要二进制上其中一位为 \(1\),那么路径上所有边的二进制上这一位都得是 \(1\)。回到上面枚举的 \(s\),不妨固定我们需要 \(s\) 上的所有 \(1\),那么满足条件的边就是二进制上至少有这些 \(1\) 的边。这些边集与所有点构成一张新的图,如果 \(u\) 和 \(v\) 在这张图上连通,那么就可以找到一条满足条件的路径,使得价值 \(\ge s\ge v\)。

判断连通性,用并查集。最多有 \(60\) 个边集,只需要其中一个连通即可。记得开 longlong

复杂度 \(O(m\log n)\)。

#include <bits/stdc++.h>
#define pii std::pair<int, int>
#define mk std::make_pair
#define fi first
#define se second
#define pb push_back

using i64 = long long;
using ull = unsigned long long;
const i64 iinf = 0x3f3f3f3f, linf = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 10;
i64 n, m, q, V;
struct node {
	i64 u, v, w;
} e[N];
std::vector<int> v[61];
struct DSU {
	int fa[N];
	void init(int n) {for(int i = 1; i <= n; i++) fa[i] = i;}
	int find(int x) {
		return x != fa[x] ? fa[x] = find(fa[x]) : fa[x];
	}
	void merge(int x, int y) {
		if(x == y) return;
		fa[x] = y;
 	}
} dsu[61];
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
	std::cin >> n >> m >> q >> V;

	for(int i = 1; i <= m; i++) {
		std::cin >> e[i].u >> e[i].v >> e[i].w;
	}

	for(i64 i = 60; i >= 0; i--) {
		dsu[i].init(n);
		i64 s = 0;
		if(i && ((V >> (i - 1)) & 1LL)) continue;

		if(i) s |= (1LL << (i - 1));
		for(i64 j = 59; j >= i; j--) {
			s |= ((1LL << j) & V);
		}
		for(int j = 1; j <= m; j++) {
			i64 w = e[j].w;
			if((s & w) == s) v[i].pb(j);
		}
		
		for(auto id : v[i]) {
			int x = dsu[i].find(e[id].u), y = dsu[i].find(e[id].v);
			dsu[i].merge(x, y);
		}
	}	

	while(q--) {
		int u, v;
		std::cin >> u >> v;

		bool flg = 0;
		for(int i = 60; i >= 0; i--) {
			if(dsu[i].find(u) == dsu[i].find(v)) {
				flg = 1;
				break;
			}
		}

		std::cout << (flg ? "Yes\n" : "No\n");
	}
	return 0;
}

标签:std,P9565,int,SDCPC2023,long,i64,二进制,Another,define
From: https://www.cnblogs.com/FireRaku/p/18286594

相关文章

  • Yet Another Sigma Problem
    题目传送门题目跳转看吧题解哈希,字典树对字符串的前缀进行哈希处理,转换为数字,用\(map\),然后为了避免重复,可以将每一种公共字符串前缀的权重都设置为1例如:\(a\),\(ab\),\(aba\)权重都为1,因为\(ab\)是2,但是有一种包含在\(a\)里面,同理,&aba&是3,但是被&ab&,&a&......
  • D. Yet Another Monster Fight
    cf链接洛谷链接方法一最大最小值问题我们很容易想到二分答案法。那么我们如何写出check函数呢?对于答案x,若x-i+1<a[i],则选定怪物一定不在i位置左侧,即L=i;若x-n+i<a[i],则选定怪物一定不在i位置右侧,R=min(R,i)。遍历数组,如果L<=R则答案符合题意;否则不符合。code #includ......
  • MySQL The instance is already part of another Replication Group
    MySQLInnoDBCluster(测试环境为MySQL8.0.35)将一个实例重新加入集群时,遇到了下面这个错误"Theinstance'dbu03:3306'isalreadypartofanotherReplicationGroup"MySQL  10.160.2.55:3306 ssl  JS > cluster.addInstance('[email protected]:3306')ERROR: Ru......
  • GD32学习中遇到 warning: #188-D: enumerated type mixed with another type 强迫症尽
    项目场景:今天往GD32的系统板里加入六个按键,在DEMO程序的基础上要做一些修改。在对时钟使能的时候,习惯的用STM32的方法。加|线隔开两个GPIO口,结果报出warning:#188-D:enumeratedtypemixedwithanothertype的警告。强迫症尽量不要有警告……rcu_periph_clock_enable(RC......
  • CF1234F Yet Another Substring Reverse
    CF1234FYetAnotherSubstringReverse状压dp+高维前缀和一个很显然的发现是最长子串长度不会超过字符集。那么如果没有这个操作,是很简单的,我们看看多了这个操作意味着什么。对于一个子串,考虑一次翻转对它的影响。在它内部的翻转肯定是没有意义的;我们一定有一个操作能将任意......
  • git 命令报错:Another git process seems to be running in this repository, e.g. an
    执行git命令时,报错:Anothergitprocessseemstoberunninginthisrepository,e.g.aneditoropenedby'gitcommit'.Pleasemakesureallprocessesareterminatedthentryagain.Ifitstillfails,agitprocessmayhavecrashedinthisrepository......
  • CF1228E Another Filling the Grid 题解
    tag:容斥原题+组合数设F[i]F[i]F[i]表示至少......
  • Another Filling the Grid
    AnotherFillingtheGrid题目信息题目描述Youhave$n\timesn$squaregridandaninteger$k$.Putanintegerineachcellwhilesatisfyingtheconditionsbelow.Allnumbersinthegridshouldbebetween$1$and$k$inclusive.Minimumnumberofth......
  • [SDCPC2023] Colorful Segments 线段树转移DP
    Codeforces链接​  洛谷题目链接#[SDCPC2023]ColorfulSegments##题面翻译**【题目描述】**考虑数轴上的$n$条线段,其中第$i$条线段的左端点为$l_i$,右端点为$r_i$。每一条线段都被涂上了颜色,其中第$i$条线段的颜色为$c_i$($0\lec_i\le1$)。颜色共有两种,$c_i......
  • Engage with world in another way, Strench myself. dataism已经进入房间, 等待历史
    忘记历史,你就不会被历史所羁绊,你看到的每一天都是全新的。engagewithyourlife,而不是藏在生活的后面,liveinyourlife,notbehindoraboveyourlife,notpretenttolive,justliveinit.体现物体特性的其实是分子,而不是原子。虽然游离态的原子更自由,但是原子性质更单......