pat-1034

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;//phone给的是边数没给点数要乘以二(注意) 
int visit[2010]={0},numm=0,weight=0,a[2010][2010],head=0,nt=1,w[2010]={0};//numm要条件重置,nt从1开始不能和判断有没有的0冲突 
map<string,int> si;
map<int,string> is;
map<string,int> ans; 
int stoin(string kp ){
	if(si[kp]==0){//不存在给标号 
		si[kp]=nt;
		is[nt]=kp;
		nt++;
		return si[kp];
	}
	else{
		return si[kp];
	}
}
void dfs(int f){//记两个量 nummemeber weight head 
	visit[f]=1;
	numm++;
	if(w[f]>w[head]){
		head=f;
	}
	for(int i=1;i<=si.size();i++){
		if(a[f][i]!=0){
			weight+=a[f][i];//注意本题要将点标记与边标记分开,如果只按点标记会漏掉形成环的一条边的权值 
			a[f][i]=a[i][f]=0;//标记边(注意) 
			if(visit[i]==0) //标记点(分开) 
			dfs(i);
		}
	}
	
} 
int main(){
int m,k;
string  _1,_2;
int _3,_10,_11;
//fill(a[0],a[0]+1010*1010,0); 
cin>>m>>k;
for(int i=0;i<m;i++){
	cin>>_1>>_2>>_3;
	_10=stoin(_1 );//函数圆括号 
	_11=stoin(_2);
	a[_10][_11]+=_3;
	a[_11][_10]+=_3;//出问题边权 应合并为一个 
	w[_10]+=_3;
	w[_11]+=_3;
}
for(int i=1;i<=si.size();i++){
	
	if(visit[i]==0){
		numm=0;weight=0;head=0;
		dfs(i);
		if(numm>2&&weight>k){
			ans[is[head] ]=numm;
		}
	}
}
printf("%d\n",ans.size());
for(auto it=ans.begin();it!=ans.end();it++){
	cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}

to sum up

1. Determine the number of connected components DFS and traverse all vertices once  

2. When no vertices are given, only the number of edges is given, allowing you to store data during the input process. If it is only a number set, you can record the number. If string creates two key-value pairs, and assign values ​​in the order of his input

3. Combine the two edges into one weight, mark the edges at the same time, and separate them from the marked points. If you only follow the marked points, there will be missing edges when there is a ring, so you must separate, add the past and change it to 0, and the points are only recorded Should you visit down

4. Function parentheses

5. If the number of sides is not given, multiply the number of points by two, otherwise a segmentation error will occur (note)

6. nt starts from 1 and cannot conflict with 0 to determine whether there is a 0

7. Which amount of currency a function needs to be clear

English

 

problem

dfs is too slow to write, summarize the templates of various question types

 

 

Guess you like

Origin blog.csdn.net/m0_45359314/article/details/112950862