简单DFS。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn=+;
map<string,int>m1;
map<int,string>m2;
int n,k,tot; struct Edge
{
int u,v,val;
}e[maxn]; struct Ans
{
string name;
int cnt;
}ans[maxn];
int num; vector<int>g[maxn];
int flag[maxn];
int Block;
int cnt[maxn]; void dfs(int x)
{
num++; flag[x]=Block;
for(int i=;i<g[x].size();i++)
{
if(flag[g[x][i]]!=) continue;
dfs(g[x][i]);
}
} bool cmp(const Ans&a,const Ans&b){return a.name<b.name;} int main()
{
scanf("%d%d",&n,&k); tot=;
memset(flag,,sizeof flag);
memset(cnt,,sizeof cnt);
for(int i=;i<=n;i++)
{
string a,b; cin>>a>>b>>e[i].val;
if(m1[a]==)
{
m1[a]=++tot;
m2[tot]=a;
}
if(m1[b]==)
{
m1[b]=++tot;
m2[tot]=b;
}
e[i].u=m1[a];
e[i].v=m1[b];
cnt[e[i].v]+=e[i].val;
cnt[e[i].u]+=e[i].val;
g[e[i].u].push_back(e[i].v);
g[e[i].v].push_back(e[i].u);
} num=;
int t=;
for(int i=;i<=tot;i++)
{
if(flag[i]!=) continue;
Block++; num=; dfs(i);
if(num<=) continue;
int sum=;
for(int j=;j<=n;j++)
if(flag[e[j].u]==Block&&flag[e[j].v]==Block)
sum=sum+e[j].val;
if(sum<=k) continue; int id,Max=-;
for(int j=;j<=tot;j++)
if(flag[j]==Block&&cnt[j]>Max)
Max=cnt[j],id=j; ans[t].name=m2[id];
ans[t].cnt=num;
t++;
} sort(ans,ans+t,cmp); printf("%d\n",t);
for(int i=;i<t;i++)
cout<<ans[i].name<<" "<<ans[i].cnt<<endl; return ;
}

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

  1. PAT (Advanced Level) Practise - 1095. Cars on Campus (30)

    http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...

  2. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

  3. PAT (Advanced Level) 1053. Path of Equal Weight (30)

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

  4. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  5. PAT (Advanced Level) 1038. Recover the Smallest Number (30)

    注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...

  6. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

  7. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  8. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  9. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

随机推荐

  1. NIO入门之轻松读取大文件

    NIO入门之轻松读取大文件 今天同事碰到了一个问题,从游戏服务器下载下来的输出log有一个多G大.用记事本打不开,EditPlus也打不开,都提示文件太大.用word也打不开,提示文件大于512M.打 ...

  2. Charles Petzold 编程界大师,世界顶级技术作家 《CODE》值得阅读

    <CODE>The Hidden Language of Computer Hardware and Software 从书内容的第一页开始就惊叹于作者的耐心和责任心 整本书以两个隔街对窗 ...

  3. 测试当前C环境的栈帧增长方向以及传递参数时的压栈顺序

    前文链接:上次由于一个很常见的printf-bug(下文有提及)引发了我对栈的思考,并写下了一点总结.这次就尝试对不同的C环境进行实践,检测其传递参数的一些性质. 这是今天写的检查C环境的一段程序.能 ...

  4. Java8函数式编程和lambda表达式

    文章目录函数式编程JDK8接口新特性函数接口方法引用函数式编程函数式编程更多时候是一种编程的思维方式,是一种方法论.函数式与命令式编程区别主要在于:函数式编程是告诉代码你要做什么,而命令式编程则是告诉 ...

  5. windows常用bat脚本

    windows常用bat脚本 https://blog.csdn.net/longyan_csc/article/details/78737722 Windows_批处理+任务计划实现文件夹定时备份 ...

  6. 事件冒泡 & 阻止事件冒泡

    事件冒泡 : 当一个元素接收到事件的时候,会把他接收到的所有传播给他的父级,一直到顶层window.事件冒泡机制 阻止冒泡 : 当前要阻止冒泡的事件函数中调用 event.cancelBubble = ...

  7. 初识WEBGL

    WEBGL (全写Web Graphics Library)是一种3D绘图协议,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的一 ...

  8. 判断请求是否为ajax

    判断请求是否为ajax 转载:http://www.cnblogs.com/tony-jingzhou/archive/2012/07/30/2615612.html x-requested-with ...

  9. vs2010的资源视图中,对话框显示数字的解决方法之一

    以上是不正常显示. 我这次遇到该问题的原因是资源名IDD_DLG_INTENSITY重复定义导致的, 所以在resource.h文件中去除重复定义就好了. 正常应该显示DD_XXX,如下图所示

  10. hibernate5.x版本org.hibernate.MappingException: Unknown entity问题

    /* * //创建hibernate配置对象 Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xm ...