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并查集)的更多相关文章

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

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

  3. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

  4. 1013 Battle Over Cities (25 分)(图的遍历or并查集)

    这题用并查集或者dfs都可以做 dfs #include<bits/stdc++.h> using namespace std; ; bool mp[N][N]; int n,m,k; 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 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 ...

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

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

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

  9. 1034. Head of a Gang (30)

    分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...

随机推荐

  1. c语言描述的简单选择排序

    基本思想:首先,选出最小的数,放在第一个位置:然后,选出第二小的数,放在第二个位置:以此类推,直到所有的数从小到大排序 #include<stdio.h> #include<stdl ...

  2. o'Reill的SVG精髓(第二版)学习笔记——第六章

    第六章:坐标系统变换 想要旋转.缩放或者移动图片到新的位置.可以给对应的SVG元素添加transform属性. 6.1 translate变换 可以为<use>元素使用x和y属性,以在特性 ...

  3. sharepoint2013配置开发环境

  4. 使用transfor让图片旋转

    材料:Transform,onmouseout,onmouseover css: html: js:

  5. MySQL数据库初识——初窥MySQL

    初步了解MySQL基本数据库语言 1.创建一个Mysql数据库 create database  database_name: 2.显示所有的Mysql数据库 show databases: 3.使用 ...

  6. LeetCode-环形链表II

    LeetCode-环形链表II 为找到入口点可以用以下方法 使用快慢指针法直到两个指针相遇 头节点处创建一个新的指针,并且向前移动,两个指针相遇处创建一个新的指针,并且向前移动,直到两个指针相遇为入口 ...

  7. H5混合开发进阶

    混合开发: 原生app里面,IOS 安卓的原生app里面,嵌套h5界面. 通过原生app里的一个webView盒子进行交互.webView是原生app内置的一个XXX,里面可以放置h5界面.可以相互调 ...

  8. PHP如何实现99乘法表?

    看到这个问题,可能大家更多的是考虑到用for循环,个人觉得使用for循环太影响程序性能.推荐使用递归处理.  /** * Title : 递归实现99乘法表 * Author : Bruceqi * ...

  9. 吐血分享:QQ群霸屏技术教程2017(活跃篇)

    热门词的群排名,在前期优化准备充分的情况下,活跃度不失为必杀技. 在<吐血分享:QQ群霸屏技术(初级篇)>中,我们提及到热门词的群排名,有了前面的基础,我们就可以进入深度优化,实现绝对的霸 ...

  10. Qt on Android 蓝牙通信开发

    版权声明:本文为MULTIBEANS ORG研发跟随文章,未经MLT ORG允许不得转载. 最近做项目,需要开发安卓应用,实现串口的收发,目测CH340G在安卓手机上非常麻烦,而且驱动都是Java版本 ...