九度oj 1446
2015-08-02 15:56
204 查看
题目描述:
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.
输入:
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.
输出:
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.
样例输入:
样例输出:
来源:
2012年浙江大学计算机及软件工程研究生机试真题
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 2003
#define NL 6
class People
{
public:
char name[NL];
int tTime;//总通话时间
int father;//用于并查集
void init(char n[NL], int time, int f)
{
for (int i=0; i<NL; i++)
{
name[i] = n[i];
}
tTime = time;
father = f;
}
};
People tree[MAX];//并查集
class Gang
{
public:
int head;//head在tree中的位置
int num;//人数
int time;
Gang()
{
head = -1;
num = 0;
time = 0;
}
void clear()
{
head = -1;
num = 0;
time = 0;
}
bool operator < (Gang g) const
{
if (head == -1)
{
return false;
}
return strcmp(tree[head].name, tree[g.head].name) < 0;
}
};
Gang g[MAX];//团伙
int num;//当前并查集中元素个数
int findPeople(char name[NL])//在并查集中找到name对应people的位置
{
for (int i=0; i<num; i++)
{
if (strcmp(name,tree[i].name) == 0)
{
return i;
}
}
return -1;
}
int findRoot(int t)
{
if (tree[t].father == -1)
{
return t;
}
int tmp = findRoot(tree[t].father);
tree[t].father = tmp;
return tmp;
}
int main()
{
int n, k, i;
while (cin >> n)
{
num = 0;//并查集清空
cin >> k;
for (i=0; i<n; i++)
{
char name1[NL];
char name2[NL];
int time;
cin >> name1>> name2>> time;
//读入people信息
int n1 = findPeople(name1);
int n2 = findPeople(name2);
if (n1 == -1)
{
tree[num].init(name1, time, -1);
n1 = num;//重新定位people在tree中位置
num++;
}
else
{
tree[n1].tTime += time;
}
if (n2 == -1)
{
tree[num].init(name2, time, -1);
n2 = num;
num++;
}
else
{
tree[n2].tTime += time;
}
//合并集合
n1 = findRoot(n1);
n2 = findRoot(n2);
if (n1 != n2)
{
tree[n1].father = n2;
}
}//end of for
for (i=0; i<num; i++)
{
g[i].clear();
}
for (i=0; i<num; i++)
{
int root = findRoot(i);
//第一个结点
if (g[root].head == -1)
{
g[root].head = i;
}
//最长时间为head
if (tree[g[root].head].tTime < tree[i].tTime)
{
g[root].head = i;
}
g[root].time += tree[i].tTime;
g[root].num++;
}
vector<Gang> v;
for (i=0; i<num; i++)
{
if (tree[i].father == -1
&& g[i].num > 2
&& g[i].time > 2*k)
{
v.push_back(g[i]);
}
}
if (!v.empty())
{
sort(v.begin(), v.end());
cout <<v.size() << endl;
for (i=0; i<v.size(); i++)
{
cout << tree[v[i].head].name << " " << v[i].num<< endl;
}
}
else
{
cout << 0 << endl;
}
}//end of while
return 0;
}
此题代码引用:http://blog.csdn.net/j597960549/article/details/21025425
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.
输入:
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.
输出:
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.
样例输入:
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 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
样例输出:
2 AAA 3 GGG 3 0
来源:
2012年浙江大学计算机及软件工程研究生机试真题
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 2003
#define NL 6
class People
{
public:
char name[NL];
int tTime;//总通话时间
int father;//用于并查集
void init(char n[NL], int time, int f)
{
for (int i=0; i<NL; i++)
{
name[i] = n[i];
}
tTime = time;
father = f;
}
};
People tree[MAX];//并查集
class Gang
{
public:
int head;//head在tree中的位置
int num;//人数
int time;
Gang()
{
head = -1;
num = 0;
time = 0;
}
void clear()
{
head = -1;
num = 0;
time = 0;
}
bool operator < (Gang g) const
{
if (head == -1)
{
return false;
}
return strcmp(tree[head].name, tree[g.head].name) < 0;
}
};
Gang g[MAX];//团伙
int num;//当前并查集中元素个数
int findPeople(char name[NL])//在并查集中找到name对应people的位置
{
for (int i=0; i<num; i++)
{
if (strcmp(name,tree[i].name) == 0)
{
return i;
}
}
return -1;
}
int findRoot(int t)
{
if (tree[t].father == -1)
{
return t;
}
int tmp = findRoot(tree[t].father);
tree[t].father = tmp;
return tmp;
}
int main()
{
int n, k, i;
while (cin >> n)
{
num = 0;//并查集清空
cin >> k;
for (i=0; i<n; i++)
{
char name1[NL];
char name2[NL];
int time;
cin >> name1>> name2>> time;
//读入people信息
int n1 = findPeople(name1);
int n2 = findPeople(name2);
if (n1 == -1)
{
tree[num].init(name1, time, -1);
n1 = num;//重新定位people在tree中位置
num++;
}
else
{
tree[n1].tTime += time;
}
if (n2 == -1)
{
tree[num].init(name2, time, -1);
n2 = num;
num++;
}
else
{
tree[n2].tTime += time;
}
//合并集合
n1 = findRoot(n1);
n2 = findRoot(n2);
if (n1 != n2)
{
tree[n1].father = n2;
}
}//end of for
for (i=0; i<num; i++)
{
g[i].clear();
}
for (i=0; i<num; i++)
{
int root = findRoot(i);
//第一个结点
if (g[root].head == -1)
{
g[root].head = i;
}
//最长时间为head
if (tree[g[root].head].tTime < tree[i].tTime)
{
g[root].head = i;
}
g[root].time += tree[i].tTime;
g[root].num++;
}
vector<Gang> v;
for (i=0; i<num; i++)
{
if (tree[i].father == -1
&& g[i].num > 2
&& g[i].time > 2*k)
{
v.push_back(g[i]);
}
}
if (!v.empty())
{
sort(v.begin(), v.end());
cout <<v.size() << endl;
for (i=0; i<v.size(); i++)
{
cout << tree[v[i].head].name << " " << v[i].num<< endl;
}
}
else
{
cout << 0 << endl;
}
}//end of while
return 0;
}
此题代码引用:http://blog.csdn.net/j597960549/article/details/21025425
相关文章推荐
- ULN2003的简单使用
- Android Studio安装与gradle的关系
- 开放寻址法
- Java学习笔记(2015.7.27~7.31)
- uva 12300
- [深入理解Android卷一全文-第五章]深入理解常见类
- POJ 1862 Stripies(优先队列)
- Android UI 关于Android计量单位dip dp sp px 你所需要知道的一切
- Git
- [python]解析python打印出来的数组
- iOS基础-UIKit框架-手势处理
- hdu5341 Gcd and Lcm
- hdu 5323
- man文档阅读技巧
- iOS学习笔记4-GCDAsyncUdpSocket的使用(实现异步发送接收数据)
- [深入理解Android卷一全文-第五章]深入理解常见类
- 设计模式六大原则
- Outputting Strings in the Console
- 102.Binary Tree Level Order Traversal
- 浅谈android入门