您的位置:首页 > 其它

浙大2012上机 PAT1034. Head of a Gang (30)

2014-03-22 13:49 369 查看
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 threthold 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

总体思路:
需要对人员作离散化处理,然后用并查集处理即可。注意输出时要按名字的字母序排列。
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
const int MAXN=3000;
using namespace std;
int set[MAXN];
int height[MAXN];
map<int,int> m;
map<string,int> person;//对人名进行离散化处理
map<int,string> itp;
void init(int n)
{
for(int i=0;i<=n;i++)
{
set[i]=i;
height[i]=0;
}
}
int cha(int x)
{
if(set[x]==x)
return x;
else
return set[x]=cha(set[x]);
}
void unite(int x,int y)
{
if(cha(x)==cha(y))
return;
int tx=cha(x);
int ty=cha(y);
if(height[ty]>height[tx])
{
set[tx]=ty;
}
else
{
set[ty]=tx;
if(height[tx]==height[ty])
height[tx]++;
}
}
bool isSame(int x,int y)
{
return cha(x)==cha(y);
}
struct gang
{
string name;
int num;
bool operator < (const gang& rhs) const
{
return name<rhs.name;
}
};
vector<gang> headOfGang;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
#endif
int N(0),K(0);
cin>>N>>K;
string str1,str2;
int t;
int i(0);
init (N*3);
while(N--)
{
cin>>str1>>str2>>t;
if(person.find(str1)==person.end())
{
itp[i]=str1;
person[str1]=i++;
}
if(person.find(str2)==person.end())
{
itp[i]=str2;
person[str2]=i++;
}
int t1=person[str1];
int t2=person[str2];
m[t1]+=t;
m[t2]+=t;
unite(t1,t2);
}
vector<int> coll;
int num=person.size();//总人数
for(int i=0;i<num;i++)
{
if(set[i]==i)
coll.push_back(i);
}
for(int i=0;i<coll.size();i++)
{
int cnt(0);
int index=coll[i];
int totaltime(0);
for(int j=0;j<num;j++)
{
if(isSame(coll[i],j))
{
cnt++;
totaltime+=m[j];
if(m[j]>m[index])
index=j;
}
}
if(cnt>2&&totaltime>K*2)
{
gang t;
t.name=itp[index];
t.num=cnt;
headOfGang.push_back(t);
}
}
cout<<headOfGang.size()<<endl;
sort(headOfGang.begin(),headOfGang.end());
for(int i=0;i<headOfGang.size();i++)
{
cout<<headOfGang[i].name<<" "<<headOfGang[i].num<<endl;
}
return 0;
}


第一次写时的代码如下:

//错误点主要集中在下列几个方面:
//段错误:是因为开的空间太小了,数组访问越界!!!
//题意理解有偏差,名字是三个大写字母,没说三个大写字母一样
//所以需要离散化处理,具体做法是开两个map做映射。
//这种思路很好。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<queue>
#include<functional>
#include<string>
#include<iomanip>
const int INF = 0x7fffffff;
const int MAXN = 2010;
using namespace std;
int set[MAXN];
int height[MAXN];
int r[MAXN];
bool flag[MAXN];
struct info
{
string name;
int d;
bool operator < (const info& rhs) const
{
return name < rhs.name;
}
};
vector<info> coll1;
void init(int n)
{
for (int i = 0; i < n; i++)
{
set[i] = i;
height[i] = 0;
r[i] = 0;
}
}
int cha(int x)
{
if (x == set[x])
return x;
else
return set[x]=cha(set[x]);
}
void unite(int x, int y)
{
x = cha(x);
y = cha(y);
if (x == y)
return;
if(height[y]>height[x])
{
set[x] = y;
}
else
{
set[y] = x;
if (height[x] == height[y])
height[x]++;
}
}
bool isSame(int x, int y)
{
return cha(x) == cha(y);
}
map<int, string> mis;
map<string, int> msi;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
int N(0), K(0);
cin >> N >> K;
string a, b;
int t(0);

init(MAXN);
int index(0);
int xx(0), yy(0);
for (int i = 0; i < N; i++)
{
cin >> a >> b>>t;
if (msi.find(a) == msi.end())
{
index++;
mis[index] = a;
msi[a] = index;
xx = index;
}
else
{
xx = msi[a];
}
if (msi.find(b) == msi.end())
{
index++;
mis[index] = b;
msi[b] = index;
yy = index;
}
else
{
yy = msi[b];
}
unite(xx,yy);
r[xx] += t;
r[yy] += t;
}
vector<int> coll;
for (int j = 1; j < mis.size()+1; j++)
{
if (set[j] == j)
coll.push_back(j);
}
if (coll.empty())
{
cout << 0 << endl;
}
else
{
coll1.clear();
for (int i = 0; i < coll.size(); i++)
{
int max = r[coll[i]];
int index = coll[i];
int cnt(0);
int num(0);
for (int j = 1; j <= msi.size(); j++)
{
if (isSame(index, j))
{
num = num  + r[j];
cnt++;
if (r[j]>max)
{
max = r[j];
index = j;
}
}
}
if (num> K*2  && cnt>2)
{
string name =mis[index];
info in;
in.name = name;
in.d = cnt;
coll1.push_back(in);
}
}
cout << coll1.size() << endl;
std::sort(coll1.begin(), coll1.end());
for (int x = 0; x < coll1.size(); x++)
{
cout << coll1[x].name << " " << coll1[x].d << endl;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: