so easy(unordered_map+并查集)

Posted 0xiaoyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了so easy(unordered_map+并查集)相关的知识,希望对你有一定的参考价值。

There are nn points in an array with index from 11 to nn, and there are two operations to those points.

1: 1 x1 x marking the point xx is not available

2: 2 x2 x query for the index of the first available point after that point (including xx itself) .

 

技术图片

样例输入

5 3
1 2
2 2
2 1

样例输出

3
1
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 unordered_map<int, int> fa;
 6 
 7 int findfa(int x)
 8 {
 9     if (!fa.count(x)) return x;
10     return fa[x] = findfa(fa[x]);
11 }
12 
13 int main()
14 {
15     int n, q;
16     int op, x;
17     scanf("%d %d", &n, &q);
18     while (q--)
19     {
20         scanf("%d %d", &op, &x);
21         if(op == 1) fa[x] = findfa(x + 1);
22         else printf("%d
", findfa(x));
23     }
24     return 0;
25 }

 

 

 

 

以上是关于so easy(unordered_map+并查集)的主要内容,如果未能解决你的问题,请参考以下文章

上海交大机试 第一题 Easy *并查集

B1. Books Exchange (easy version)1000 / 并查集

CF #738(div2)D1. Mocha and Diana (Easy Version)(暴力,并查集)

1114 Family Property(并查集)

[并查集] leetcode 128 Longest Consecutive Sequence

并查集