简单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. SQL 导出csv

    https://jingyan.baidu.com/album/4b07be3c466b5d48b280f37f.html?picindex=9

  2. HDU 5380 Travel with candy (贪心,单调队列)

    题意: 有n+1个城市按顺序分布在同一直线上,现在需从0号城市按顺序走到n号城市(保证可达),从0号城市到i号城市需要消耗ai个糖果,每个城市都可以通过买/卖糖果来赚取更多的钱,价格分别是buyi和s ...

  3. SEO 第三章

    SEO第三章 本次课目标: 1.  掌握关键词的选取方法 2.  掌握关键词的竞争强度分析 3.  掌握关键词的拓展方法 一.关键词的选取 选择关键词的时候可以根据公司网站的定位,围绕公司的主营产品或 ...

  4. EXECUTE - 执行一个准备好的查询

    SYNOPSIS EXECUTE plan_name [ (parameter [, ...] ) ] DESCRIPTION 描述 EXECUTE 用于执行一个前面准备好的语句. 因为一个准备好的查 ...

  5. uva820 Internet Bandwidth

    就是模板... #include<cstdio> #include<cstring> #include<vector> #include<queue> ...

  6. 倍增实现LCA

    Today,we will talk about how to find The Least Common Ancestor. Now ,let us get into the business(正题 ...

  7. [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays

    https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...

  8. mysql 数据库 show命令

    MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 1. show tables或show tables fr ...

  9. ubuntu中执行docker info出现警告信息WARNING: No memory limit support 或 WARNING: No swap limit support

    docker info 指令报若下错误:WARNING: No memory limit support 或WARNING: No swap limit support 解决方法: 1.打开/etc/ ...

  10. 队列的JS实现及广度优先搜索(BFS)的实现

    队列是先进先出(FIFO)的数据结构,插入操作叫做入队,只能添加在队列的末尾:删除操作叫做出队,只能移除第一个元素.在JS中,用数组可以很简单的实现队列.JavaScript实现排序算法 functi ...