PAT 1034. Head of a Gang (30) DFS,删边
2017-08-22 20:49
483 查看
一开始以为是用并查集,后来发现是用DFS的。亮点在于DFS只是载体,删掉遍历过的边才是重点
#include <iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int sum=0;
int edge[1001][1001]={0};
int cnt;
int head=-1;
int p;
int tiime[2002]={0};
int mark[2002]={0};
int max(int z,int c) {return z>c?z:c;}
int toutou;
struct ZZ
{ int id;
int cnt;
string name;
bool operator < (const ZZ &A) const{
return name<A.name;
}
}tutu[1000];
void DFS(int x)
{
if(mark[x]==0)cnt++;
mark[x]=1;
for(int i=0;i<p;i++)
{
if(edge[x][i]>0)
{ tiime[x]+=edge[x][i];
tiime[i]+=edge[x][i];
sum+=edge[x][i];
edge[x][i]=edge[i][x]=0;
DFS(i);
}
head=max(head,tiime[x]);
if(head==tiime[x]) toutou=x;
}
}
int main()
{
int n,k;
cin>>n>>k;
p=0;
map<string,int> M;
map<int,string> SM;
for(int i=0;i<n;i++)
{
int c;
int t1,t2;
string a,b;
cin>>a>>b>>c;
if(M.find(a)==M.end()) {M[a]=p++;t1=p-1;}
else t1=M[a];
if(M.find(b)==M.end()) {M[b]=p++;t2=p-1;}
else t2=M[b];
edge[t1][t2]+=c;
edge[t2][t1]+=c;
SM[t1]=a;
SM[t2]=b;
}
int p2=0;
for(int i=0;i<p;i++)
{ cnt=0;
head=-1;
toutou=-1;
sum=0;
if(mark[i]==0) DFS(i);
if(cnt>2&&sum>k)
{
tutu[p2].id=toutou;
tutu[p2].cnt=cnt;
tutu[p2].name=SM[toutou];
p2++;
}
}
sort(tutu,tutu+p2);
cout<<p2<<endl;
for(int i=0;i<p2;i++)
cout<<SM[tutu[i].id]<<' '<<tutu[i].cnt<<endl;
return 0;
}
#include <iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int sum=0;
int edge[1001][1001]={0};
int cnt;
int head=-1;
int p;
int tiime[2002]={0};
int mark[2002]={0};
int max(int z,int c) {return z>c?z:c;}
int toutou;
struct ZZ
{ int id;
int cnt;
string name;
bool operator < (const ZZ &A) const{
return name<A.name;
}
}tutu[1000];
void DFS(int x)
{
if(mark[x]==0)cnt++;
mark[x]=1;
for(int i=0;i<p;i++)
{
if(edge[x][i]>0)
{ tiime[x]+=edge[x][i];
tiime[i]+=edge[x][i];
sum+=edge[x][i];
edge[x][i]=edge[i][x]=0;
DFS(i);
}
head=max(head,tiime[x]);
if(head==tiime[x]) toutou=x;
}
}
int main()
{
int n,k;
cin>>n>>k;
p=0;
map<string,int> M;
map<int,string> SM;
for(int i=0;i<n;i++)
{
int c;
int t1,t2;
string a,b;
cin>>a>>b>>c;
if(M.find(a)==M.end()) {M[a]=p++;t1=p-1;}
else t1=M[a];
if(M.find(b)==M.end()) {M[b]=p++;t2=p-1;}
else t2=M[b];
edge[t1][t2]+=c;
edge[t2][t1]+=c;
SM[t1]=a;
SM[t2]=b;
}
int p2=0;
for(int i=0;i<p;i++)
{ cnt=0;
head=-1;
toutou=-1;
sum=0;
if(mark[i]==0) DFS(i);
if(cnt>2&&sum>k)
{
tutu[p2].id=toutou;
tutu[p2].cnt=cnt;
tutu[p2].name=SM[toutou];
p2++;
}
}
sort(tutu,tutu+p2);
cout<<p2<<endl;
for(int i=0;i<p2;i++)
cout<<SM[tutu[i].id]<<' '<<tutu[i].cnt<<endl;
return 0;
}
相关文章推荐
- PAT - 甲级 - 1034. Head of a Gang (30)(DFS求连通块)
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- PAT甲级1034. Head of a Gang (30)
- PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
- PAT甲级 1034. Head of a Gang (30)
- 【PAT】【Advanced Level】1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- Python实现Pat 1034. Head of a Gang (30)
- PAT甲题题解-1034. Head of a Gang (30)-并查集
- PAT 1034. Head of a Gang (30) 并查集
- 【PAT甲级】1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- PAT (Advanced) 1034. Head of a Gang (30)
- PAT (Advanced Level) 1034. Head of a Gang (30) 黑帮头目,并查集
- 1034. Head of a Gang (30) (并查集 或者 DFS)
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise