1034 Head of a Gang

One way that the police finds the head of a gang is to check people’s phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A “Gang” is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time
where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:
For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:
8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
Sample Output 1:
2
AAA 3
GGG 3
Sample Input 2:
8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
Sample Output 2:
0
cluster 英[ˈklʌstə®]
美[ˈklʌstər]
n. (同类物丛生或聚集的) 簇,团,束,串; (人或动物的) 群,组; 辅音丛; 辅音连缀;
v. 群聚; 聚集;

threshold 英[ˈθreʃhəʊld]
美[ˈθreʃhoʊld]
n. 门槛; 门口; 阈; 界; 起始点; 开端; 起点; 入门;

头铁,第一次自己做了3个多小时,并且还只有17分,问题太多了,有时候反省想说很多,但是又觉得有些矫情。

先说过了的吧,图的遍历,虽然我用的是并查集。这题一拿到的时候感觉要处理的东西很多,看完别人的(算法笔记上)思路和代码突然发现有很简单。。。
但注意点还是很多
1.题目说最多有1000条,如果都不同,那么就有2000个点,所以数组要开到2000多
2.还是字符串和整形的转换,这里用map来转换,而且map是按字典序来排序的(虽然我做的时候忘掉了题目要求按字典序,小声逼逼),map转换时真的爽感觉,存图的时候也很舒服,我做图的题目几乎都是邻接矩阵,虽然说一般超过1000用邻接表,但是2000不怎么影响PAT的审核机制
3.G[u][i] = G[i][u] 在遍历完之后一定要清零,因为你在遍历的时候只能控制点是否访问过,而边无法控制,只能清零,防止一条边访问2次(虽然我很想弄成单向边),这一点非常重要
4.&引用的使用吧,虽然知道,但是没怎么用过

#include<iostream>
#include<map>
#include<vector>
const int maxn =2015;
using namespace std;
map<string,int> cti;
map<int,string> itc;
map<string,int> gang;
int id=1,k;
int turn(string s){
	if(!cti[s]){
		cti[s] = id;
		itc[id] = s;
		return id++;
	}
	else return cti[s];
}
int G[maxn][maxn],weight[maxn];
bool vis[maxn]={false};
void dfs(int u,int &head,int &numMember,int &totalweight){
	vis[u] = true;
	numMember++;
	if(weight[u] > weight[head]) head = u;
	for(int i=1;i<id;i++){
		if(G[u][i] ){
			totalweight+=G[u][i];
			G[u][i] = G[i][u] = 0;
			if(!vis[i]) dfs(i,head,numMember,totalweight);
		}
	}
}
void dfstrave(){
	for(int i=1;i<id;i++){
		if(!vis[i]){
			int head = i,numMember=0,totalweight=0;
			dfs(i,head,numMember,totalweight);
			if(numMember > 2 && totalweight > k){
				gang[itc[head]] = numMember;
			}
		}
	}
}
int main(){
	int n,w;
	cin>>n>>k;
	string s1,s2;
	for(int i=1;i<=n;i++){
		cin>>s1>>s2>>w;
		int id1=turn(s1);
		int id2=turn(s2);
		G[id1][id2]+=w;
		G[id2][id1]+=w;
		weight[id1]+=w;
		weight[id2]+=w;
	}
	dfstrave();
	cout<<gang.size()<<endl;
	map<string,int> :: iterator it;
	for(it = gang.begin();it!=gang.end();it++){
		cout<<it->first<<' '<<it->second<<endl;
	}
	return 0;
} 

参考
算法笔记P354

===================================================================到这里就可以不用看了,记录我自己的失败,只有17分,想用并查集来解决的童鞋可以去看看别人的

