<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老鼠的最少花费. 解题分析: 首先就是要注意老鼠的逃生路线为强连通分量的情况,毫无疑问,这种情况就是在那个强连通分量中的代价最小的房间安装老鼠夹(因为根据老鼠的流通性,只需要在连通分量中安装一个老鼠夹就能捕获所有的老鼠),所以我们先用Tarjan对这些房间进行缩点.然后我们只需要将那些出度为0的强连通分量的代价…
题目描述: Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80%of applicants a…
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y所在的块连在一起,并使新块的最长路径最短. 分析 先想想最长路径怎么求,倘若我们以一个点为根,那么最长路径就是这棵有根树的直径了,那么我们可以先从任意点u出发,走到最远点v,再从v出发走到最远点,此时就能得出这棵树的直径了.现在想想怎么把两块合并?应该想到的是并查集,由于还得考虑合并后的直径最小,经…
话说这道题的机遇是看到了http://blog.csdn.net/u010885899/article/details/50611895很有意思:然后就去补了这题 题意: 建最少的边使得给出的点相连. 思路: 直观感觉,如果a->b,b->c,那么a->c就不用建了. 然后还有一种情况就是回路a->b->c->a,这样的话要有n条. 所以其实思路就是这样,弱连通的时候n个点就是+n-1条边,强连通的时候n个点+n条边: 印象中tarjan当是这样一条(a->b,b…
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <string> #include <vector> using namespace std; ; co…
http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路径相连),问在这样的图里面遵守这样的规则可以最多添加几条边. 思路:这种题之前在做强连通的时候很常见,于是就写了tarjan..醒来后发现不用这么复杂,直接用并查集就可以做了. 1.对于每一个连通块,最多可以加上n*(n-1)/2条边. 2.对于受限制的连通块,取出一个点数最多的,和不受限制的块相连…
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are…
B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/B Description Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game? There are n balls put in a row. Each ball i…
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James diGriz, I'm the most clever robber and treasure hunter in the whole galaxy. There are books written about my adventures and songs about my operations…
辣鸡错误:把dfs和ldfs搞混...QAQ 题意:给定一个无向图,然后查询q次,求每次查询就在图上增加一条边,求剩余割边的个数. 先把边双缩点,然后预处理出LCA的倍增数组: 然后加边时,从u往上跳,把所有u到LCA(u,v)路径上割边去掉,即 --ans:v同理: 而向上跳的时候可以用并查集,把已经去掉的边,用路径压缩忽略掉,会往上跳的更快些. memset记得写全,不然.. #include<cstdio> #include<iostream> #include<cma…
好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm> #include <cstdio> using namespace std; , M = 2e5 + ; ], Next[M * ]; int dfn[N], low[N], n, m, tot, num; ]; void add(int x, int y) { ver[++tot] =…
1140 分珠 时间限制:500MS  内存限制:65536K提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不同,珠子之间有一些细线将它们连在一起.现要求切断一些细线,将它们分成两部分, 分割后,单独每一部分的珠子仍保持相连,且要求尽量做到两部分总重相等或相差最少. 请编一程序,给定珠子个数.每颗珠子的重量以及珠子之间的连接情况,输出按上述要求分割后两部分总重的差值的绝对值. 输入格式 第一行有两个数N与M…
Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Input The first line contains a single integer nn (2≤n≤150000) — the number of kittens. Each of the following n−1lines contains integers xi and yi (1≤xi,…
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <string> #include <vector> using namespace std; /* dfs求最大层数 并查集求连通个数 */ +; int n; ; //最大层数 vector<int>deepro…
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 思路: 并查集维护这m条关系,用个vector存一下当前点所能到达的点,拍下序大的优先,输出的时候输出当前点所能找到的最大的数,已输出的标记下就好了. 实现代码: #include<bits/stdc++.h> using namespace std; ; int f[M],vis[M],a[M…
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 ----------------------------------------------------------------------------------------------------------------------…
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now…
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deepest root. 给定一个图,按升序输出所有 deepest root.如果给定的图有多个连通分量,则输出连通分量的数量. 1.使用并查集判断图是否为连通的. 2.任意选取一点,做 dfs 搜索,选取其中一个最远距离的点 A,再做一次 dfs,找到的所有距离最远的点以及点 A 都是 deepest…
CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who…
题目链接  Educational Codeforces Round 40  Problem I 题意  定义两个长度相等的字符串之间的距离为:   把两个字符串中所有同一种字符变成另外一种,使得两个字符串相等所需要操作的次数的最小值.   求$s$中每一个长度为$t$的长度的连续子串与$t$的距离.字符集为小写字母$a$到$f$ 首先解决求两个长度相等的字符串之间的距离这个问题. $s$和$t$相同位上的字母连一条无向边,最后的答案是$s$和$t$中所有出现过的字符的个数减去这个无向图的连通块…
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数组 num[i] 维护编号为 i 的边当前最大能承受的重量. 在加边的过程中根据给出的父亲节点将当前边所在的链上所有边的num都减去当前加的边的重量, 注意当前边也要减自重. 那么当num首次出现负数时加的边号即位答案: 事实上这个算法的时间复杂度是O(n^2)的, 不过本题并没有出那种退化成单链的…
Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB                             Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse…
Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1381    Accepted Submission(s): 386 Problem Description "The Werewolves" is a popular card game among young people.In the basic ga…
题目链接:https://vjudge.net/problem/POJ-3694 A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so d…
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prep…
题目链接:http://codeforces.com/problemset/problem/859/E 题目大意: 有$n$个人,$2n$个座位. 给出这$n$个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的位置不动,要么坐到想坐的座位上,但是不能有两个人坐在同一个座位上. 问你合法的安排座位的方案数. 题解: 考虑把每个人看成边,把每个人想坐的位置连起来,显然我们会得到一个个的联通块 我们设某个联通块的边数为$e$,点数为$v$,那么有$e>=v-1$. $e>v$的时候显然不存在这…
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of n passwo…
[题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的数量,然后求边双联通分量并记录每个结点v所属的连通分量号c[v],之后进行缩点,将每个双联通分量作为都缩成一个新点,如果新点之间可以连边就连边 (能不能连边取决于原图,我就不多bb辽,XD),形成新图. 对于每次询问x, y,判断c[x]!=c[y],然后从c[x]和c[y]分别向上寻找父结点,找到…
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: 11110 11010 11000 00000 输出: 1 示例 2: 输入: 11000 11000 00100 00011 输出: 3 思路分析:题解和示例图来自:liweiwei1419 说明:以下介绍的算法,除了并查集以外,DFS 和 BFS 都属于很基础的算法内容,也非常好理…
题目大意: 输入 p,n,t :p为地点数 判断 t 能否回到源点1 接下来n行 每行输入 a b c: a能到达b和c Sample Input 13 6 76 7 82 3 410 11 128 9 101 2 134 5 6 Sample Output 512467   可用深搜做 #include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; int p,n,t; ],nexti[],flag[]; ],v[],…