PAT 1139 First Contact[难][模拟]
1139 First Contact(30 分)
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a girl B, he would usually not contact her directly in the first place. Instead, he might ask another boy C, one of his close friends, to ask another girl D, who was a friend of both B and C, to send a message to B -- quite a long shot, isn't it? Girls would do analogously.
Here given a network of friendship relations, you are supposed to help a boy or a girl to list all their friends who can possibly help them making the first contact.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (1 < N ≤ 300) and M, being the total number of people and the number of friendship relations, respectively. Then M lines follow, each gives a pair of friends. Here a person is represented by a 4-digit ID. To tell their genders, we use a negative sign to represent girls.
After the relations, a positive integer K (≤ 100) is given, which is the number of queries. Then K lines of queries follow, each gives a pair of lovers, separated by a space. It is assumed that the first one is having a crush on the second one.
Output Specification:
For each query, first print in a line the number of different pairs of friends they can find to help them, then in each line print the IDs of a pair of friends.
If the lovers A and B are of opposite genders, you must first print the friend of A who is of the same gender of A, then the friend of B, who is of the same gender of B. If they are of the same gender, then both friends must be in the same gender as theirs. It is guaranteed that each person has only one gender.
The friends must be printed in non-decreasing order of the first IDs, and for the same first ones, in increasing order of the seconds ones.
Sample Input:
10 18
-2001 1001
-2002 -2001
1004 1001
-2004 -2001
-2003 1005
1005 -2001
1001 -2003
1002 1001
1002 -2004
-2004 1001
1003 -2002
-2003 1003
1004 -2002
-2001 -2003
1001 1003
1003 -2001
1002 -2001
-2002 -2003
5
1001 -2001
-2003 1001
1005 -2001
-2002 -2004
1111 -2003
Sample Output:
4
1002 2004
1003 2002
1003 2003
1004 2002
4
2001 1002
2001 1003
2002 1003
2002 1004
0
1
2003 2001
0
题目大意:首先给出N个人之间的M个关系,表示A和B之间又关系(其中负值表示的是女生),再给出K个查询,表示A要追求B,就算有直接关系,但是也不使用,使用第三方关系。
输入要求:如果AB是异性,那么先输出的就是和A同性的人,如果AB是同性,那么他们的朋友输出的都必须是同性。
//这里的关系也不是一一对应,好像也不能用map.这个很难去遍历啊,首先需要找到A的一个同性朋友,如果AB异性,那么再找到异性朋友,再找到和B的同性朋友。
如果AB是同性,那么就一直找同性就可以了。
代码来自:https://www.liuchuo.net/archives/4210
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <unordered_map> #include<stdio.h> #include<stdlib.h> using namespace std; unordered_map<int, bool> arr; struct node { int a, b; }; bool cmp(node x, node y) { return x.a != y.a ? x.a < y.a : x.b < y.b; } int main() { int n, m, k; scanf("%d%d", &n, &m); vector<int> v[10000]; for (int i = 0; i < m; i++) { string a, b; cin >> a >> b; if (a.length() == b.length()) {//同性之间使用邻接表来存储。 v[abs(stoi(a))].push_back(abs(stoi(b))); v[abs(stoi(b))].push_back(abs(stoi(a))); } //居然还有这种形式的邻接矩阵 //使用前4个0,表示一方,后四个0,表示另一方。不会出现重复。 arr[abs(stoi(a)) * 10000 + abs(stoi(b))] = arr[abs(stoi(b)) * 10000 + abs(stoi(a))] = true; } scanf("%d", &k); for (int i = 0; i < k; i++) { int c, d; cin >> c >> d; vector<node> ans; for (int j = 0; j < v[abs(c)].size(); j++) { for (int k = 0; k < v[abs(d)].size(); k++) { if (v[abs(c)][j] == abs(d) || abs(c) == v[abs(d)][k]) continue; //如果下一个正好是对方,那么就跳过。 if (arr[v[abs(c)][j] * 10000 + v[abs(d)][k]] == true) //如果在它们俩的同性之间有交集。 ans.push_back(node{v[abs(c)][j], v[abs(d)][k]}); //以这两个为参数新建一个结构体插入。 } } sort(ans.begin(), ans.end(), cmp); printf("%d\n", int(ans.size()));//需要强制转换为int并以%d输出。 for(int j = 0; j < ans.size(); j++) printf("%04d %04d\n", ans[j].a, ans[j].b); } return 0; }
//真是很厉害了。
1.使用unordered_map<int, bool> arr替代二维数组可避免内存超限。
2.这个arr的利用方法很特别。
3.将所有关系都存进这个arr形成一个邻接矩阵;
4.将所有同性关系存进去邻接表里
5.找到A认识的同性C,B认识的同性D,判断一下二者是否相等就可以了。
6.id会有0000和-0000,此时无法区分男女,所以需要用字符串输入。
7.使用stoi函数将字符串转化为int.
代码来自:
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <map> #include <set> #include <queue> #include <vector> #include <stack> #include <algorithm> #include <iostream> using namespace std; #define long long ll const int MAXN = 1e4 + 10; const int INF = 0x7fffffff; const int dx[]={0, 1, 0, -1}; const int dy[]={1, 0, -1, 0}; int n, m; struct NODE{ vector<int>same; }node[MAXN]; map<int, map<int, bool> >mp;//居然还可以这样来做邻接矩阵,厉害了。 struct ANS{ int idx, idy; }ans[MAXN]; bool cmp(ANS x, ANS y){ if(x.idx==y.idx) return x.idy<y.idy; return x.idx<y.idx; } int main(){ while(~scanf("%d%d", &n, &m)){ mp.clear(); for(int i=0; i<m; i++){ char x[12], y[12]; scanf("%s%s", x, y); int s=abs(atoi(x)), t=abs(atoi(y)); mp[s][t]=mp[t][s]=true;//表示两者有联系方式 if(strlen(x)==strlen(y)){///同性 node[s].same.push_back(t); node[t].same.push_back(s); } } int k; scanf("%d", &k); while(k--){ int s, t, cnt=0; scanf("%d%d", &s, &t); s=abs(s), t=abs(t); for(int i=0; i<node[s].same.size(); i++){///s认识的同性x for(int j=0; j<node[t].same.size(); j++){///t认识的同性y int x=node[s].same[i], y=node[t].same[j]; if(x==t || s==y) continue;//如果两者有直接关系,那么跳过。 if(mp[x][y]) ans[cnt].idx=x, ans[cnt++].idy=y;///判断x和y是否认识 } } sort(ans, ans+cnt, cmp); printf("%d\n", cnt); for(int i=0; i<cnt; i++) printf("%04d %04d\n", ans[i].idx, ans[i].idy); } } return 0; }
//这个构建邻接矩阵也十分不错,不会出现超市的 情况。厉害。