D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input:standard input output: standard output Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types o…
题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反义,若没关系就按操作归为同义或反义 思路 经典并查集的动物园问题 维护两个并查集,find(a)表示a的同类代表元,find(a+maxn)表示异类代表元 find(a)==find(b) && find(a+maxn)==find(b+maxn) 就是说ab同类 find(a+maxn)==…
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of…
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可以利用这个性质 考虑一个左边的点和它右边联通的两个点,发现这两个点只能选择一个和这个左边的点匹配 那么我们考虑把这个点点匹配的模型转化成点边匹配 我们在同一个左边点连的两个右边点之间连边,那么问题就变成了一个点和一条相邻的边匹配,求完美匹配的问题了 而这个问题,我们显然可以用并查集来很好的解决 考虑…
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean t…
并查集. 将每一个物品拆成两个,两个意义相反,然后并查集即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set>…
题目链接 \(Description\) 给定\(n\)个数对\(A_i,B_i\).你可以进行任意次以下两种操作: 选择一个位置\(i\),令\(A_i=A_i+1\),花费\(B_i\).必须存在一个位置\(j\),满足\(A_i=A_j,\ i\neq j\),才可以进行. 选择一个位置\(i\),令\(A_i=A_i-1\),花费\(-B_i\).必须存在一个位置\(j\),满足\(A_i=A_j+1\),才可以进行. 你需要对于所有\(i\in[1,n]\),求使得\(A_1,A_2,…
题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(Solution\) 把每个人向\(ai,bi\)连边.对于每个连通块单独考虑. 记点数为n,边数为m.可以发现当某一连通块n>m(n=m+1)时,可以有一个点不选(最大的): 当n=m时,所有点都要选:n<m时,无解. 用并查集维护连通,顺便维护最大.次大值.当第一次出现环时,即n=m:第二次出现就…
/** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照顺序放n个木块,每个木块满足两种条件. 1,和地面相邻或者和以前放过的木块有一个相邻的面. 2,不在封闭空间内.即可从无限远到达该木块. 判断该种放木块顺序是否合法. 思路:https://www.zhihu.com/question/42406890 逆向处理,并查集维护那些可以从无限远到达的位置…
参考:大牛blog 思路: 因为是环,所以可以复制一下图,先判断一下和他是不是和与他相邻的8个之一的一个障碍使得构成了一个环,环就是一个连通,用并查集维护即可: 如果没有就ans++,然后并把这个点加入. 大致意思就是这样. #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> #include <ctime> #include <cmath…