简单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. Redis学习笔记(二)字符串进阶

    1.增减操作 字符串可以储存字节串.整数.浮点数三种类型的值,如果值无法被解释为十进制的整数或浮点数,那么对此值进行增减操作会返回错误,如果对一个不存在的或者保存了空串的键进行增减操作,Redis将当 ...

  2. PHP实时输出内容到浏览器

    buffer buffer是一个内存地址空间,Linux系统默认大小一般为4096(4kb),即一个内存页.主要用于存储速度不同步的设备或者优先级不同的设备之间传办理数据的区域.通过buffer,可以 ...

  3. Matlab 图像转极坐标系

    实心圆环 imgVP1=flip(imgVP1,1);  % 水平翻转 polarVP1=polarVolinPlot(imgVP1); % 调用函数空心圆环 [m,n,~]=size(imgVP2) ...

  4. 解决selenium.common.exception.WebDriverException:Message:'chromedriver' executable needs to be in Path

    'chromedriver' executable needs to be in Path 声明:本人萌新,刚学python不久记录一下自己的坑,发出来若能帮助到一些人尽早解决问题那便是极好的,( ̄▽ ...

  5. PHP 中空字符串介绍0、null、empty和false之间的关系

    0是数字,是empty,是false,不是null,值相当于空字符串,但类型不是字符串,去空格或强制转换为字符串型时不等于空字符串 ""的值相当于0,是empty,是空字符串,是f ...

  6. CAD交互绘制带周长面积的矩形框(网页版)

    主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY1 直线的开始点y坐标 DOUBLE ...

  7. AndroidStudio连不上天天模拟器

    问题:天天模拟器经常无法被Android Studio读取出来: 解决方法:手动连接它的端口: 方法一:找到Android\SDK\platform-tools目录,在当前目录下打开命令行窗口(shi ...

  8. 2018美赛准备之路——Matlab基础——命令行功能函数

    clc 清屏(只清除显示内容) clear  清除所有变量(运算结果) who  显示workspace的所有变量 whos  详细显示workspace的所有变量  help sin 显示sin函数 ...

  9. TP框架中同时使用“or”和“and”

    今天在tp中遇到一个问题,可能这并不算难的问题,但是我还是分享一下 以下是tp手册里面查询or的方式 $User = M("User"); // 实例化User对象 $where[ ...

  10. virsh 命令

    virsh是用与管理虚拟化环境中的客户机和Hypervisor的命令行工具,与virt-manager等工具类似,也是调用libvirt API来实现虚拟化的管理. 在使用virsh命令行进行虚拟化管 ...