有一个两分的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. 正则表达式,sed简单用法

      一. 正则表达式 1. 常见的正则表达式字符 [] 匹配字符集 grep "bl[lo]g" oldboy.txt 表示字符‘l’或者‘o’都可匹配 * 重复前面字符任意次 g ...

  2. The server of Nginx(二)——Nginx基本功能配置

    一.Nginx访问控制 (1)基于授权的访问控制 Nginx于Apache一样,可以实现基于用户授权的访问控制,当客户端要访问相应网站或者目录时要求输入用户名密码才能正常访问,配置步骤与Apache基 ...

  3. virsh 连接虚拟机 (vnc 或 控制台)

    第一种方式 1.如果虚拟机登录方式为VNC,在ubuntu机器上安装vncviewer 在虚拟机的配置xml中 <graphics type="vnc" autoport=& ...

  4. 进阶篇:2.1)DFMA实施障碍和关键

    本章目的:了解DFMA实施障碍与关键. 1.实施的障碍 面向制造和装配的产品开发能够降低产品成本.提高产品质量.缩短产品开发周期,但是,由于传统产品开发思想和各种条件的限制,实施面向制造和装配的产品开 ...

  5. Sklearn,TensorFlow,keras模型保存与读取

    一.sklearn模型保存与读取 1.保存 from sklearn.externals import joblib from sklearn import svm X = [[0, 0], [1, ...

  6. web服务的简单介绍及apache服务的安装

    一,web服务的作用:  是指驻留于因特网上某种类型计算机的程序,可以向浏览器等Web客户端提供文档.可以放置网站文件,让全世界浏览:   可以放置数据让全世界下载.目前最主流的三个Web服务器是Ap ...

  7. 3-----Docker实例-安装MySQL

    Docker 安装 MySQL 方法一.docker pull mysql 查找Docker Hub上的mysql镜像 runoob@runoob:/mysql$ docker search mysq ...

  8. CDH集群安装配置(三)- 集群时间同步(主节点)和 免密码登录

    集群时间同步(主节点) 1. 查看是否安装ntp服务,如果没有安装 rpm -qa |grep ntpd查看命令 yum install ntp安装命令 2. 修改配置 vi /etc/ntp.con ...

  9. 码云+jenkins+自动回复邮件

    码云和jenkins的配置流程在Gitee Jenkins Plugin上介绍的很仔细. 附上码云官方Gitee Jenkins Plugin的介绍: https://gitee.com/help/a ...

  10. Java返回泛型的方法-实例

    package com.frank.app; import java.io.UnsupportedEncodingException;import java.lang.reflect.Type;imp ...