题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去的座位或者就站在原地不动.新的座位和旧的座位,都不允许一个座位被两个人占据的情况.问你安排的方案数. 解法:对于这N个点,N条边构成的图,我们应该对每个连通块独立计算答案,最后乘起来.如果n个点,n-1条边答案显然为n.如果n个点n条边,会出现一个环,且恰好只有一个环.如果是一个自环,那么答案是1,…
题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的位置不动,要么坐到想坐的座位上,但是不能有两个人坐在同一个座位上. 问你合法的安排座位的方案数. 题解: 将2n个座位抽象成2n个点. 对于每个人,从他的初始位置向想坐的位置连一条边. 总答案即为所有连通块答案的乘积. 由于每一个点最多向外连一条边,所以对于每一个连通块只有三种情况: (1)是一棵树…
E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only…
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing r…
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1116 分析性质,只要有环,那么给环定一下向就满足了条件: 环上点的其他边可以指向外面,所以两个连通块合并时只要一个有环,那么整个连通块就都可以了. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; ,maxm=2e5+; int n,m,fa[maxn]…
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description In the aftermath of Canada’s annexation of Pittsburgh tensions have been pretty high between Canada and the US. You have personally been hired…
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a game called "Civilization". Dima helps him. The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pa…
好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环,接着给你两种操作: 1 x :求出 x 的集合中最长的边权值 2 x y:合并 x 的集合和 y的集合变成一个集合,并且将 x 集合中任意一个点与 y 集合中任意一个点相连,使合成的集合的任意两个点的最大权值最小,其中两个点相连的权值为1 开始边是固定的,因而建图并两次dfs遍历找树直径,但是不一定…
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Ivan had string s consisting of small English letters. However, hi…
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支持赋值.集合内整体加等操作,似乎就有些难处理. 我们不妨考虑此题的弱化版:没有第二类集合的版本.也就是要求支持合并两个集合,集合整体加某个数 \(x\),单点查询三个操作. 很明显集合整体加某个数 \(x\) 就相当于在并查集根节点处打一个 \(+x\) 的标记,查询就暴力跳父亲求出待询问点到根节点路径上所…