1158. Telefraud Detection (25)-PAT甲级真题 (遍历边 哈希表)

题意

给出阈值k、顶点个数n、边个数m。然后给出m条边信息:a给b打了l分钟。如果a给b打的电话时间和不超过5min则称a给b打了一个短电话。记录节点i的短电话数s,同时记录这些短电话的回打数量b,两者比例大于等于5则说明节点i可能是诈骗犯。
诈骗犯互相打过电话的称他们是一伙(gang),找到所有gang并按照一定要求(gang中最小元素的大小)排序输出。

思路

  • 人数≤1k,选择邻接矩阵存图。
  • 设置两个数组cnt与back记录打出去的短电话数目与回打数目,对比得出嫌疑人名单recif(cnt[i] > k && cnt[i] >= back[i] * 5) rec.push_back(i);
  • DFS得到深度优先生成森林,按题目要求对森林进行排序(按每棵树最小元素从小到大排列)。
  • 或者使用并查集,见解法2.

总结

  • 仔细读题,漏掉这句话debug了15min:If no one is detected, output None instead.
  • 即使传入sort的前两个参数是指针iterator,cmp也可以传vector<int>进行比较,而不用写成vector<int>::iterator的形式。
  • 巧妙避免小数运算的方法:cnt[i] >= back[i] * 5 替代(double)cnt[i] / (double)back[i] >= 5
  • 优先级 : 强制类型转化 > 乘除法

解法1 - DFS

#include<bits/stdc++.h>
using namespace std;
int n, m, k, a, b, l, cman, cnt[1001], back[1001], gra[1001][1001];
bool st[1001];
vector<int> rec, mem;
vector<vector<int>> ans;
void DFS(int s){
	st[s] = true;
	mem.push_back(s);
	for(int i = 0; i < rec.size(); i ++)
		if(!st[rec[i]] && gra[s][rec[i]] && gra[rec[i]][s])
			DFS(rec[i]);
}
int main(){
	cin>>k>>n>>m;
	while(m--){
		scanf("%d%d%d", &a, &b, &l);
		gra[a][b] += l;
	}
	for(int i = 1; i <= n; i ++){
		for(int j = 1; j <= n; j ++){
			if(gra[i][j] && gra[i][j] <= 5){
				cnt[i] ++;
				if(gra[j][i]) back[i] ++;
			}
		}
		if(cnt[i] > k && cnt[i] >= back[i] * 5) rec.push_back(i);
	}
	if(!rec.size()){
		puts("None");
		return 0;
	}
	for(int i = 0; i < rec.size(); i ++)
		if(!st[rec[i]]){
			mem.clear();
			DFS(rec[i]);
			sort(mem.begin(), mem.end());
			ans.push_back(mem);
		}
	for(auto i : ans)
		for(int j = 0; j < i.size(); j ++)
			printf("%d%c", i[j], j != i.size() - 1 ? ' ' : '\n');
	return 0;
}

解法2 - 并查集

//23:27 - 
#include<bits/stdc++.h>
using namespace std;
int n, m, k, a, b, l, cman, p[1001], cnt[1001], back[1001], gra[1001][1001];
bool st[1001];
vector<int> rec, mem;
vector<vector<int>> ans;
int find(int x){
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}
void Union(int a, int b){
	a = find(a), b = find(b);
	if(a < b) p[b] = a;
	else p[a] = b;
}
int main(){
	cin>>k>>n>>m;
	while(m--){
		scanf("%d%d%d", &a, &b, &l);
		gra[a][b] += l;
	}
	for(int i = 1; i <= n; i ++){
		p[i] = i;
		for(int j = 1; j <= n; j ++){
			if(gra[i][j] && gra[i][j] <= 5){
				cnt[i] ++;
				if(gra[j][i]) back[i] ++;
			}
		}
		if(cnt[i] > k && cnt[i] >= back[i] * 5) rec.push_back(i);
	}
	if(!rec.size()){
		puts("None");
		return 0;
	}
	for(int i = 0; i < rec.size(); i ++)
		for(int j = 0; j < rec.size(); j ++)
			if(gra[rec[i]][rec[j]] && gra[rec[j]][rec[i]])
				Union(rec[i], rec[j]);
	for(int i = 0; i < rec.size(); i ++)
		if(!st[rec[i]]){
			printf("%d", rec[i]);
			for(int j = i + 1; j < rec.size(); j ++){
				if(find(rec[i]) == find(rec[j])){
					printf(" %d", rec[j]);
					st[rec[j]] = true;
				}
			}
			puts("");
		}
	return 0;
}

题目

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records.

A person must be detected as a suspect if he/she makes more than K short phone calls to different people everyday, but no more than 20% of these people would call back. And more, if two suspects are calling each other, we say they might belong to the same gang. A makes a short phone call to B means that the total duration of the calls from A to B is no more than 5 minutes.

Input Specification:

Each input file contains one test case. For each case, the first line gives 3 positive integers K (≤500, the threshold(阈值) of the amount of short phone calls), N (≤103, the number of different phone numbers), and M (≤105, the number of phone call records). Then M lines of one day’s records are given, each in the format:

caller receiver duration

where caller and receiver are numbered from 1 to N, and duration is no more than 1440 minutes in a day.

Output Specification:

Print in each line all the detected suspects in a gang, in ascending order of their numbers. The gangs are printed in ascending order of their first members. The numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

If no one is detected, output None instead.

Sample Input 1:

5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1

Sample Output 1:

3 5
6

Note: In sample 1, although 1 had 9 records, but there were 7 distinct receivers, among which 5 and 15 both had conversations lasted more than 5 minutes in total. Hence 1 had made 5 short phone calls and didn’t exceed the threshold 5, and therefore is not a suspect.

Sample Input 2:

5 7 8
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 1 1
3 1 1

Sample Output 2:

None
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值