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
问题:
1)如果是同性喜欢,不能第二个人是目标target;
2)输出时需要设置%04d,因为会存在0000和-0000的情况。
这次提交的代码很长,x待改进的
1)之所以需要设置id到int的转换,是因为想要使用vector,使用id当作索引;这样相对有些复杂。可以使用map<string, map<string,int> >进行索引。此时的string即为输入的字符串
2)因为设置了深度是4,所以不需要写DFS,可以使用两层for循环,第一层为第二个人,第二层为第三个人,分别由第一个人和第四个人的联系人得到,然后在循环的最内部判断是否存在第二个人和第三个人联系的方式即可。
柳神的做法可以解决 绝对值相同的查询吗?表示怀疑。
另外这种也可以使用10000*id1+id2和10000*id2+id1的方式表示id1和id2是否存在关系。
在map耗费空间时,可以使用unordered_map
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
map<string,int> name_map;
map<int,string> map_name;
vector < vector<int> > vec;
vector < vector<int> > ans;
vector < int > ans_tmp;
vector<bool> visit={false};
queue <int> question;
int N,M,K;
char girl='-';
struct ANS{
int id1;
int id2;
};
vector<ANS> ans_vec;
bool cmp(ANS &ans1,ANS &ans2){
if (ans1.id1<ans2.id1) return true;
else if ((ans1.id1==ans2.id1)&&(ans1.id2<ans2.id2)) return true;
else return false;
}
void DFS(int current_id,int target_id, int step){
if((step==3)&&(current_id==target_id)){
ans_tmp.push_back(current_id);
ans.push_back(ans_tmp);
ans_tmp.pop_back();
return;
}else if (step==3){
return;
}
ans_tmp.push_back(current_id);
visit[current_id]=true;
int even = step%2;
if(even==1){
if(current_id<N){
for(int i=0;i<vec[current_id].size();i++){
if((vec[current_id][i]>=N)&&(!visit[vec[current_id][i]])){
DFS(vec[current_id][i],target_id,step+1);
}
}
}else{
for(int i=0;i<vec[current_id].size();i++){
if((vec[current_id][i]<N)&&(!visit[vec[current_id][i]])){
DFS(vec[current_id][i],target_id,step+1);
}
}
}
}else {
if(current_id<N){
for(int i=0;i<vec[current_id].size();i++){
if((vec[current_id][i]<N)&&(!visit[vec[current_id][i]])){
DFS(vec[current_id][i],target_id,step+1);
}
}
}else{
for(int i=0;i<vec[current_id].size();i++){
if((vec[current_id][i]>=N)&&(!visit[vec[current_id][i]])){
DFS(vec[current_id][i],target_id,step+1);
}
}
}
}
ans_tmp.pop_back();
visit[current_id]=false;
}
int id2int(int id){
string name = map_name[id];
int len = name.size();
string name_final;
if(name[0]==girl) name_final = name.substr(1, len);
else name_final = name;
return stoi(name_final);
}
void LES_DFS(int current_id,int target_id, int step){
//cout<<" LES current_id "<<current_id<<"target_id "<<target_id<<"step "<<step<<endl;
//cout<<" LES current_name "<<map_name[current_id]<<"target_id "<<map_name[target_id]<<"step "<<step<<endl;
if((step==3)&&(current_id==target_id)){
ans_tmp.push_back(current_id);
ans.push_back(ans_tmp);
// cout<<"LES Path"<<endl;
// for(int i =0;i<ans_tmp.size();i++){
// cout<<map_name[ans_tmp[i]]<<endl;
// }
ans_tmp.pop_back();
return;
}else if (step==3){
return;
}
ans_tmp.push_back(current_id);
visit[current_id]=true;
if(current_id<N){
for(int i=0;i<vec[current_id].size();i++){
if((vec[current_id][i]<N)&&(!visit[vec[current_id][i]])){
LES_DFS(vec[current_id][i],target_id,step+1);
}
}
}else{
for(int i=0;i<vec[current_id].size();i++){
if((vec[current_id][i]>=N)&&(!visit[vec[current_id][i]])){
LES_DFS(vec[current_id][i],target_id,step+1);
}
}
}
visit[current_id]=false;
ans_tmp.pop_back();
}
int main(int argc, char** argv) {
cin>>N>>M;
string name1,name2;
int tmp_id1,tmp_id2;
vec.resize(2*N);
visit.resize(2*N);
int boy_id = 0,girl_id = N;
for(int i=0;i<M;i++){
cin>>name1>>name2;
if(name_map.count(name1)){
tmp_id1 = name_map[name1];
}else{
if(name1[0]==girl){
tmp_id1=girl_id;
name_map[name1]=girl_id++;
}else{
tmp_id1=boy_id;
name_map[name1]=boy_id++;
}
}
if(name_map.count(name2)){
tmp_id2 = name_map[name2];
}else{
if(name2[0]==girl){
tmp_id2=girl_id;
name_map[name2]=girl_id++;
}else{
tmp_id2=boy_id;
name_map[name2]=boy_id++;
}
}
map_name[tmp_id1] = name1;
map_name[tmp_id2] = name2;
vec[tmp_id1].push_back(tmp_id2);
vec[tmp_id2].push_back(tmp_id1);
}
int quiry_num;
cin>>quiry_num;
for(int i =0;i<quiry_num;i++){
string name1,name2;
int tmp_id1,tmp_id2;
cin>>name1>>name2;
if(name_map.count(name1)){
tmp_id1 = name_map[name1];
}else tmp_id1 = 3*N;
if(name_map.count(name2)){
tmp_id2 = name_map[name2];
}else tmp_id2 = 3*N;
question.push(tmp_id1);
question.push(tmp_id2);
}
for(int i =0;i<quiry_num;i++){
int tmp_id1 = question.front();
question.pop();
int tmp_id2 = question.front();
question.pop();
if((tmp_id1==3*N)||(tmp_id2==3*N)){
cout<<0<<endl;
continue;
}
else if((tmp_id1<N)&&(tmp_id2<N))
LES_DFS(tmp_id1,tmp_id2,0);
else if((tmp_id1>=N)&&(tmp_id2>=N))
LES_DFS(tmp_id1,tmp_id2,0);
else
DFS(tmp_id1,tmp_id2,0);
cout<<ans.size()<<endl;
if(!ans.empty()){
for(int i=0;i<ans.size();i++){
ANS ans_tmp;
ans_tmp.id1 = id2int(ans[i][1]);
ans_tmp.id2 = id2int(ans[i][2]);
ans_vec.push_back(ans_tmp);
}
}
sort(ans_vec.begin(),ans_vec.end(),cmp);
for(int i=0;i<ans_vec.size();i++){
printf("%04d %04d\n",ans_vec[i].id1,ans_vec[i].id2);
}
ans_vec.clear();
ans.clear();
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
vector<string> fri, sec;
map<string, map<string, int>>G;
int N, M;
typedef struct{
string a, b;
}node;
node list[10000];
bool cmp(node &nd1, node &nd2){
if(nd1.a != nd2.a){
return nd1.a < nd2.a;
}else{
return nd1.b < nd2.b;
}
}
int main(){
scanf("%d%d", &N, &M);
for(int i = 0; i < M; i++){
string v1, v2;
cin >> v1 >> v2;
G[v1][v2] = 1;
G[v2][v1] = 1;
}
int K;
scanf("%d", &K);
for(int i = 0; i < K; i++){
string v1, v2;
cin >> v1 >> v2;
fri.clear();
sec.clear();
map<string, int>::iterator it;
for(it = G[v1].begin(); it != G[v1].end(); it++){
if(it->first != v1 && it->first != v2 && (it->first[0] != '-' && v1[0] != '-' || it->first[0] == '-' && v1[0] == '-'))
fri.push_back(it->first);
}
for(it = G[v2].begin(); it != G[v2].end(); it++){
if(it->first != v1 && it->first != v2 &&(it->first[0] != '-' && v2[0] != '-' || it->first[0] == '-' && v2[0] == '-'))
sec.push_back(it->first);
}
int pt = 0;
for(int j = 0; j < fri.size(); j++){
for(int k = 0; k < sec.size(); k++){
if(G[fri[j]].count(sec[k]) != 0){
string s1 = fri[j], s2 = sec[k];
if(s1[0] == '-'){
s1.erase(0, 1);
}
if(s2[0] == '-'){
s2.erase(0, 1);
}
list[pt].a = s1;
list[pt++].b = s2;
}
}
}
sort(list, list + pt, cmp);
printf("%d\n", pt);
for(int k = 0; k < pt; k++){
cout << list[k].a <<" " <<list[k].b << endl;
}
}
cin >> N;
return 0;
}