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 ...
随机推荐
- SPFA 全面讲解
SPFA全面讲解 --最短路高效算法 标签: 最短路 简介:SPFA 是1994年在西安交通大学段凡丁同学所提出,是将Dijsktra以及Bellman-Ford两种最短路算法完美结合的一个算法,效率 ...
- eclipse 安装/卸载插件
1.通过Help>>Install New Soft 之后弹出对话框,可以通过“Add”按钮添加已经有的插件的(zip等)或者输入安装地址,之后按照要求即可.2.对于安装失败的插件,再次进 ...
- Java基础——XML复习
XML SGML : 标准通用置标语言 Standard Generailzed Markup Language XML ...
- nginx 方向代理
#nginx 监听原理 先监听端口 --> 再配置域名 -->匹配到就访问local 否则 没有匹配到域名就默认访问第一个监听端口的local地址 # vi nginx.conf user ...
- C++的抽象类、虚函数、虚基类和java的抽象类和接口
简单整理如下: C++虚函数 == java普通函数 C++纯虚函数 == java抽象函数 C++抽象类 == java抽象类 C++虚基类(全都是纯虚函数) == java接口
- lua浮点数取整
向下取整 math.floor(num) 向上取整 math.ceil(num) 取整取余 math.modf(num) 测试 num = 12.4 print(math.floor(num)) 12 ...
- 构建高可靠hadoop集群之4-保全模式
本文主要翻译自http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/SecureMode.html 译注:之所以 ...
- echarts饼图扇区添加点击事件
在echarts最后面添加上这段代码就可以了 function eConsole(param) { //alert(option.series[0].data.length); //alert(opt ...
- 解决每次运行Xcode,都需要输入密码的问题
新买的Mac,在安装了 Xcode 7.1的时候,不知道是配置信息哪里手残了一下,导致每次运行Xcode模拟器 后 都需要输入一次密码. 为此在网上也是查阅了不少的资料,当时 所谓的 XCode--- ...
- css公共类
/*iOS弹性滚动*/ .scrolling{ position: absolute; width: 100%; height:100%; overflow-x:hidden; overflow-y: ...