简单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. qt sql多重条件查询简便方法

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情 ...

  2. 洛谷 P2604 [ZJOI2010]网络扩容

    题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的最小扩容费用. ...

  3. 3.12 在运算和比较时使用NULL值

    问题:NULL值永远不会等于或不等于任何值,也包括NULL值自己,但是需要像计算真实值一样计算可为空列的返回值.例如,需要在表emp中查出所有比“WARD”提成(COMM)低的员工,提成为NULL(空 ...

  4. swift Equatable 的缺省实现

    Starting from Swift 4.1, all you have to is to conform to the Equatable protocol without the need of ...

  5. [转] 学习,思维三部曲:WHAT、HOW、WHY(通过现象看本质)

    https://www.douban.com/note/284947308/?type=like 学习技术的三部曲:WHAT HOW WHY 我把学习归类为三个步骤:What.How.Why.经过我对 ...

  6. Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)

    转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...

  7. DROP TYPE - 删除一个用户定义数据类型

    SYNOPSIS DROP TYPE name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP TYPE 将从系统表里删除用户定义的类型. 只有类 ...

  8. EBS ORACLE使用API批量取消销售订单

    需要切换组织,还有用户的id.下面红色字体代表要修改的地方. /*BEGIN MO_GLOBAL.INIT('M'); MO_GLOBAL.set_policy_context ('S',); FND ...

  9. 169. Majority Element@python

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. luogu 5月月赛 #A

    T29693 取石子 题目描述 Alice 和 Bob 在玩游戏 他们有 n 堆石子,第 i 堆石子有ai​ 个,保证初始时 ai​≤ai+1​(1≤i<n) . 现在他们轮流对这些石子进行操作 ...