1139 First Contact(30 分)
Posted 8023spz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
当初考试的时候只得了一部分的分,现在拿出来重新做,思路清晰了很多,对于A,找一个同性好友C,C再找个好友D,D要是B的同性好友,男女编号绝对值可能一样,所以女编号用加10000来区分,但是0000和-0000,通过int读取是无法区分性别的,所以选择字符串读取。
用vector存某人的好友,也方便查找给定一个编号判断是否有这个好友。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; typedef pair<int,int> P; vector<int> s[20000];///存好友 下标代表本人 P ans[90000];///存答案 int vis[20000];///标记是否排序过 int n,m,k,num; char a[6],b[6]; inline int change(char* t) {///字符串转换为int int d = 0,i = 0; if(t[0] == ‘-‘)d = 1,i ++;///负数直接绝对值加10000来区分 while(t[i]) { d = d * 10 + t[i ++] - ‘0‘; } return d; } inline bool check(int x,int y) {///检查是否同性 以10000为界限 if(x >= 10000 && y >= 10000 || x < 10000 && y < 10000)return true; return false; } int main() { scanf("%d%d",&n,&m); for(int i = 0;i < m;i ++) { scanf("%s%s",a,b); int aa = change(a); int bb = change(b); s[aa].push_back(bb); s[bb].push_back(aa); } scanf("%d",&k); while(k --) { scanf("%s%s",a,b); int aa = change(a); int bb = change(b); if(!vis[aa]) { vis[aa] = 1; sort(s[aa].begin(),s[aa].end()); } num = 0; for(int i = 0;i < s[aa].size();i ++) { if(s[aa][i] != bb && check(aa,s[aa][i])) {///先找A的同性好友C 要保证不是B AB可能同性 int t = s[aa][i]; if(!vis[t]) { vis[t] = 1; sort(s[t].begin(),s[t].end()); } for(int j = 0;j < s[t].size();j ++) { ///找C的好友D 保证D不等于A 如果D和B同性 且是B好友则满足 if(s[t][j] != aa && check(bb,s[t][j]) && find(s[s[t][j]].begin(),s[s[t][j]].end(),bb) != s[s[t][j]].end()) { ans[num ++] = P(t,s[t][j]); } } } } printf("%d ",num); for(int i = 0;i < num;i ++) { printf("%04d %04d ",ans[i].first % 10000,ans[i].second % 10000); } } }
以上是关于1139 First Contact(30 分)的主要内容,如果未能解决你的问题,请参考以下文章
1139 First Contact (30 分)难度: 一般 / 知识点: 模拟
Codeforces1139D_CF1139DSteps to One (Mobius_DP)
Entity Framework - CTP4 - Code First - 如何关闭自动复数?
webService 客户端调用及异常信息First Element must contain the local name, Envelope , but found definitions(示例代