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. Android学习笔记_63_手机安全卫士知识点归纳(3)分享 程序锁 服务 进程管理 widget

    1.分享: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setT ...

  2. Storm 出现 no jzmq in java.library.path

    在真实环境中运行时,在log日志下,查看workpid日志发现出现该错误. 解决办法: 在conf/storm.yaml添加jzmq安装的路径, 我使用的默认安装在/usr/local/lib下 ja ...

  3. mac启动springboot失败,8080端口被占用,mac命令行关闭端口

    如下图,idea启动springboot失败,8080端口被占用 Error starting ApplicationContext. To display the conditions report ...

  4. vue 集成百度富文本编辑器

    <template> <div> <textarea style="display:none" id="editor_content&quo ...

  5. Jedis连接redis客户端

    1 单点的redis利用jedis客户端连接 如何连接 //1 利用jedis连接对象操作redis @Test public void test01(){ //构造一个具有连接信息的jedis对象 ...

  6. Javascript和android原生互调

    最近在做原生和js端的互调的功能,自己改了个demo,给大家讲解下. 先上js代码 <!DOCTYPE html> <html> <head> <meta c ...

  7. CRS

    CRS是集群就绪服务(cluster ready service)的简称,主要负责集群中的资源管理以及OCR管理.为了与10gR2集群管理软件名称crs区分,这里用CRSD代替CRS.相关概念:--资 ...

  8. Zabbix 3.x中使用Percona Monitoring Plugins监控MySQL

    1.下载安装percona-zabbix-templates-1.1.7-2.noarch.rpm 下载地址:https://www.percona.com/downloads/percona-mon ...

  9. scala成长之路(1)基本语法和数据类型

    scala作为JVM上的Lisp,是一种geek类型的编程语言,也一直是我等java程序员眼中的梦寐以求的一门技能,遂下定决心花一段时间好好学习scala.第一天学习,主要介绍与java在编程上的主要 ...

  10. STM32(4)——系统时钟和SysTick

    1.STM32的时钟系统 在STM32中,一共有5个时钟源,分别是HSI.HSE.LSI.LSE.PLL HSI是高速内部时钟,RC振荡器,频率为8MHz: HSE是高速外部时钟,可接石英/陶瓷谐振器 ...