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个人的 ...
随机推荐
- js判断浏览器类型以及语言
1.检查是否是移动端(Mobile).ipad.iphone.微信.QQ等 <script type="text/javascript"> //判断访问终端 var b ...
- 图论 竞赛图(tournament)学习笔记
竞赛图(tournament)学习笔记 现在只是知道几个简单的性质... 竞赛图也叫有向完全图. 其实就是无向完全图的边有了方向. 有一个很有趣的性质就是:一个tournament要么没有环,如果 ...
- JAVA 集合随笔
JAVA中提供了现成操纵数据的集合,使得我们在开发中基本不用自己动手实现复杂的数据结构,来保存和操纵数据. 所以当我们有了基本的数据结构知识,会合理利用JAVA提供的集合就好啦. JDK1.8;(仅列 ...
- Jenkins 发布平台 MSB4064: The "Retries" parameter is not supported & error MSB4063: The "Copy" task could not be initialized
____________________________________________________________________________________________________ ...
- Idea maven编译报错 javacTask: 源发行版 1.8 需要目标发行版 1.8
javacTask: 源发行版 1.8 需要目标发行版 1.8 [INFO] ------------------------------------------------------------- ...
- nginx应用
windows: 启动:start nginx 退出:nginx -s quit 检查配置是否正确:nginx -t -c ./conf/nginx.conf 查看是否在运行:tasklist /fi ...
- lua路径问题
方法1:lua进行require绝对路径时,会从package.path中进行遍历 print(package.path)会得到类似下面的结果: --> "lualibs/p4ulib ...
- Rational Rose2007下载和安装
网上关于Rational Rose2007安装包,网上找了一堆大多都是垃圾,最后找到一个可用的(带激活文件),保存在自己的网盘里,这里分享出来:https://pan.baidu.com/s/1bpb ...
- IO流(一)字节流
1:io流体系:对数据进行读写操作.所以IO不是读就是写咯. 2:io流的相关的类:java.io包. 有关IO的操作都会产生IOException异常 3:io:参照物是程序, i:input.进来 ...
- 【JS】逻辑运算符 非! 与&& 或||
JS中的逻辑运算符在处理布尔值的判断时,和其他语言没有什么不同,不过在处理对象时,就需要好好梳理记忆下了. 逻辑非(!) 如果一个操作数是一个对象,返回false; 如果一个操作数是一个空字符串,返回 ...