分析:

  考察并查集,注意中间合并时的时间的合并和人数的合并。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cctype>
#include <map> using namespace std; const int Max_Int = 0x7fffffff;
const int Max_required = ; struct Node //记录gang的信息
{
int total_person; //gang中总人数
int time_length; //gang中总时间
int tree; //并查集用处
}gang[]; struct Name
{
char ch[];
}; map<string, int> Map_Name2int; //名字转下标之用
map<int, string> Map_int2Name;
int _index; int perp[]; //记录每个人花的时间 void Init()
{
Map_Name2int.clear();
_index = ;
for (int i = ; i <= ; i++)
{
gang[i].total_person = ;
gang[i].tree = i;
gang[i].time_length = ;
perp[i] = ;
}
} int string2index(string name)
{
map<string, int>::iterator iter = Map_Name2int.find(name);
if (iter != Map_Name2int.end())
return iter->second;
Map_Name2int.insert(make_pair(name, _index));
Map_int2Name.insert(make_pair(_index, name));
_index++; return _index - ;
} int get_root(int r)
{
while (r != gang[r].tree)
r = gang[r].tree;
return r;
} //-------------------------------------------------
void merge(int fir, int sec, int time) //合并操作。
{
fir = get_root(fir);
sec = get_root(sec); if (fir != sec) //如果两个拥有不同的根
{
gang[sec].tree = fir;
gang[fir].time_length += time + gang[sec].time_length;
gang[fir].total_person += gang[sec].total_person;
}
else
gang[fir].time_length += time;
//cout << gang[fir].total_person << " " << gang[fir].time_length << endl;
} struct Gang //以root为首的群的head,存储答案
{
int head;
int root;
string name;
}gang_head[];
int total_gang; void find_head(int k)
{
total_gang = ;
for (int i = ; i < _index; i++)
{
int r = get_root(i); //cout << r << " " << Map_int_Name.find(i)->second << endl; if (gang[r].total_person > && gang[r].time_length > k)
{
bool flag = ;
for (int j = ; j < total_gang; j++)
{
if (gang_head[j].root == r)
{
flag = ;
if (perp[gang_head[j].head] < perp[i])
gang_head[j].head = i;
break;
}
} if (flag == )
{
gang_head[total_gang].head = i;
gang_head[total_gang].root = r;
total_gang++;
}
}
}
} int cmp(const Gang &a, const Gang &b)
{
return a.name < b.name;
} void print_info()
{
for (int i = ; i < total_gang; i++)
gang_head[i].name = Map_int2Name.find(gang_head[i].head)->second; sort(gang_head, gang_head + total_gang, cmp); printf("%d\n", total_gang);
for (int i = ; i < total_gang; i++)
{
cout << gang_head[i].name << " " << gang[gang_head[i].root].total_person << endl;
} } int main()
{
int n, k; while (scanf("%d%d", &n, &k) != EOF)
{ Init(); //初始化 string name1, name2;
int time;
while (n--)
{
cin >> name1 >> name2 >> time;
int fir = string2index(name1);
int sec = string2index(name2); perp[fir] += time;
perp[sec] += time;
merge(fir, sec, time);
} find_head(k); print_info();
}
return ;
}

1034. Head of a Gang (30)的更多相关文章

  1. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  2. PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

    1034 Head of a Gang (30 分)   One way that the police finds the head of a gang is to check people's p ...

  3. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  4. 1034. Head of a Gang (30) -string离散化 -map应用 -并查集

    题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

  5. 1034 Head of a Gang (30)(30 分)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  6. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  7. 1034 Head of a Gang (30分)(dfs 利用map)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  8. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

  9. PAT (Advanced Level) 1034. Head of a Gang (30)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

随机推荐

  1. @Override报错

    仔细看了下项目 , 是因为有人把project的信息传上来了 , 使用编译的JDK变成了1.5(难道是因为他的1.8的版本我没有 ?) , 右键项目, 选property , 把compiler变成1 ...

  2. c#深拷贝

    /// <summary> /// 对象拷贝 /// </summary> /// <param name="obj">被复制对象</pa ...

  3. java maven诡异的错误no class found

    从服务器下载一个java web项目,启动老提示no class found,查看maven依赖库,相关的jar包都已经引入.同样一个项目,在别的机器都可以运行,唯独在我本机运行出错. 为了排错,将其 ...

  4. 如何修改SharePoint列表条数等阈值

    若要修改SharePoint中对列表最大条数等设定的阈值,可按如下步骤操作: 1. 打开页面:管理中心 > 应用程序管理 > 管理Web应用程序.2. 选择要修改阈值的Web应用程序,并在 ...

  5. AX 2012 SSRS print setting-报表打印输出设置

    static void callerreport_printsetting(Args _args) { LedgerJournalController controller = new LedgerJ ...

  6. [VB.NET]取消按钮按下的默认事件响应

    大家应该有过这样的经历:有两个文本框a,b.通过编程,当我们在A中回车后,光标会移动到B文本框. 但是,不可避免的会听到一声“铛”的声音. 解决办法: 在Keydown或者KeyUp事件中,设置e.S ...

  7. Metasploit更新

    Metasploit更新,官方放弃SVN,开始使用GIT 有一段时间没有写文章了,今天翻译一篇文章吧.以后Metasploit更新,请不要再使用下面的方法了 1 svn co https://www. ...

  8. 关于UIView的autoresizingMask属性的研究【转】

    在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum  ...

  9. Testin云测试平台初体验

    这几天偶然接触到了一个叫做Testin的云测试平台,经过一番体验,感觉还是不错的,因为里面提供了大量的测试机型,可以针对Android手机的严重碎片化现象做出比较全面的测试,同时Testin的测试内容 ...

  10. 纸上谈兵:栈(stack)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 栈(stack)是简单的数据结构,但在计算机中使用广泛.它是有序的元素集合.栈最显 ...