本文共 4079 字,大约阅读时间需要 13 分钟。
06-图1:列出连通集.
Description:
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N-1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。
输入第1行给出2个整数N(0, 10)和E,分别是图的顶点数和边数。随后E行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。
Output:
按照"{v1, v2..., vk}"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。
8 6
0 7
0 1
2 0
4 1
2 4
3 5
SampleOutput:
{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }
Codes:
//#define LOCAL#include #include using namespace std;#define M 10int ne, nv, check[M], g[M][M];void bG() { int i, v1, v2; scanf("%d%d", &nv, &ne); for(i=0; i
q; i = cV(); if(i == -1) return i; q.push(i); check[i] = 1; printf("{ %d ", i); while(!q.empty()) { int t = q.front(); q.pop(); for(j=0; j
06-图2:Saving James Bond - Easy Version.
Description:
This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether or not he can escape.
Each input file contains one test case. Each case starts with a line containing two positive integers N(<=100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x, y)(x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output:
For each test case, print in a line "Yes" if James can escape, or "No" if not.
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
SampleOutput1:
Yes
4 13
-12 12
12 12
-12 -12
12 -12
SampleOutput2:
No
Codes:
//#define LOCAL#include #include struct C { double x, y; };int rC(double d, C p) { return (15+d)*(15+d)>=p.x*p.x+p.y*p.y; }int rBe(double d, C p1, C p2) { return d*d>=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y); }int rBa(double d, C p) {return p.x<=-50+d||p.x>=50-d||p.y>=50-d||p.y<=d-50;}int DFS(double d, C *cr, int v, int *vi, int n) { if(rBa(d, cr[v])) return 1; for(int i=0; i = 35) { printf("Yes\n"); return 0; } C cr[102]; for(i=0; i
06-图3:六度空间.
Description:
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入第1行给出两个正整数,分别表示社交网络图的结点数N(1<N<=1000,表示人数)、边数M(<=33×N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。
Output:
对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。
10 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
SampleOutput:
1: 70.00%
2: 80.00%
3: 90.00%
4: 100.00%
5: 100.00%
6: 100.00%
7: 100.00%
8: 90.00%
9: 80.00%
10: 70.00%
Codes:
//#define LOCAL#include #include #include #include using namespace std;#define M 100010int m, n, vi[M];vector v[M];int sds(int t) { queue q; q.push(t); vi[t] = 1; int i, cN = 0, lN = 1, l = 0, cnt = 1; while(!q.empty()) { t = q.front(); q.pop(); --lN; for(i=0; i