(算法)Travel Information Center

时间:2023-10-06 16:13:56

题目:

Aps Island has many cities. In the summer, many travellers will come to the island and attend festive events in different cities. The festive events in Aps Island are crazy. Once it starts, it will never end. In the following sentences, the cities which have festive events are called festive cities.
At the beginning, only city No. 1 is festive city. If a new city becomes festive city, the government will tellthe information center about this news.
Everyday, the information center will receive many inquiries from travellers from different cities of this land. They want to know the closest festive city, and calculate the distance (If current city has festive event, the distance is 0).
Due to the growing number of the travellers, the information center is overloaded. The government wants to fix the problem by developing a system to handle the inquiries automatically.
As a fact, cities in Aps Island are connected with highways(bidirectional, length of every highway is 1). Any two cities are connected directly or indirectly, and there is ONLY one path between any 2 cities.

Input:
 There are two integers in the first line, n (2<=n<=10^5) and m (1<=m<=10^5), n is the number of cities in the Aps Island and m is the number of queries.

The coming n-1 lines are the highways which connect two cities. In the line, there are two integers ai and bi (1<=ai,bi<=n,ai!=bi), representing two cities.  Each line means the highway connecting the two cities.
 Next m lines are inquiries from travellers or news from government. Each line has two integers qi andci (1<=qi<=2,1<=ci<=n). If qi=1, the government announces a new festive city ci. If qi=2, you have  to find and print the shortest distance from the city ci to the closest festive city.

Output:
  Results from each (qi = 2) Questions. Print every result with a new line.

C++
int main(){
// TODO: Implement your program
}

Sample Test
input
5 5

1 2

1 3

3 4

3 5

2 5

2 3

1 3

2 3

2 4
output
2

1

0

1

思路:

1、DFS

2、算法优化

代码:

1、DFS

#include<iostream>
#include<vector> using namespace std; struct Node{
vector<int> adjList;
}; void dfs(const vector<Node> &cities,vector<int> &dis,int x,int p){
vector<int> adj=cities[x].adjList;
for(int i=;i<adj.size();i++){
if(adj[i]==p)
continue;
if(dis[adj[i]]==- || dis[adj[i]]>dis[x]+){
dis[adj[i]]=dis[x]+;
dfs(cities,dis,adj[i],x);
}
}
} int main(){
int city_num;
int query_num;
int city_1,city_2; //input: two connected cities
int query,city; //input: query type and city
while(cin>>city_num && cin>>query_num){
if(city_num> && query_num>){
vector<Node> cities(city_num+);
vector<int> distances(city_num+,-);
// input information
for(int i=;i<city_num-;i++){
if(cin>>city_1 && cin>>city_2){
if(city_1> && city_1<=city_num && city_2> && city_2<=city_num){
cities[city_1].adjList.push_back(city_2);
cities[city_2].adjList.push_back(city_1);
}
else
return ;
}
} distances[]=;
dfs(cities,distances,,); for(int i=;i<query_num;i++){
cin>>query>>city;
if(query==){
distances[city]=;
dfs(cities,distances,city,);
}
else
cout<<distances[city]<<endl;
}
}
} return ;
}

2、算法优化

#include<iostream>
#include<vector> using namespace std; #define MIN_DISTANCE 1000000
typedef struct Node CityNode; /***** definition of data structure about each city *****/
struct Node{
int parent;
int depth;
bool isFestival;
vector<int> adjList;
Node():parent(-),depth(),isFestival(false){}
}; /***** function declaration *****/
// compute parent and depth of each node on the tree
void getParentAndDepth(vector<CityNode> &citites,int city_num);
// compute distance from the festival city by finding the nearear common parents
int getDistFromFesCity(vector<CityNode> &cities,int cur_city,int fes_city,vector<vector<int> > &distances); /***** main function *****/
int main(){
int city_num;
int query_num;
int city_1,city_2; //input: two connected cities
int query,city; //input: query type and city
while(cin>>city_num && cin>>query_num){
if(city_num> && query_num>){
vector<CityNode> cities(city_num);
vector<vector<int> > distances(city_num,vector<int>(city_num,));
// input information
for(int i=;i<city_num-;i++){
if(cin>>city_1 && cin>>city_2){
if(city_1> && city_1<=city_num && city_2> && city_2<=city_num){
cities[city_1-].adjList.push_back(city_2-);
cities[city_2-].adjList.push_back(city_1-);
}
else
return ;
}
} // compute parent,depth of each node on the tree
getParentAndDepth(cities,city_num); vector<int> festivalCity; //city who announced as festival city
vector<int> miniDist; // minimum distance of each query
festivalCity.push_back();
cities[].isFestival=true;
int dist; // find the nearest path from all festival cities
for(int i=;i<query_num;i++){
if(cin>>query && cin>>city){
int nearest=MIN_DISTANCE;
// if query==1, add to festival cities
if(query== && city> && city<=city_num){
festivalCity.push_back(city-);
cities[city].isFestival=true;
}
// if query==2, find the nearest festival city
else if(query== && city> && city<=city_num){
for(int k=;k<festivalCity.size();k++){
if(distances[city-][festivalCity[k]]!=)
dist=distances[city-][festivalCity[k]];
else
dist=getDistFromFesCity(cities,city-,festivalCity[k],distances);
if(dist<nearest)
nearest=dist;
}
miniDist.push_back(nearest);
}
else
return ;
}
} for(int i=;i<miniDist.size();i++)
cout<<miniDist[i]<<endl;
}
}
return ;
} void getParentAndDepth(vector<CityNode> &cities,int city_num){
vector<int> stk;
stk.push_back();
int node;
int v;
int count=;
while(!stk.empty() && count<city_num){
node=stk.back();
stk.pop_back();
for(int i=;i<cities[node].adjList.size();i++){
v=cities[node].adjList[i];
if(v== ||cities[v].parent!=-)
continue;
cities[v].parent=node;
cities[v].depth=cities[node].depth+;
stk.push_back(v);
count++;
}
}
} int getDistFromFesCity(vector<CityNode> &cities,int cur_city,int fes_city,vector<vector<int> > &distances){
int a=cur_city;
int b=fes_city; if(a==b)
return ;
int dist=;
while(cities[a].depth>cities[b].depth){
a=cities[a].parent;
dist++;
}
while(cities[a].depth<cities[b].depth){
b=cities[b].parent;
dist++;
}
while(a!=b){
a=cities[a].parent;
dist++;
b=cities[b].parent;
dist++;
} distances[cur_city][fes_city]=dist; return dist;
}