1. 程式人生 > >1139 First Contact (30 分)

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,A] D 這種情況,也就是說  應該是四個不同的人,但是隻有三個不同的人。改到自閉。

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

#define rep(i,a,b) for(int i=a;i<b;++i)

const int N=310;
int rel[N*N][2],gen[N*N][2];

int r1[N*N],r2[N*N];

int mp[N*2][N*2];

map<int,int>ID1,ID2;

int ans[N*N][2];

char s1[10],s2[10];

int cal(char *s,int len){
	int sum=0;
	if(len==4){
		rep(i,0,4){
			sum=sum*10+s[i]-'0'; 
		}
	}else{
		rep(i,1,5){
			sum=sum*10+s[i]-'0';
		} 
	}
	return sum;
}

int main() {
	int n,m;
	scanf("%d %d",&n,&m);
	
	rep(i,0,m){
		scanf("%s %s",s1,s2);
		int len1=strlen(s1),len2=strlen(s2);
		
		if(len1!=4) gen[i][0]=-1;
		else gen[i][0]=1;
		
		if(len2!=4) gen[i][1]=-1;
		else gen[i][1]=1;
		
		rel[i][0]=cal(s1,len1),rel[i][1]=cal(s2,len2);
		//scanf("%d %d",&rel[i][0],&rel[i][1]);
	}

	int cnt1=0,cnt2=0;
	rep(i,0,m) {
		if(gen[i][0]<0) {
			r2[cnt2++]=rel[i][0];
		} else {
			r1[cnt1++]=rel[i][0];
		}

		if(gen[i][1]<0) {
			r2[cnt2++]=rel[i][1];
		} else {
			r1[cnt1++]=rel[i][1];
		}
	}

	int id1=1,id2=2;

	sort(r1,r1+cnt1);
	sort(r2,r2+cnt2);

	cnt1=unique(r1,r1+cnt1)-r1;
	cnt2=unique(r2,r2+cnt2)-r2;
	//!!!
/*
	printf("**cnt1:%d cnt2:%d\n",cnt1,cnt2);
	rep(i,0,cnt1)printf("%d%c",r1[i],i==cnt1-1?'\n':' ');
	rep(i,0,cnt2)printf("%d%c",r2[i],i==cnt2-1?'\n':' ');
*/
	rep(i,0,cnt1) {
		ID1[r1[i]]=id1;
		id1+=2;
	}
	rep(i,0,cnt2) {
		ID2[r2[i]]=id2;
		id2+=2;
	}

	rep(i,0,m) {
		int p1=gen[i][0]>0?ID1[rel[i][0]]:ID2[rel[i][0]],p2=gen[i][1]>0?ID1[rel[i][1]]:ID2[rel[i][1]];
		mp[p1][p2]=mp[p2][p1]=1;
	//	printf("p1:%d p2:%d\n",p1,p2);
	}

	int T;
	scanf("%d",&T);
	while(T--) {
		
		//scanf("%d %d",&u,&v);
		scanf("%s %s",s1,s2);
		int len1=strlen(s1),len2=strlen(s2);
		int u=cal(s1,len1),v=cal(s2,len2);
		
		
		int cnt=0;
		if(len1*len2==20) {
			int p1,p2;
		//	printf("p1:%d p2:%d\n",p1,p2);
			if(len1==4) {
				p1=ID1[u],p2=ID2[v];
				for(int i=1; i<id1; i+=2) {
					if(p1!=i&&mp[p1][i])
						for(int j=2; j<id2; j+=2) {
							if(p2!=j&&mp[p2][j]) {
								if(mp[i][j]) {
									ans[cnt][0]=i;
									ans[cnt][1]=j;
									cnt++;
								}
							}
						}
				}
				printf("%d\n",cnt);
				rep(i,0,cnt) {
					int t1=(ans[i][0]+1)/2-1,t2=(ans[i][1])/2-1;
					printf("%04d %04d\n",r1[t1],r2[t2]);
				}
			} else {
				p1=ID2[u],p2=ID1[v];
				for(int i=2; i<id2; i+=2) {
					if(p1!=i&&mp[p1][i])
						for(int j=1; j<id1; j+=2) {
							if(p2!=j&&mp[p2][j]) {
								if(mp[i][j]) {
									ans[cnt][0]=i;
									ans[cnt][1]=j;
									cnt++;
								}
							}
						}
				}
				printf("%d\n",cnt);
				rep(i,0,cnt) {
					int t1=(ans[i][0])/2-1,t2=(ans[i][1]+1)/2-1;
					printf("%04d %04d\n",r2[t1],r1[t2]);
				}
			}
		} else {
			int p1,p2;
			if(len1==4) {
				p1=ID1[u],p2=ID1[v];
				for(int i=1; i<id1; i+=2) {
					if(i!=p1&&mp[p1][i])
						for(int j=1; j<id1; j+=2) {
							if(j!=p2&&j!=p1&&i!=p2&&mp[p2][j]) {
								if(mp[i][j]) {
									ans[cnt][0]=i;
									ans[cnt][1]=j;
									cnt++;
								}
							}
						}
				}
				printf("%d\n",cnt);
				rep(i,0,cnt) {
					int t1=(ans[i][0]+1)/2-1,t2=(ans[i][1]+1)/2-1;
					printf("%04d %04d\n",r1[t1],r1[t2]);
				}

			} else {
				p1=ID2[u],p2=ID2[v];
				for(int i=2; i<id2; i+=2) {
					if(p1!=i&&mp[p1][i])
						for(int j=2; j<id2; j+=2) {
							if(j!=p2&&j!=p1&&i!=p2&&mp[p2][j]) {
								if(mp[i][j]) {
									ans[cnt][0]=i;
									ans[cnt][1]=j;
									cnt++;
								}
							}
						}
				}
				printf("%d\n",cnt);
				rep(i,0,cnt) {
					int t1=(ans[i][0])/2-1,t2=(ans[i][1])/2-1;
					printf("%04d %04d\n",r2[t1],r2[t2]);
				}
			}
		}
	}
	return 0;
}