4014 - solution of simple path with length k based on adjacency table (C + +, with ideas)

Keywords: C++ Algorithm data structure

describe

A connected graph uses adjacency table as storage structure. An algorithm is designed to judge whether there is a simple path with length k at any given two points in an undirected graph.

input

Multiple groups of data, each group of m+3 data rows. The first line has two numbers n, m and k, representing n vertices, m edges and length k. The second line has n characters, representing the number of n vertices. From the third line to the m+2 line, each line has two characters h and p, representing the two vertices to which the edge is attached. The length of each edge is 1. Line m+3 has two characters d and f, representing the two characters to be judged.

output

Output one row for each group of data. If there is a path, output "YES", otherwise output "NO".

Input sample 1  

3 2 2
abc
ab
bc
ac
4 2 5
bcsw
bs
sw
bw
0 0 0

Output sample 1

YES
NO

Idea:

The basic operations such as the establishment of adjacency tables are written in this article, so they will not be repeated below~

4003 addition of new vertices based on adjacency table (C + +, with detailed analysis)_ Crane Tianxun blog - CSDN bloghttps://blog.csdn.net/qq_54416938/article/details/121582300?spm=1001.2014.3001.5501

a global map is used to store the sequence number nodes corresponding to each node. For example, abc is used to establish the adjacency table. a corresponds to 1, b corresponds to 2, and c corresponds to 3. Then they are all stored in the map, and then the input string is used to initialize the adjacency table.

Then input the strings one by one, find the sequence number of the current node in the adjacency table with the help of the iterator of map, and establish the adjacency table.

After that, find the path, set a pedometer len, and let len + + go every step. If you are finished, continue to traverse in the adjacency table with the last node as the starting point until you reach the target node. Compare the values of len and k, and if they are equal, it is YES, otherwise NO will be output.

I think the highlight is

            pos = GetPos(p->data);
            p = &alg.VList[pos];

These two sentences of code and the idea of map achieve the effect of "loop traversal".

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<set>
#include<map>
using namespace std;
map<char, int> mp;
typedef struct LNode
{
	char data;
	struct LNode* next;
}*linklist, LNode;
typedef struct
{
	int arcnum;
	int vexnum;
	linklist VList;
}ALGraph;
void Create(ALGraph& alg, int n, int m)
{
	alg.arcnum = m;
	alg.vexnum = n;
	alg.VList = new LNode[n + 1];
	string ves;
	cin >> ves;

	for (int i = 0; i < ves.length(); i++)
		mp[ves[i]] = i + 1;
	int j = 0;
	for (int i = 1; i <= n; i++)
	{
		alg.VList[i].data = ves[j++];
		alg.VList[i].next = NULL;
	}
	map<char, int>::iterator it = mp.begin();
	for (int i = 0; i < m; i++)
	{
		string t;
		cin >> t;
		linklist p = new LNode;
		p->data = t[1];
		while (it != mp.end())
		{
			if (it->first == t[0])
			{
				p->next = alg.VList[it->second].next;
				alg.VList[it->second].next = p;
				break;
			}
			it++;
		}
	}
}
int GetPos(char c)
{
	map<char, int>::iterator it = mp.begin();
	int pos = 0;
	while (it != mp.end())
	{
		if (it->first == c)
		{
			pos = it->second;
			break;
		}
		it++;
	}
	return pos;
}
void Find(ALGraph alg,int k)
{
	string t;
	cin >> t;
	int pos = GetPos(t[0]);
	linklist p = &alg.VList[pos];
	int len = 0;
	while (p)
	{
		len++;
		p = p->next;
		if (p->data == t[1])
			break;
		else
		{
			pos = GetPos(p->data);
			p = &alg.VList[pos];
		}
	}
	if (len != k)
		cout << "NO" << endl;
	else
		cout << "YES" << endl;
}
int main()
{
	int m, n, k;
	while (cin >> n >> m >>k&& m != 0 && n != 0&&k!=0)
	{
		ALGraph a;
		Create(a, n, m);
		Find(a, k);
	}
	return 0;
}

Posted by vitorjamil on Sat, 27 Nov 2021 23:32:48 -0800