Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You are given a city network in the form of a tree(an acyclic graph). There are N cities and each city has a unique ID (integer from 1 to N). There exists a unique path between each pair of cities. There are only N-1 paths in the city network. The population of each city is provided.

Given Q queries, you need return the number of cities that have a population of at most W on the path from U to V. U and V are the given city ID.

Inputs:
No. of cities = 5

Population for each cities = 4 7 8 6 4

Road Available: 1 and 5,
5 and 2,
2 and 3,
2 and 4

No. of Queries: 5

Query 1(U,V,W): 4 2 1

Query 2(U,V,W): 2 3 7

Query 3(U,V,W): 2 4 8

Query 4(U,V,W): 5 3 1

Query 5(U,V,W): 3 4 8


Expected output: 0
1
2
0
3
```
C
#include<stdio.h>
#include <stdlib.h>
  
int* city_population (int N, int* population, int** road, int Q, int** cities) ;

int main() {
    int N;
    scanf("%d", &N);
    int i_population;
    int *population = (int *)malloc(sizeof(int)*(N));
    for(i_population = 0; i_population < N; i_population++)
    	scanf("%d", &population[i_population]);
    int i_road, j_road;
    int **road = (int **)malloc((N-1)*sizeof(int *));
    for(i_road = 0; i_road < N-1; i_road++)
    {
    	road[i_road] = (int *)malloc((2)*sizeof(int));
    }
    for(i_road = 0; i_road < N-1; i_road++)
    {
    	for(j_road = 0; j_road < 2; j_road++)
    	{
    		scanf("%d", &road[i_road][j_road]);
    	}
    }
    int Q;
    scanf("%d", &Q);
    int i_cities, j_cities;
    int **cities = (int **)malloc((Q)*sizeof(int *));
    for(i_cities = 0; i_cities < Q; i_cities++)
    {
    	cities[i_cities] = (int *)malloc((3)*sizeof(int));
    }
    for(i_cities = 0; i_cities < Q; i_cities++)
    {
    	for(j_cities = 0; j_cities < 3; j_cities++)
    	{
    		scanf("%d", &cities[i_cities][j_cities]);
    	}
    }

    int* out_ = city_population(N, population, road, Q, cities);
    printf("%d", out_[0]);
    int i_out_;
    for(i_out_ = 1; i_out_ < Q; i_out_++)
    	printf("\n%d", out_[i_out_]);
}

int* city_population (int N, int* population, int** road, int Q, int** cities) 
{


}

```
Any algorithm pseudocode that might help this question?

What I have tried:

As there isnt a stack or queue for me to utilise i have no idea where to start out
Posted
Updated 27-Mar-23 8:07am
v3
Comments
Rick York 27-Mar-23 10:50am    
If I was doing this I would read all input from a file using fgets. This would make your testing much faster and easier. You can still read input from the console by using stdin as the file pointer if you want or need to.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

I'd start by thinking about how you would do it manually, and then consider converting that to something a computer can cope with!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
It sounds similar like the Travelling salesman problem. So you might look at this problem and its solution.
The best solution may be some heuristic approach by trying each combination and evaluate them.

Tip: use structs and arrays to write better code. I like than give pointers to structs as parameter into functions. Than you can separate the code better in different files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900