[Baekjun] Number 2644: Calculate the dimensions (C++)

a matter of concern.

Number 2644: Village number calculation problem Korea has a unique culture that expresses the relationship between family members or relatives in the unit of village number. The number of such villages is calculated in the following way. Basically, the parent-child relationship is defined as the first degree of kinship, and the number of villages between people is calculated from now on. For example, me, my father, my father and my grandfather go to one village, my grandfather go to two villages, my father and my brother go to one village, and my father and my brother go to three villages. Write a program to calculate the number of villages for two people when a parent-child relationship is given to multiple people. Enter one, two… www.acmicpc.net

Number 2644: Village number calculation problem Korea has a unique culture that expresses the relationship between family members or relatives in the unit of village number. The number of such villages is calculated in the following way. Basically, the parent-child relationship is defined as the first degree of kinship, and the number of villages between people is calculated from now on. For example, me, my father, my father and my grandfather go to one village, my grandfather go to two villages, my father and my brother go to one village, and my father and my brother go to three villages. Write a program to calculate the number of villages for two people when a parent-child relationship is given to multiple people. Enter one, two… www.acmicpc.net

Japan has a unique culture that expresses the relationship between family members or relatives in terms of dimensions. The number of such villages is calculated in the following way. Basically, the parent-child relationship is defined as the first degree of kinship, and the number of villages between people is calculated from now on. For example, me, my father, my father and my grandfather go to one village, my grandfather go to two villages, my father and my brother go to one village, and my father and my brother go to three villages.Write a program to calculate the number of villages for two people when a parent-child relationship is given to multiple people.Since the direction between the pool nodes is bidirectional, we started by adding values for each graph. It is a problem to return the shortest distance between the starting point A and the arrival point B. I wasted a lot of time solving it because I thought I should save only the A->B distance, but if I think about it briefly, I can save the shortest distance in a vector. For example, there is a graph of 1->2->3, and the shortest distance to 1->3 should be found. The distance at 1->2 is 1, so it becomes visited[2]=visited[1]+1, and the distance at 2->3 is 1, so it becomes visited[3]=visited[2]+1. In other words, the shortest distance is 2.Each time a new point is put into the queue, the distance moved can be put into the next node distance vector.It’s not easy to explain, but it’s easy to understand if you try to debug the curNode and nextNode logs when queue.push when you’re confused

#include<iostream>#include<vector>#include<queue>名前空間 std;int N, A, B, M;void bfs(vector<vector<int>&graph, vector<int>&visited)Use . { true<int> q;.push(A); hile (!q.empty ())nt temp = = q.front ();f (temp == B)out << visited [temp];eturn;or (inti= 0); i < graph [temp].size (); i++)nt next= graph [temp];f (visited[next]== 0.pop] + + 1)<INTurnt > <>{main> main> main> main> main> main <, 0) 0)0);or (int i = 0; i < M; i++)nt x, y;in >> x >> x >> y;raph[x]。push_back(y);raph[y]Click .Push_back (x) ( graph, display));

#include<iostream>#include<vector>#include<queue>名前空間 std;int N, A, B, M;void bfs(vector<vector<int>&graph, vector<int>&visited) {ueue<int> is being used q;.push(A); hile (!q.empty ())nt temp = = q.front ();f (temp == B)out << visited [temp];eturn; or (inti = 0; i < graph [temp].size (); i++)nt next= graph [temp];f (visited[next]== 0.pop] + + 1)<INTurnt > <>{main> main> main> main> main> main <, 0)0)0);or (int i = 0; i < M; i++)nt x, y;in >> x >> x >> y;raph[x]。push_back(y);raph[y] and then click the .push_back(x);fs(graph, displayed);

 

error: Content is protected !!