【解決方法】無向グラフに関するこの質問に役立つ可能性のある疑似 CDE またはアルゴリズムはありますか?


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
```
#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) 
{


}

“`
この質問に役立つアルゴリズムの疑似コードはありますか?

私が試したこと:

利用できるスタックやキューがないので、どこから始めればよいかわかりません

解決策 1

私たちは立ち往生している人々を喜んで助けますが、それは私たちがあなたのためにすべてをするためにここにいるという意味ではありません! 私たちがすべての作業を行うことはできません。あなたはこれに対して報酬を受け取っているか、またはそれはあなたの成績の一部であり、私たちがあなたのためにすべてを行うことはまったく公平ではありません.

だから私たちはあなたが仕事をする必要があり、あなたが行き詰まったときにあなたを助けます. それは、あなたが提出できる段階的な解決策を提供するという意味ではありません!
現在の状況と、プロセスの次のステップを説明することから始めます。 次に、その次のステップを機能させるために何を試みたか、またその際に何が起こったかを教えてください。

手動で行う方法を考えることから始めて、それをコンピューターが処理できるものに変換することを検討します!

開始するのに問題がある場合は、これが役立つ場合があります。 問題を解決するためのコードの書き方、初心者向けガイド[^]

コメント

タイトルとURLをコピーしました