先从最简单的开始:
题意: 一共n个强盗,m条线索,输出有多少个独立团伙(同伙的同伙是同伙,老鼠的儿子会打洞)
样例输入:
10 9
1 2
3 4
5 2
4 6
2 6
8 7
9 7
1 6
2 4
样例输出:
3
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
没啥好总结的。。。
然后有变形。。。又是王教授出的。。
找朋友
时间限制(C/C++):1000MS/3000MS 运行内存限制:65536KByte
总提交:422 测试通过:138
描述
假设朋友关系是可以传递的,也就是说,如果A和B是朋友,B和C是朋友,那么A和C也是朋友。给出一个人员集合及其朋友关系,则这个集合可以分割成为不相交的子集合。给出一个人员的集合,以及他们之间部分的关系(哪两个人是朋友),让你求出分割出的子集合中最大的那个有多少人。
输入
第一行是一个整数,在[2,50]之间,表示要输入的案例的数量。对每个案例,第一行是2个整数,用空格隔开。第一个整数m表示集合的规模,第二个整数给出的关系数量n。后面紧跟n行,每一行都是两个整数x和y,满足0 <= x, y < n。0
输出
对每一个案例,输出该集合分割后最大的那个子集合中的人数。
样例输入
2
4 4
0 3
2 1
3 1
0 2
5 5
0 1
1 2
0 2
3 4
4 3
样例输出
4
3
这下好,还要统计人数。。用个结构体就ok
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
只是跑得慢了点。。。
还有一道变了变样子的题:
AQ and his xiaoqiang
时间限制(C/C++):1000MS/3000MS 运行内存限制:65536KByte
总提交:45 测试通过:8
描述
In AQ's dormitory,there are n xiaoqiang.They are so smart that they have their ways to know others as friends or enemies,
any two of them are friends or enemies if they know each other.And the relationship among them described as follows:
(we assume 3 xiaoqiang named A,B,C)
1.if A's friend is B,B's friend is C,so C is also a friend of A.
2.if A's enemy is B,B's enemy is C,so C is a friend of A.
We know a family consist of the xiaoqiang who are all friends between any two of them.So if I tell you
m relationships among n xiaoqiang,can you tell me what is the maximum number of families in AQ's dormitory?
输入
There are multiple cases.In the first line,there is n and m,represents the number of xiaoqiang and the relationships.(n<1000, m<5000)
among them.In the following m rows,there are three numbers:p x y.
1.p=1,x and y are friends.
2.p=0,x and y are enemies.
输出
Please output the maximum number of families in one line.
样例输入
6 4
0 1 4
1 3 5
1 4 6
0 1 2
样例输出
3
居然是小强我也是醉了,还有敌人啊朋友我也是醉了
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
这个算法还算比较简单。。
不过 简单是简单 当然有值得注意的地方
比如图中判断是否直接或者间接连接的问题时,例如CF的这道题
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 ton. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.
Mr. Kitayuta wants you to process the following q queries.
In the i-th query, he gives you two integers — ui and vi.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.
Output
For each query, print the answer in a separate line.
Note
Let's consider the first sample.
The figure above shows the first sample.
- Vertex 1 and vertex 2 are connected by color 1 and 2.
- Vertex 3 and vertex 4 are connected by color 3.
- Vertex 1 and vertex 4 are not connected by any single color.
再例如这组数据:
5 4
2 3 1
2 5 1
2 4 1
2 1 1
4
1 2
1 3
1 4
1 5
通过合并先后把 (2,3) (2,5) (2,4) (2,1) 终点都是1这个点(通过颜色1连接)但是 3,4,5这三个点的终点并不是1,而是2,这就是这道题一直错的原因
说多了都是泪 贴代码!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include