题目相关
题目链接
CodeForce网站。DSU 题集,https://codeforces.com/edu/course/2/lesson/7/1/practice/contest/289390/problem/D。
题面
There is an undirected graph and a sequence of operations of two types in the following format:
- cut u v — remove edge u - v from the graph;
- ask u v — check whether vertices u and v are in the same connected component.
After all the operations are applied, the graph contains no edges. Please, find the result of each operation of type ask.
输入
First line of input consists of three integers n, m and k (1 ≤ n ≤ 50 000, 0 ≤ m ≤ 100 000, m ≤ k ≤ 150 000) — the number of vertices in the graph, the number of edges and the number of operations, respectively .
Each of next m lines consists of two integers ui and vi (1 ≤ ui, vi ≤ n) — ends of edge i. Vertices are numbered from 1, graph has no loops and multiple edges.
Each of the next k lines describes an operation in the following format:
- "cut u v" (1 ≤ u, v ≤ n) — remove an edge between vertices u and v
- "ask u v" (1 ≤ u, v ≤ n) — check whether vertices u and v are in the same component
Each edge is mentioned in operations of type cut once.
输出
For each of operation of type ask output "YES", if two given vertices are in the same component, and "NO" — otherwise. The order of the answers should correspond to the order of operations of type ask in input.
样例输入
3 3 7
1 2
2 3
3 1
ask 3 3
cut 1 2
ask 1 2
cut 1 3
ask 2 1
cut 2 3
ask 3 1
样例输出
YES
YES
NO
NO
题解报告
题目分析
本题也是并查集的基础题。开始的时候,题目意思没看清楚,就看了要 cut 功能,然后就想如何 DSU 代码上增加删除集合功能。想了半天都没设计出一个简单的算法。因为标准的 DSU 只有并集和查集功能。
然后再次阅读题目,才发现本题的核心是:“After all the operations are applied, the graph contains no edges.” 也就是所有操作完成后,图中不包含任何边。也就是说,我们可以逆向思维,开始的时候,图上没有边,将 cut 变成合并集合。这样立马就是标准 DSU 代码了。
样例数据分析
根据样例数据,我们将操作倒序,变成如下:
ask 3 1
join 2 3
ask 2 1
join 1 3
ask 1 2
join 1 2
ask 3 3
这样,我们就可以轻松得到对应的答案,当然也是倒序的。
问题就变成,如何保存输入数据。可以通过数据结构轻松实现。例如
vector <pair<string, pair<int, int> > > xxx;
当然也可以使用结构体。
struct OP {
string op;
int u;
int v;
};
vector<OP> xxx;
AC 参考代码
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
using namespace std;
//Disjoint Set Union
template <class T>
struct DSU {
private:
/*
当_p[x]<0的时候,说明x的父节点是自己。同时 |_p[x]| 表示集合内元素数量
当_p[x]>0的时候,表示父节点
*/
T _n;//数据容量
std::vector<T> _p;//并查集关系
std::vector<T> _size;//集合大小
public:
DSU(T n) : _n(n), _p(n, 0), _size(n, 1) {
iota(_p.begin(), _p.end(), 0);
}
//查找x的父亲
T find_root(T u) {
return (u== _p[u] ? u : find_root(_p[u]));
}
//建立关系
bool union_set(T u, T v) {
if (u<0 || u>=_n || v<0 || v>=_n) {
return false;
}
u = find_root(u);
v = find_root(v);
if (u==v) {
//两个已经在同一个集合
return false;
}
if (_size[u]<_size[v]) {
//保证不会出现单边增长
swap(u, v);
}
_p[v] = u;
_size[u] += _size[v];
return true;
}
//查找u和v是否在同一个父亲
bool relation(T u, T v) {
if (u<0 || u>=_n || v<0 || v>=_n) {
return false;
}
return find_root(u) == find_root(v);
}
//数据 u 所在集合大小
T size(T u) {
if (u<0 || u>=_n) {
return 0;
}
return _size[find_root(u)];
}
}; //end of struct
int main() {
int n,m,k;
cin >> n >> m >> k;
for (int i = 0, u, v; i < m; i++){
//这些数据其实是无用的
cin >> u >> v;
}
vector <pair<string, pair<int, int> > > op;
string s;
for (int i = 0, u, w; i < k; i++){
cin >> s >> u >> w;
op.push_back({s, {u, w}});
}
vector<string> ans;
DSU<int> dsu(n+1);
while (op.size()){
int u = op.back().second.first;
int v = op.back().second.second;
if (op.back().first == "cut"){
dsu.union_set(u, v);
} else {
if (dsu.relation(u, v)) {
ans.push_back("YES");
} else {
ans.push_back("NO");
}
}
op.pop_back();
}
//输出
for (vector<string>::reverse_iterator rit=ans.rbegin(); rit!=ans.rend(); rit++) {
cout<<*rit<<"\n";
}
return 0;
}