#include<iostream>
#include<map>
#include<vector>
const int maxn =2015;
using namespace std;
int p[maxn];
int num[maxn];
int findf(int k){
	while(k!=p[k]) k=p[k];
	return k;
}
void Union(int a,int b){
	int fa=findf(a);
	int fb=findf(b);
	if(num[a] > num[b] ){
		if(num[fa] < num[a]) {
			p[fa] = a;
			p[a] =a;
			p[fb] = a;
		}
		else{
			p[fb] = fa;
		}
		
	}
	else if(num[a] == num[b] ){
		if(fa != fb)  p[fa] = fb;
	}
	else{
		if(num[fb] < num[b]) {
			p[fb] = b;
			p[b] =b;
			p[fa] = b;
		}
		else{
			p[fa] = fb;
		}
	}
	
}
map<string,int> cti;
map<int,string> itc;
vector<int> bk;
vector<int> ans;
int main(){
	int n,k,i=1;
	cin>>n>>k;
	for(int t=1;t<=n;t++) p[t] = t;

	for(int t=1;t<=n;t++){
		int q;
		string a,b;	
		cin>>a>>b>>q;
		if(!cti[a]) 
		{
			cti[a]=i;
		    itc[i]=a;
		    i++;
		}
		if(!cti[b]) 
		{
			cti[b]=i;
		    itc[i]=b;
		    i++;
		}
		num[cti[a]]+=q;
		num[cti[b]]+=q;
      Union(cti[a],cti[b]);
	}
	int cnt=0;
	for(int j=1;j<= n ;j++){
		if(p[j] == j  ) {
			bk.push_back(j);
		}
	}

	for(int t=0;t<bk.size();t++){
		int nu=0,weight=0;
		for(int j=1;j<=n;j++){
			if(p[j] == bk[t]) {
				nu++;
				weight+=num[j];
			}
		}
		if(nu > 2 && weight > k ){
			cnt++;
			ans.push_back(bk[t]);
		}
	}
	cout<<cnt<<endl;
	for(int i=0;i<ans.size() ;i++){
		int nu=0;
		cout<<itc[ans[i]]<<' ';
		for(int j=1;j<=n;j++){
			if(p[j] == ans[i]) nu++;
		}
		cout<<nu<<endl;
	}
	
	return 0;
} 

真的是又臭又长,思路也很乱,我自己是模拟数据,然后用并查集做。怎么说,在审题清楚,思路清晰,没有掉进陷阱里来说,1个小时准确写出来,17分我也可以接受,但是第一次做的时候做了3个多小时,这其中很多细节,自己的问题,
比如
1.map<stirng,int> 在全局变量里是都是默认为零的,所以你纪录得时候也最好从下表1开始,0来判断是否存在,这一点是简单,到那时一开始的一开始我是从下标0来开始记录的,这一点我花了很长时间才找到问题
2.审题不清楚吧,题目要求的是一个团伙的总值要超过阈值,我理解成head要超过了,
3.自己弄的数组还是变量一定要自己知道到底代表的是什么意思

在看别人的反思的时候觉得很有道理,但是有些坑还是踩了,唉,最近也发生了不少事情,但是无论如何,都要对自己有信心,坚持。

=======================================================
2020 1 20
又走了一遍,用的并查集 但是只得了 26 分,不知道哪里错了

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
const int N = 10005;
using namespace std;
int father[N],w[N],isroot[N],sum[N];
map<string,int> m,m1;//m对应转换,m1储存头头 
int findfather(int x){
	while(x != father[x]){
		x = father[x];
	}
	return x;
}
void Union(int a,int b){
	int fa = findfather(a);
	int fb = findfather(b);
	if(w[a] > w[fa]) {
		father[fa] = a;
		father[a] = a;
		fa = a;
	}
	if(w[b] > w[fb]) {
		father[fb] = b;
		father[b] = b;
		fb = b;
	}
		if( w[fa] > w[fb]) {
		  father[fb] = fa;
		  sum[fa]+=w[fa];
		  sum[fa]+=w[fb];
		}
		else {
			father[fa] = fb;
			sum[fb]+=w[fb];
			sum[fb]+=w[fa];
		}
}
int main(){
	int n,k,cnt=0;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		father[i] = i;
	}
	for(int i=1;i<=n;i++){
		string n1,n2;
		int w1;
		cin>>n1>>n2>>w1;
		if(m[n1] == 0) m[n1] = ++cnt;
		if(m[n2] == 0) m[n2] = ++cnt;
		w[m[n1]]+=w1;
		w[m[n2]]+=w1;
		Union(m[n1],m[n2]);
	}
	for(int i=1;i<=cnt;i++){
		isroot[findfather(i)]++;
	}
	map<string,int>::iterator it;	
	for(int i=1;i<=cnt;i++){
		if(isroot[i] >2 && sum[i] > k) {
			string name;
			for(it = m.begin();it!=m.end();it++) {
				if(it->second == i){
					name = it->first;
					break;
				}
			}
			m1[name] = isroot[i];
		}
	}
	cout<<m1.size()<<endl;
	for(it = m1.begin();it!=m1.end();it++){
		cout<<it->first<<' '<<it->second<<endl;
	} 
	return 0 ;
} 

敢无视那个阈值是整个gang的通话的总和,其他的就没什么

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值