[PAT] Grade 1034 Head of a Gang (30 minutes)

Meaning of the questions:

Enter two positive integers N and K (<= 1000), the next input data N rows, each row including two of the three ID capital letters, as well as two call time. The number of output gangs (each other through the telephone number of> = 3), and regarded as the boss in accordance with the number of lexicographical output when the gang boss of the gang's ID and call (the longest of long gang of people, a gang of data to ensure that only one boss).

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
string s,s2;
int x[1007],y[1007];
vector<pair<int,int> >edge[20007];
bool vis[20007];
int val[20007];
int mx=0;
int pos=0;
int ans[20007];
int num[20007];
int cnt=0;
int c[1007];
bool visedge[1007];
int sum2=0;
void dfs(int x){
if(!vis[x])
++cnt;
vis[x]=1;
for(auto it:edge[x]){
if(visedge[it.second])
continue;
visedge[it.second]=1;
if(!vis[it.first]){
++cnt;
vis[it.first]=1;
}
val[x]+=c[it.second];
val[it.first]+=c[it.second];
sum2+=c[it.second];
if(val[x]>mx){
mx=val[x];
pos=x;
}
dfs(it.first);
}
}
int main(){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;++i){
cin>>s>>s2>>c[i];
x[i]=(s[0]-'A')*26*26+(s[1]-'A')*26+s[2]-'A';
y[i]=(s2[0]-'A')*26*26+(s2[1]-'A')*26+s2[2]-'A';
edge[x[i]].push_back({y[i],i});
edge[y[i]].push_back({x[i],i});
}
int sum=0;
for(int i=1;i<=n;++i){
sum2=0;
mx=0;
pos=0;
cnt=0;
if(!vis[x[i]])
dfs(x[i]);
if(cnt>2&&sum2>k){
ans[pos]=sum2;
num[pos]=cnt;
++sum;
}
}
cout<<sum<<"\n";
for(int i=0;i<26*26*26;++i){
if(ans[i]){
char alp[3];
int tmp=i;
alp[0]=tmp/26/26;
tmp%=26*26;
alp[1]=tmp/26;
tmp%=26;
alp[2]=tmp;
for(int j=0;j<=2;++j){
alp[j]+='A';
cout<<alp[j];
}
cout<<" "<<num[i]<<"\n";
}
}
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11521016.html