有一个两分的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]的更多相关文章

  1. 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 ...

  2. 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 ...

  3. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  4. PAT甲级1034. Head of a Gang

    PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...

  5. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. PAT甲级1034 Head of a Gang【bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...

随机推荐

  1. 常用博客 API地址

    新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/ap ...

  2. Ionic2文档整理

    来自:Rainey's Blog 原文地址:http://rainey.space/2016/04/06/Ionic2_Chinese_Document/ Github:https://github. ...

  3. python requests 包 使用

    1: 发送带 cookie 的 请求 resp = requests.get(self.url_item_list_first_page, cookies=self.cookies) >> ...

  4. P5108 仰望半月的夜空

    题目链接 题意分析 给你一个字符串 让你求\(1-n\)长度下的字符串的中字典序最小并且最靠左的字符串的开头位置 我们考虑先建出\(SA\) 然后考虑对于一个字符串后缀排序之后 baba 后缀排序之后 ...

  5. NavigationItem的Title不居中,BackButton修改它得title

    国外得一个链接: http://situee.blogspot.com/2014/10/ios-set-navigation-bar-back-button-title.html 最近迷上了简洁风,在 ...

  6. Boost内存池使用与测试

    目录 Boost内存池使用与测试 什么是内存池 内存池的应用场景 安装 内存池的特征 无内存泄露 申请的内存数组没有被填充 任何数组内存块的位置都和使用operator new[]分配的内存块位置一致 ...

  7. 进阶篇:3.9)3d打印件设计

    本章目的:了解3d打印,购买3d打印机. 1.3d打印基础知识: 现在主流的3d打印技术有4种:①FDM:②SLA:③SLS:④3DP.具体如下: ①熔融沉积造型(Fused deposition m ...

  8. zabbix4.2 安装

    官网https://www.zabbix.com/cn/download a. 安装 数据库rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86 ...

  9. node.js修改全局安装文件路径

    在进行 node.js 的开发过程中,我们需要下载大量的依赖模块,为了不让 c 盘的东西太过于散乱,可以通过修改node的配置参数,来修改node依赖的下载路径. 步骤: ①创建两个文件夹:node_ ...

  10. lucene原理

    lucene查找原理: https://yq.aliyun.com/articles/581877