1034 Head of a Gang (30 分)(图的遍历or并查集)

dfs
#include<bits/stdc++.h> using namespace std;
const int N=;
int mp[N][N];
int weight[N];
int vis[N];
map<string,int>si;
map<int,string>is;
map<string,int>gang;
int cnt;
//进行转换
int solve(string x)
{
if(si.find(x)!=si.end()){
return si[x];
}
else{
si[x]=cnt;
is[cnt]=x;
return cnt++;
}
}
void dfs(int now,int &head,int &number,int &num)
{
if(weight[now]>weight[head]){
head=now;//判断头目
}
number++;//增加数量
vis[now]=true;
for(int i=;i<cnt;i++){
if(mp[now][i]>){
num+=mp[now][i];//总和
mp[now][i]=mp[i][now]=;
if(!vis[i]){
vis[i]=true;
dfs(i,head,number,num);
}
}
}
}
int main()
{
fill(weight,weight+N,);
fill(mp[],mp[]+N*N,);
fill(vis,vis+N,false);
int n,k;
cin>>n>>k;
for(int i=;i<n;i++){
string a,b;
int Num;
cin>>a>>b>>Num;
int x=solve(a);
int y=solve(b);
weight[x]+=Num;//每个点都需要
weight[y]+=Num;
mp[x][y]+=Num;
mp[y][x]+=Num;
}
for(int i=;i<cnt;i++){
if(!vis[i]){
int head=i;
int number=;
int num=;
vis[i]=true;
dfs(i,head,number,num);
if(number>&&num>k){
gang[is[head]]=number;
}
}
}
cout<<gang.size()<<endl;
for(auto &it:gang){
cout<<it.first<<" "<<it.second<<endl;
}
return ;
}
并查集
自己做的时候用的是并查集 AC了 就是暴力找 后面的代码自己不仔细看也不知道都啥意思 反正很乱
#include<bits/stdc++.h> using namespace std;
const int N=;
int weight[N];
int p[N];
int sum[N];
int mp[N][N];
int findth(int x)
{
if(x==p[x]) return x;
return p[x]=findth(p[x]);
}
void unionn(int x,int y)
{
int xx=findth(x);
int yy=findth(y);
if(xx!=yy){
p[yy]=xx;
sum[xx]+=sum[yy];
}
} map<string,int>si;
map<int,string>is;
map<string,int>gang;
int cnt;
int solve(string a)
{
if(si.find(a)!=si.end()) return si[a];
else{
si[a]=cnt;
is[cnt]=a;
return cnt++;
}
}
struct node
{
int id;
int sum1;
node(int _id,int _sum):id(_id),sum1(_sum){}
}; int main()
{
int n,k;
cin>>n>>k;
for(int i=;i<n;i++){
p[i]=i;
sum[i]=;
}
cnt=;
memset(mp,,sizeof(mp));
for(int i=;i<n;i++){
string a,b;
cin>>a>>b;
int num;
cin>>num;
int x=solve(a);
int y=solve(b);
weight[x]+=num;
weight[y]+=num;
mp[x][y]+=num;
unionn(x,y);
}
set<int>st;
for(int i=;i<cnt;i++){
int x=findth(i);
st.insert(x);
}
vector<int>vec;
for(auto it:st){
if(sum[it]>){
vec.push_back(it);
}
}
if(vec.size()==){
cout<<""<<endl;
return ;
}
vector<int>ve[N];
for(int i=;i<vec.size();i++){
for(int j=;j<cnt;j++){
if(p[j]==vec[i]){
ve[i].push_back(j);
}
}
}
int su=; vector<int>pp;
map<int,int>mm;
vector<node>no;
for(int i=;i<vec.size();i++){
vector<int>tt;
for(int j=;j<ve[i].size();j++){
tt.push_back(ve[i][j]);
}
for(int z=;z<tt.size();z++){
for(int w=;w<tt.size();w++){
su+=mp[tt[z]][tt[w]];
}
}
if(su>k){
no.push_back(node(i,su));
}
su=;
}
if(no.size()==){
cout<<""<<endl;
return ;
}
cout<<no.size()<<endl;
for(int i=;i<no.size();i++){
int maxn=-;
int idd=;
for(int j=;j<ve[no[i].id].size();j++){
if(weight[ve[no[i].id][j]]>maxn){
maxn=weight[ve[no[i].id][j]];
idd=ve[no[i].id][j];
}
}
gang[is[idd]]=sum[findth(idd)];
}
for(auto it:gang){
cout<<it.first<<" "<<it.second<<endl;
}
return ;
}
1034 Head of a Gang (30 分)(图的遍历or并查集)的更多相关文章
- 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 ...
- 1034 Head of a Gang (30分)(dfs 利用map)
One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- 1013 Battle Over Cities (25 分)(图的遍历or并查集)
这题用并查集或者dfs都可以做 dfs #include<bits/stdc++.h> using namespace std; ; bool mp[N][N]; int n,m,k; 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 Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...
- 1034. Head of a Gang (30) -string离散化 -map应用 -并查集
题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
随机推荐
- FreeImage.lib库的配置和简单使用 转
转载地址 http://www.codeweblog.com/win8-1%E4%B8%8Bfreeimage-lib%E5%BA%93%E7%9A%84%E9%85%8D%E7%BD%AE%E5%9 ...
- 【OJ-UVa227】
耗时一周.哭. 本题重在输入输出.所以对英文题目的理解非常重要.看清楚题目,省时省力. 题目要点: 1.开始有5×5的数据,每行仅有5个字符.注意:样例输入中的尾部空格是无法复制的(UVa官网上),其 ...
- django-orm简记
首先orm是什么? orm-------->对象关系映射 专业性解释网上一大推,随便搜搜就能了解大概.在我理解(通俗):一个类 ----- 数据库中一张表 类属性 ----- 数据表中的字段名 ...
- BZOJ3277: 串(广义后缀自动机)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1196 Solved: 478[Submit][Status][Discuss] Descripti ...
- .Net core NPOI导入导出Excel
最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...
- css实现下拉菜单功能(多中实现方式即原理)
引导思路: 1.需要用到的元素:position hover (z-index) 或(overflow)或(display)等等. 关键点就是div的溢出部分的处理. 2.实现过程: 2.1:就是要 ...
- nodeJs 对 Mysql 数据库的 curd
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : ' ...
- Linux 系统无法登录?你的程序有问题吧!
今天遇到一个问题,有个用户连接不上服务器(无法ssh远程连接) su: failed to execute /bin/bash: Resource temporarily unavailable 谷歌 ...
- python中一些内置函数实例
lambda表达式 简单函数可用lambda表达式 1. def f1() return(123) r1=f1() print() 2. f2=lambda:123 r2=f2() print() 以 ...
- Leecode刷题之旅-C语言/python-53.最大子序和
/* * @lc app=leetcode.cn id=53 lang=c * * [53] 最大子序和 * * https://leetcode-cn.com/problems/maximum-su ...