PAT 1034. Head of a Gang[bug]
有一个两分的case出现段错误,真是没救了,估计是要写bfs的形式,可能栈溢出了
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm> using namespace std; int G[][] = {}; class Man {
public:
int id;
string name;
vector<int> adj;
bool visited;
Man(string &n): name(n), visited(false){}
}; int get_gid(string &name, unordered_map<string, int>& n2i, int &gid, vector<Man>& mans) {
int id = gid;
auto iter = n2i.find(name);
if (iter == n2i.end()) {
n2i.insert(make_pair(name, gid++));
mans.push_back(Man(name));
} else {
id = iter->second;
}
return id;
} void dfs(vector<Man>& mans, int curi, int K, int &head, int &max_weight, int &count, int &rtotal) {
if (mans[curi].visited) {
return;
}
count++;
mans[curi].visited = true; int weight = ;
int len = mans[curi].adj.size();
for (int i=; i<len; i++) {
int r = G[curi][mans[curi].adj[i]];
weight += r;
if (mans[mans[curi].adj[i]].visited) {
continue;
}
rtotal += r;
} if (weight > max_weight) {
head = curi;
max_weight = weight;
}
for (int i=; i<len; i++) {
int r = G[curi][mans[curi].adj[i]];
dfs(mans, mans[curi].adj[i], K, head, max_weight, count, rtotal);
}
} class MyCmp{
private:
vector<Man>* mans;
public:
bool operator()(const pair<int, int>& a, const pair<int, int> &b) {
return (*mans)[a.first].name < (*mans)[b.first].name;
}
MyCmp(vector<Man>* ms) {mans = ms;}
}; int main() {
int N, K;
scanf("%d%d", &N, &K); unordered_map<string, int> name2id;
vector<Man> mans; int gid = ; char name1[];
char name2[];
int time; int ida, idb; for (int i=; i<N; i++) {
scanf("%s%s%d", name1, name2, &time);
string s1(name1);
string s2(name2);
ida = get_gid(s1, name2id, gid, mans);
idb = get_gid(s2, name2id, gid, mans);
if (!G[ida][idb]) {
mans[ida].adj.push_back(idb);
}
if (!G[idb][ida]) {
mans[idb].adj.push_back(ida);
}
G[ida][idb] += time;
G[idb][ida] += time;
} vector<pair<int, int> > heads;
int count, head, max_weight, rtotal;
for (int i=; i<gid; i++) {
if (mans[i].visited) {
continue;
}
count = ;
max_weight = ;
rtotal = ;
dfs(mans, i, K, head, max_weight, count, rtotal);
if (count > && rtotal > K) {
heads.push_back(make_pair(head, count));
}
} sort(heads.begin(), heads.end(), MyCmp(&mans)); int len = heads.size(); printf("%d\n", len);
for (int i=; i<len; i++) {
printf("%s %d\n", mans[heads[i].first].name.c_str(), heads[i].second);
} return ;
}
PAT 1034. Head of a Gang[bug]的更多相关文章
- PAT 1034 Head of a Gang[难][dfs]
1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...
- PAT 1034. Head of a Gang
1034. Head of a Gang (30) One way that the police finds the head of a gang is to check people's phon ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- PAT甲级1034. Head of a Gang
PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...
- pat 甲级 1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's p ...
- pat 甲级 1034 ( Head of a Gang )
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's pho ...
- 1034 Head of a Gang (30 分)
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's pho ...
- PAT甲级1034 Head of a Gang【bfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...
随机推荐
- ubuntu和centos 编译安装nginx及常用命令
转自http://www.cnblogs.com/piscesLoveCc/p/5794926.html 一. ubuntu安装 1. 安装依赖库 安装gcc g++的依赖库 ubuntu平台可以使用 ...
- 极光开发者沙龙 JIGUANG MEETUP —— 移动应用性能优化实践
活动背景 移动互联网时代,各大互联网公司都已将自己的产品和服务全面移动化,各类新产品功能都会优先在移动 App 上尝试. 应用性能作为影响用户体验最重要的因素,在开发过程中显得尤为重要. 用户网络环境 ...
- Django中的CSRF(跨站请求伪造)
Django中的CSRF(跨站请求伪造) Django CSRF 什么是CSFR 即跨站请求伪装,就是通常所说的钓鱼网站. 钓鱼网站的页面和正经网站的页面对浏览器来说有什么区别? (页面是怎么来的? ...
- [BZOJ 4857][Jsoi2016]反质数序列
传送门 $ \color{green} {solution : } $ 因为 $ 1 $ 的个数我们最多只能选一个,所以剩下的数如果组成素数那么只有一奇一偶,显然是个二分图模型 #include &l ...
- Pycharm+QTDesigner+PyQt5环境配置
python+PyQt5写界面很方便,记录下个人配置环境过程.... 安装软件: pycharm2017 Qt5.9.6 python3.6.6/python2.7.15 配置PyQt5: pytho ...
- 最近总想着写一个模拟alert和confirm插件,代替原生的
msgbox-confirm github: https://github.com/tong-mikasa/msgbox-confirm 主要js代码,scss代码 (function($) { $ ...
- 小米MIX 3获得ROOT权限的步骤
小米MIX 3有没有办法开启Root超级权限?大伙都知道,Android设备有Root超级权限,一旦手机开启root相关权限,可以实现更强大的功能,比如大伙部门的营销部门的妹纸,使用一些营销工具都需要 ...
- saltstack源码详解一
目录 初识源码流程 入口 1.grains.items 2.pillar.items 2/3: 是否可以用python脚本实现 总结pillar源码分析: @(python之路)[saltstack源 ...
- display和overflow
一.display <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 安装Cloudera Manager集群时首次运行命令部署客户端设置失败的解决办法(图文详解)
不多说,直接上干货! 问题详情 解决办法 (1) 时间同步检查下(尤其是这个) (2) 防火墙是否关闭 (3) cloudera-scm-server 和 cloudera-scm-agent 是否启 ...