Nodes are indexed by positive integers from 1 ton .
Each edge is represented by (u,v) connecting node u and node v .
Distance of two nodes, say x and y, is defined as number of edges on the shortest path between x and y, if x and y are reachable from each other; otherwise, it is defined as 0.
There is a reference node s, and a special query Max Index (d), which is defined as maximal index of node among nodes whose distance to s is d.
If there is no node whose distance to s is d, Max Index(d)is defined as 0 .
Our task is to respond a sequence of n queries, D=di(i=1~n).
Input Format
The 1st line is n, m and s .
The following m lines are edges.
The following line is D.
Output Format
Max Index(di)
Sample Input 0
6 7 1
1 2
1 3
2 3
2 4
3 5
4 5
4 6
2 1 3 1 2 3
Sample Output 0
5 3 6 3 5 6
How can this problem be solved, I don't think I'm on the right path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// C++ program to find minimum edge
// between given two vertex of Graph
#include<bits/stdc++.h>
using namespace std;
// function for finding minimum no. of edge
// using BFS
int minEdgeBFS(vector <int> edges[], int u,
int v, int n)
{
// visited[n] for keeping track of visited
// node in BFS
vector<bool> visited(n, 0);
// Initialize distances as 0
vector<int> distance(n, 0);
// queue to do BFS.
queue <int> Q;
distance[u] = 0;
Q.push(u);
visited[u] = true;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i=0; i<edges[x].size(); i++)
{
if (visited[edges[x][i]])
continue;
// update distance for i
distance[edges[x][i]] = distance[x] + 1;
Q.push(edges[x][i]);
visited[edges[x][i]] = 1;
}
}
return distance[v];
}
// function for addition of edge
void addEdge(vector <int> edges[], int u, int v)
{
edges[u].push_back(v);
edges[v].push_back(u);
}
int main()
{
int n,m,s,u,v;
cin>>n>>m>>s;
vector <int> edges[n];
while(m--)
{cin>>u>>v;
addEdge(edges, u, v);}
vector<int>D(n);
for(auto &d:D) cin>>d;
return 0;
}
|