题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是就想到了用并查集来预先判断是否属于同一个连通分量. 2.可以将与d属于同一个连通分量的点用一个数组保存起来,然后dfs搜索这个数组就可以了,这也就是只搜索了与d在一个连通分量里的点. 3.当搜索到d的时候就输出路径. 代码: #include <bits/stdc++.h> #define inf…
题意:输入着火点n,求结点1到结点n的所有路径,按字典序输出,要求结点不能重复经过. 分析:用并查集事先判断结点1是否可以到达结点k,否则会超时.dfs即可. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmat…
题意:构造出一张图,给出一个点,字典序输出所有从1到该点的路径. 裸搜会超时的题目,其实题目的数据特地设计得让图稠密但起点和终点却不相连,所以直接搜索过去会超时. 只要判断下起点和终点能不能相连就行了,可以用并查集也可以用floyd算法,这样就能过了. 但是这个方法不是很完美的,如果两点之间只有一条线相连,而图又是稠密图,这样也很容易超时,数据强电就会挂掉. 可以把算法改进一下:是先从终点出发,无回溯的走遍和终点相连的所有点并标记,然后从起点出发,DFS判断下标记,这样就不会多走很多路了.另一个…
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在于操作2,对于并查集,要去除掉一个结点,假设该结点不是根那就好办了,那么就多开n个结点,每一个结点初始父亲都是它的i + n,这样在移动的时候,就不用操心他是根结点了剩下就是普通的带权并查集了 代码: #include <cstdio> #include <cstring> const…
题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现与之前告诉你的相矛盾,此时输出“The first n(第几个I) facts are conflicting.”接着一直保持沉默,否则不输出.最后一类询问可能得不到值,就输出“I don’t know.”,否则输出结果 题解:告诉你时使用并查集的合并操作,可以记录权值为此点异或父亲节点的值,祖先节…
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. In the Not-Sprea…
//思路详见课本 P 214 页 思路:直接用并查集,set [ k ]  存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k  的敌人 所在集合的代表元素. #include<iostream> #include<cstring> using namespace std; const int maxn=2 *10000 +100; int set[maxn]; int n; int set_find(int d) { if(set[d]<0) return…
Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to implement something similar, but not identical. The data structure you need to write is also a collection of disjoint sets, supporting 3 operations: 1 p…
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4922 题意是给定一颗森林,然后每次都可以删除一条边,或者询问某两个点是否连通. 如果顺着做是不行的.因为并查集路径压缩了,是删不了边的,(据说不路径压缩能AC,) 所以考虑逆向操作.首先不能把已经删除了的边加进去森林里面,先处理出最终状态,然后倒着模拟,就能把删边操作等价于变…
                                 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you’re to implement somethingsimilar, but not identical.The data structure you need to write is also a collection of disjoint sets…