18.12.5 发现他,抓住他(并查集)&正方形(哈希表)

发现他,抓住他(10分)

题目内容:

一个城市中有两个犯罪团伙A和B,你需要帮助警察判断任意两起案件是否是同一个犯罪团伙所为,警察所获得的信息是有限的。假设现在有N起案件(N<=100000),编号为1到N,每起案件由团伙A或团伙B所为。你将按时间顺序获得M条信息(M<=100000),这些信息分为两类:

1. D [a] [b]

其中[a]和[b]表示两起案件的编号,这条信息表明它们属于不同的团伙所为

2. A [a] [b]

其中[a]和[b]表示两起案件的编号,这条信息需要你回答[a]和[b]是否是同一个团伙所为

注意你获得信息的时间是有先后顺序的,在回答的时候只能根据已经接收到的信息做出判断。

 

输入格式:

第一行是测试数据的数量T(1<=T<=20)。
每组测试数据的第一行包括两个数N和M,分别表示案件的数量和信息的数量,其后M行表示按时间顺序收到的M条信息。

 

输出格式:

对于每条需要回答的信息,你需要输出一行答案。如果是同一个团伙所为,回答"In the same gang.",如果不是,回答"In different gangs.",如果不确定,回答”Not sure yet."。

 

输入样例:

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4

 

输出样例:

Not sure yet.
In different gangs.
In the same gang.
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #define maxn 100005
13 #define inf 999999
14 
15 using namespace std;
16 int relation[maxn],set[maxn];
17 int n, m;
18 
19 void find(int x) {
20     if (x == set[x])return;
21     int father = set[x];
22     find(father);
23     relation[x] = (relation[father] + relation[x]) % 2;
24     set[x] = set[father];
25 }
26 
27 void Union(int x, int y) {
28     find(x), find(y);
29     int fx = set[x], fy = set[y];
30     set[fx] = fy;
31     relation[fx] = (1 + relation[y] + relation[x]) % 2;
32 }
33 
34 void init() {
35     int kase;
36     scanf("%d", &kase);
37     while (kase--) {
38         scanf("%d%d", &n, &m);
39         for (int i = 1; i <= n; i++) {
40             set[i] = i;
41             relation[i] = 0;
42         }
43         while (m--) {
44             char cmd;
45             int x, y;
46             scanf("\n%c", &cmd);
47             if (cmd == 'D') {
48                 scanf("%d%d", &x, &y);
49                 Union(y, x);
50             }
51             else if (cmd == 'A') {
52                 scanf("%d%d", &x, &y);
53                 find(x), find(y);
54                 if (set[x] != set[y])
55                     printf("Not sure yet.\n");
56                 else if (relation[x] == relation[y])
57                     printf("In the same gang.\n");
58                 else
59                     printf("In different gangs.\n");
60             }
61         }
62     }
63 }
64 
65 int main()
66 {
67     init();
68     return 0;
69 }
View Code

突然sssx……?我不是很明白这个用检索怎么做

正方形(10分)

题目内容:

给定直角坐标系中的若干整点,请寻找可以由这些点组成的正方形,并统计它们的个数。

 

输入格式:

包括多组数据,每组数据的第一行是整点的个数n(1<=n<=1000),其后n行每行由两个整数组成,表示一个点的x、y坐标。输入保证一组数据中不会出现相同的点,且坐标的绝对值小于等于20000。输入以一组n=0的数据结尾。

 

输出格式:

对于每组输入数据,输出一个数,表示这组数据中的点可以组成的正方形的数量。

 

输入样例:

4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0

 

输出样例:

1
6
1
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #define maxn 100005
13 #define inf 999999
14 
15 using namespace std;
16 int n;
17 
18 struct point {
19     int x, y;
20     int count;
21     point() {
22         x = 0, y = 0, count = 0;
23     }
24 }Hash[1100], all[1100];
25 
26 int p2hash(point x) {
27     return (x.x*x.x + x.y*x.y) % 1003;
28 }
29 
30 void put(point a) {
31     int i = p2hash(a);
32     while (Hash[i].count != 0) {
33         i++;
34     }
35     Hash[i].x = a.x, Hash[i].y = a.y;
36     Hash[i].count++;
37 }
38 
39 bool find(point a) {
40     int i = p2hash(a);
41     while (Hash[i].count != 0) {
42         if (Hash[i].x == a.x&&Hash[i].y == a.y)
43             return true;
44         i++;
45     }
46     return false;
47 }
48 
49 void init() {
50     int ans = 0;
51     for (int i = 1; i <= n; i++) {
52         int x, y;
53         scanf("%d%d", &x, &y);
54         all[i].x = x, all[i].y = y;
55         put(all[i]);
56     }
57     for(int i=1;i<=n;i++)
58         for (int j = 1; j <= n; j++) {
59             if (i == j)continue;
60             point a1, a2;
61             int dx = all[j].x - all[i].x, dy = all[i].y - all[j].y;
62             a1.x = all[i].x + dy;
63             a1.y = all[i].y + dx;
64             a2.x = all[j].x + dy;
65             a2.y = all[j].y + dx;
66             if (find(a1) && find(a2))
67                 ans++;
68         }
69     printf("%d\n", ans/4);
70 }
71 
72 int main()
73 {
74     while(scanf("%d",&n)&&n)
75         init();
76     return 0;
77 }
View Code

两个点确定另外两个点,然后检索。

 

猜你喜欢

转载自www.cnblogs.com/yalphait/p/10074025.html
今日推荐