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个人的 ...
随机推荐
- ibatis遍历数组:ParameterObject or property was not a Collection, Array or Iterator.
这个问题在使用ibatis的<iterate></iterate>时出现的,很简单,但是蛋疼了很久,记下来 首先从错误提示看,明显意思是你给出ibatis的参数不对路,人家不认 ...
- ARC初步介绍
[转载自 http://onevcat.com/2012/06/arc-hand-by-hand/] 手把手教你ARC——iOS/Mac开发ARC入门和使用 Revolution of Objecti ...
- P4365 [九省联考2018]秘密袭击coat
$ \color{#0066ff}{ 题目描述 }$ Access Globe 最近正在玩一款战略游戏.在游戏中,他操控的角色是一名C 国士 兵.他的任务就是服从指挥官的指令参加战斗,并在战斗中取胜. ...
- 2016级算法第三次上机-B.Bamboo和巧克力工厂
B Bamboo和巧克力工厂 分析 三条流水线的问题,依然是动态规划,但是涉及的切换种类比较多.比较易于拓展到n条流水线的方式是三层循环,外层是第k个机器手,里面两层代表可切换的流水线 核心dp语句: ...
- 主流服务器虚拟化技术简单使用——Xen(一)
Tips:因为博客园网页布局的原因,部分图片显示不清晰,可以放大网页查看清晰图片. 如果系统使用物理机,需要在BIOS里面开启Intel VT-x(或AMD-V),如果是VMware workstat ...
- JS之判断json对象中是否含有某个key值
var json = {"key1":"val1","key2":"val2","key3":&qu ...
- [转] crontab命令
[From] http://man.linuxde.net/crontab 当前位置:首页 » 系统管理 » crontab crontab命令 crontab命令被用来提交和管理用户的需要周期性 ...
- (转)Go语言核心36讲之Go语言入门基础知识
- 分分钟钟学会Python -基础&运算符
day002 基础&运算符 1.循环语句 ### 1.循环格式 while 条件: print('') ''' while True: print('人生苦短,我用Python.') ''' ...
- Java_无参数无返回类型方法及练习
无参数无返回类型方法语法格式: public static void 方法名称(){ 方法体; } class Method03{ /*练习3:输出1-100中的每个数,要求使用无参无返回类型的方法完 ...