PAT (Advanced Level) 1034. Head of a Gang (30)
简单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)的更多相关文章
- 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 ...
- PAT (Advanced Level) 1064. Complete Binary Search Tree (30)
因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- 【PAT Advanced Level】1014. Waiting in Line (30)
简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...
- PAT (Advanced Level) 1038. Recover the Smallest Number (30)
注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
随机推荐
- 十个 JDBC 的最佳实践
JDBC是Java为多种关系型数据库提供的统一的访问接口,以下是我长期使用JDBC总结的十个最佳实践. 1. 使用PrearedStatement 任何一个使用过JDBC的Java程序员几乎都知道这个 ...
- Android掌中游斗地主游戏源码完整版
源码大放送-掌中游斗地主(完整版),集合了单机斗地主.网络斗地主.癞子斗地主等,有史以来最有参考价值的源码,虽然运行慢了一点但是功能正常,用的是纯java写的. 项目详细说明:http://andro ...
- 保密安全风险自评估单机版检查工具V1.0
下载链接:http://download.csdn.net/download/moremoretea1983/10273128
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- js实现ctrl+v粘贴并上传图片
前端页面: <textarea class="scroll" id="text" placeholder="在此输入...">& ...
- C++:new的使用
这里先开个头,以后做详细补充个: new 分配内存失败后会返回空指针:
- nginx 集群
Nginx是什么? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.一直纳闷这个X是怎么来 ...
- 项目中遇到的超卖问题及解决办法(使用go做测试工具)
超卖问题:在一个很短的时间内,Mysql的数据状态在 取出,比较,提交,或修改中,另外一个进程访问数据导致的超卖问题. 案例: 1.前端没有做限制,如果用户连续点击签到,那么会有多条数据发送到后端,如 ...
- 基于flask的网页聊天室(四)
基于flask的网页聊天室(四) 前言 接前天的内容,今天完成了消息的处理 具体内容 上次使用了flask_login做用户登录,但是直接访问login_requare装饰的函数会报401错误,这里可 ...
- python-----定制群发微信消息
如何使用表格中的信息群发微信消息? 如何读取csv? → 使用内置模块csv 如何按对应信息发送到微信?→ 使用第三方库wxpy 以下代码素材自取:链接:https://pan.baidu.co ...