1139 First Contact (30 point(s))

1139 First Contact (30 point(s))

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. 以string的形式读入编号,如果该编号尚未存在,需要插入相关信息,记录结点的编号和姓名。鉴于编号是4位的,不能直接开那么大的邻接矩阵,采用map实现编号和自定下标的映射。

2. 在此以字符串形式读入起始和末尾结点,并取绝对值得到x,y,然后遍历每一组可能的数对,如果结点的性别满足且x、y、i、j互不相等,则合法。(这里可以优化一下,比如要i满足要求再搜索j)

3. 然后对数对进行排序,输出即可。

注意点:

1. 注意不能用int来读入,测试用例会存在-0000这样的输入。

2. 读入的结点可能不存在。比如用例,实际上只出现了9个结点,为什么N=10呢?是题目故意错了。我觉得它是想表达有只出现在查询里的1111不存在吧。

3. pair<int,int> 的排序重载就是先按照first排序,再按照second排序,不需自己写了。

4. 给出的起始和末尾结点也是带符号的,需要取绝对值。

大神思路: 

还是使用vector<int> graph[]解决稀疏矩阵的存储,避免使用麻烦的map来做映射(当然这样就只需要300*300的矩阵了)。

#include <bits/stdc++.h>
using namespace std;
vector<int>graph[10005];//图
bool boy[10005];//下标为ID,元素表示该ID是否是男
int N,M,K,vstart,vend;
int main(){
    scanf("%d%d",&N,&M);
    for(int i=0;i<M;++i){//读取边的数据
        string a,b;
        cin>>a>>b;
        int ia=abs(stoi(a)),ib=abs(stoi(b));//将字符串化为正整数
        graph[ia].push_back(ib);//向图中加入边
        graph[ib].push_back(ia);//向图中加入边
        boy[ia]=(a[0]!='-');//表示该人性别
        boy[ib]=(b[0]!='-');//表示该人性别
    }
    scanf("%d",&K);
    while(K--){
        scanf("%d%d",&vstart,&vend);//读取首尾结点
        vector<pair<int,int>>result;//存储符合题目要求的两个结点
        for(int i:graph[abs(vstart)])//遍历首节点的朋友
            if(i!=abs(vend)&&i!=abs(vstart)&&boy[i]==boy[abs(vstart)])//找到非首尾结点且与首节点性别相同的朋友作为第一个节点
                for(int j:graph[i])//遍历第一个节点的朋友
                    if(j!=abs(vend)&&j!=abs(vstart)&&boy[j]==boy[abs(vend)])//找到非首尾结点且与尾节点性别相同的朋友作为第二个节点
                        for(int k:graph[j])//遍历第二个节点的朋友
                            if(k==abs(vend))//尾结点是第二个节点的朋友
                                result.push_back({i,j});//i,j两个节点符合要求
        printf("%d\n",result.size());
        sort(result.begin(),result.end());//排序
        for(auto&i:result)//输出
            printf("%04d %04d\n",i.first,i.second);
    }
    return 0;
}

我的AC代码: 

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int N,M,K;
const int MAX = 307;
map<int,int> mp;//ID to index;
int gender[MAX];//index to gender;male 1 female -1
int id[MAX];//index to ID
int idx =0;
bool graph[MAX][MAX];
bool cmp(pair<int,int> p1,pair<int,int> p2){
	if(p1.first!=p2.first) return p1.first<p2.first;
	return p1.second<p2.second; 
}
int main(void){
	cin>>N>>M;string a,b;
	memset(graph,false,sizeof(graph));
	mp.clear();
	for(int i=0;i<M;i++){
		int na,nb;
		int ga=1;int gb=1;
		cin>>a;
		if(a[0]=='-'){
			a.erase(a.begin());
			ga = -1;
		} 
		na = stoi(a);
		if(mp.find(na)==mp.end()){
			mp[na]=idx;
			id[idx]=na;
			gender[idx]=ga;
			idx++;
		}
		cin>>b;
		if(b[0]=='-'){
			b.erase(b.begin());
			gb = -1;
		}
		nb = stoi(b);
		if(mp.find(nb)==mp.end()){
			mp[nb]=idx;
			id[idx]=nb;
			gender[idx]=gb;
			idx++;
		}
		graph[mp[na]][mp[nb]]=true;
		graph[mp[nb]][mp[na]]=true;
	}
	cin>>K;int start,end; 
	vector<pair<int,int> > v;
	while(K--){
		cin>>start>>end;
		if(mp.find(abs(start))==mp.end()||mp.find(abs(end))==mp.end()){
			cout<<"0"<<endl;continue;
		} 
		int x = mp[abs(start)];int y=mp[abs(end)];
		v.clear();
		for(int i=0;i<idx;i++){
			for(int j=0;j<idx;j++){
				if(x!=i&&i!=j&&j!=y&&x!=j&&i!=y&&graph[x][i]&&graph[i][j]&&graph[j][y]){
					if(gender[x]*gender[i]==1&&gender[y]*gender[j]==1) v.push_back(pair<int,int>(id[i],id[j]));
				} 
			}
		}
		sort(v.begin(),v.end(),cmp);
		cout<<v.size()<<endl;
		for(auto it:v) printf("%04d %04d\n",it.first,it.second);
	}	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值