1139 First Contact (30 分)(cj)

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

卡時間的dfs 沒想到可以直接用 for 循環嵌套來完成。速度會大提升。

坑點 注意 -0000 和 0000 是相同的 無法確定性別。

 pintia 甲級題最後一道 終於刷完了。[]~( ̄▽ ̄)~* 乾杯。

code

#pragma warning(disable:4996)
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#define abs(x) (x>0? x:-x)
using namespace std;
map<int, int> mp;
vector<int> varr[305];
vector<int> vres;
vector<pair<int, int>> v;
bool vis[305];
int idex = 1;
int demp[305];
void dfs(int pos, int des);
bool cmp(pair<int, int>& a, pair<int, int>& b);
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	char s1[10], s2[10];
	int x, y;
	for (int i = 0; i < m; ++i) {
		scanf("%s %s", s1, s2);
		x = atoi(s1);
		if (x == 0) {
			if (s1[0] == '-') {
				x = -10000;
			}
			else x = 10000;
		}
		y = atoi(s2);
		if (y == 0) {
			if (s2[0] == '-') {
				y = -10000;
			}
			else y = 10000;
		}
		if (mp.count(x) == 0) {
			mp[x] = idex;
			demp[idex] = x;
			++idex;
		}
		if (mp.count(y) == 0) {
			mp[y] = idex;
			demp[idex] = y;
			++idex;
		}
		varr[mp[x]].push_back(mp[y]);
		varr[mp[y]].push_back(mp[x]);
	}
	int k;
	scanf("%d", &k);
	for (int i = 0; i < k; ++i) {
		scanf("%d %d", &x, &y);
		v.clear();
		dfs(mp[x], mp[y]);
		printf("%d\n", v.size());
		sort(v.begin(), v.end(), cmp);
		for (int i = 0; i < v.size(); ++i) {
			printf("%04d %04d\n", (v[i].first), (v[i].second));
		}
	}
	system("pause");
	return 0;
}
bool cmp(pair<int, int>& a, pair<int, int>& b) {
	if (a.first == b.first) return a.second < b.second;
	return a.first < b.first;
}
void dfs(int pos, int des) {
	for (int i = 0; i < varr[pos].size(); ++i) {
		int a = varr[pos][i];
		if (a == pos || a == des) continue;
		if (demp[a] * demp[pos] < 0) continue;
		for (int j = 0; j < varr[a].size(); ++j) {
			int b = varr[a][j];
			if (b == pos || b == des) continue;
			if (demp[b] * demp[des] < 0) continue;
			for (int k = 0; k < varr[b].size(); ++k) {
				int c = varr[b][k];
				if (c == des) {
					v.push_back(make_pair(abs(demp[a])%10000, abs(demp[b])%10000));
				}
			}
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章