图的遍历 DFS&BFS A1034head of gang &A1076forwords on weibo

图的遍历有两种方法,深度遍历和广度遍历,可以用邻接表或者邻接矩阵。邻接表用二维数组,邻接矩阵用vector.

  1. DFS 深度遍历
    邻接矩阵:
const inf =10000000;//表示两节点之间的距离无限大,说明两节点不联通
const int n ;//节点个数
int G[maxn][maxn];//表示邻接图
bool vis[maxn]={false};//表示节点是否被访问过
void dfs(int u,int depth)//深度遍历
{
vis[u]=true;//设置节点被访问
for(int i=0;i<n;i++){//遍历节点
if(vis[i]==false&&G[u][i]!=inf)//若与联通节点未被访问
dfs(i,depth+1);//深度递归该联通节点
}
}
void dfstrave ()//遍历图
{
for(int i=0;i<n;i++)//遍历图的所有节点
{
if(vis[i]==false)//如果该节点没有被访问
{
dfs{i,1};//则递归访问该节点及其联通块,1表示为第一层
}
}
}

邻接表

const int maxn;
vector <int> adj[maxn];
bool vis[maxn]={false};
void dfs(int u,int depth)
{
vis[u]=true;
if(int i=0;i<adj.size();i++)
{
int v=adj[u][i];
if(vis[v]==false)
{
dfs(i,depth+1);
}
}
}
void dfstrave()
{
for(int i=0;i<n;i++)
{
if(vis[i]==false)
{
dfs(i,1);
}
}
}

A1034 head of gang
原题描述:
One way that the police finds the head of a gang is to check people’s phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A “Gang” is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:
For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:
8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 1:
2
AAA 3
GGG 3

Sample Input 2:
8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 2:
0
参考答案:

#include <cstdio>
#include <string>
#include <map>
using namespace std;
const int maxn=2010,inf=1000000000;
int n,k,nump;
map <string,int> strtoint;
map <int,string> inttostr;
map <string,int> gang;
int G[maxn][maxn]={0},weight[maxn]={0};
bool vis[maxn]={false};
void dfs(int nowvis,int &head,int &num,int total)
{
	num++;
	vis[nowvis]=true;
	if(weight[nowvis]>weight[head])
	{
		head=nowvis;
	}
	for(int i=0;i<nump;i++)
	{
		if(G[nowvis][i]>0)
		{
			total+=G[nowvis][i];
			G[nowvis][i]=G[i][nowvis];
			if(vis[i]==false)
			{
				dfs(i,head,num,total);
			}
		}
	}
}
void dfstrave()
{
	for(int i=0;i<nump;i++)
	{
		if(vis[i]==false)
		{
			int head=i,num=0,total=0;
			dfs(i,head,num,total);
			if(num>2&&total>k)
			{
				gang[inttostr[head]]=num;
			}
		}
	}
}
int changestring(string str1)
{
	if(strtoint.find(str1)!=strtoint.end())
	{
		return strtoint[str1];
	}
	else
	{
		strtoint[str1]=nump++;
		inttostr[nump]= str1;
		return nump++;
	}
}
int main()
{
	scanf("%d %d",&n,&k);//calls numbers  ,  weight threthold
	string namev,namew;
	int w;
	for(int i=0;i<n;i++)
	{
		scanf("%s %s %d",&namev,&namew,&weight);
		int d1=changestring(namev);
		int d2=changestring(namew);
		weight[d1]+=w;
		weight[d2]+=w;
	}
	dfstrave();
	printf("%d",gang.size());
	map<string,int>::iterator it;
	for(it=gang.begin();it!=gang.end();it++)
	{
		printf("%s %d",it->first,it->second);
	}
	return 0;
}
  1. BFS广度遍历:
    邻接矩阵:

邻接表:

A1076 forwords on weibo
原题描述:
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=1000), the number of users; and L (<=6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:

M[i] user_list[i]

where M[i] (<=100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that are followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

Then finally a positive K is given, followed by K UserID’s for query.

Output Specification:

For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can triger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.
Sample Input:

7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6

Sample Output:

4
5
参考答案:

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn=2010;
struct node
{
	int layer;
	int id;
};
vector <node> adj[maxn];
bool inq[maxn]={false};
int bfs(int u,int l)
{
	queue <node> q;
	node s;
	int numfor=0;
	s.id=u;s.layer =0;
	q.push(s);
	inq[u]=true;
	while(!q.empty())
	{
		node top=q.front();
		q.pop();
		int it=top.id;
		for(int v=0;v<adj[u].size();v++)
		{
			node next=adj[it][v];
			next.layer=top.id+1;
			if(inq[next.id]==false&&next.layer<l)
			{
				q.push(next);
				inq[next.id]=true;
				numfor++;
			}
		}
		
 	}
	return numfor++;
}

void main()
{
	int n,l;
	scanf("%d %d",&n,&l);
	int t,temp;
	node user;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&t);
		user.id=i;
		for(int j=0;j<t;j++)
	    {
			scanf("%d",&temp);
			adj[temp].push_back(user);
	    }
	}
	int numfollow,sv;
	scanf("%d",&numfollow);
	for(int i=0;i<numfollow;i++)
	{
		memset(inq,false,sizeof(inq));
		scanf("%d",&sv);
		int numforword=bfs(sv,l);
		printf("%d\n",numforword);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值