如何将数组转换为树的邻接列表?

huangapple go评论70阅读模式
英文:

How i convert array to adjacency list of tree?

问题

这是一个问题:

编写一个程序,确定树的两个节点中的第一个节点是否是另一个节点的父节点。

输入数据

第一行包含树中顶点的数量n(1 ≤ n ≤ 10^5)。第二行包含n个数字,第i个数字给出顶点i的直接祖先的顶点号。如果这个值为零,那么该顶点是树的根。

第三行包含查询数量m(1 ≤ m ≤ 10^5)。接下来的每个m行包含两个不同的数字a和b(1 ≤ a, b ≤ n)。

输出数据

对于每个m个查询,如果顶点a是顶点b的父节点之一,则在单独的行上打印数字1,否则打印数字0。

示例树

输入示例 #1

6
0 1 1 2 3 3
5
4 1
1 4
3 6
2 6
6 5

输出示例 #1

0
1
1
0
0

链接:问题链接

我花了几个小时,但是我无法将输入的第二部分转换成邻接矩阵。这是我需要的全部信息。我已经找到了解决方案的其他部分。

我的解决方案(C++):

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> g;
vector<int> time_in, time_out;
int Time = 0;

void dfs(int node, int parent) {
    time_in[node] = ++Time;
    for (int &to : g[node]) {
        if (to != parent) {
            dfs(to, node);
        }
    }
    time_out[node] = ++Time; // 或者 time_out[node]=Time;
}

bool isAncestor(int ancestor, int node) {
    return time_in[ancestor] <= time_in[node] and time_out[ancestor] >= time_out[node];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    time_in.resize(n + 1);
    time_out.resize(n + 1);
    g.resize(n + 1);
    vector<int> ancestors(n + 1);
    for (int i = 1; i <= n; ++i) { // 我认为祖先是排序的。
        cin >> ancestors[i];
    }

    int start = 1;

    dfs(start, 1);

    int q, u, v;
    cin >> q;
    while (q--) {
        cin >> u >> v;
        cout << isAncestor(u, v) << '\n'; // v的祖先是u。//(u,v)--> (a,b)
    }
    return 0;
}
英文:

This is problem:

Write a program that determines for two nodes of a tree whether the first one is a parent of another.

Input data

The first line contains the number of vertices n (1 ≤ n ≤ 10^5) in a tree. The second line contains n numbers, the i-th one gives the vertex number of direct ancestor for the vertex i. If this value is zero, then the vertex is a root of a tree.

The third line contains the number of queries m (1 ≤ m ≤ 10^5). Each of the next m lines contains two different numbers a and b (1 ≤ a, b ≤ n).

Output data
For each of the m queries print on a separate line number 1, if the vertex a is one of the parents of a vertex b, and 0 otherwise.

Example tree

Input example #1

6
0 1 1 2 3 3
5
4 1
1 4
3 6
2 6
6 5

Output example #1

0
1
1
0
0

Link: Problem

I tried for hours but i couldn't convert the second part of the input into an adjacency matrix. This is all I need. I have found the other parts of solution.

My soluton(C++):

#include&lt;iostream&gt;
#include&lt;vector&gt;
#include&lt;algorithm&gt;
using namespace std;
vector&lt;vector&lt;int&gt;&gt; g;
vector&lt;int&gt; time_in,time_out;
int Time=0;
void dfs(int node,int parent){
    time_in[node]=++Time;
    for(int &amp;to:g[node]){
        if(to!=parent){
                dfs(to,node);
        }
    }
    time_out[node]=++Time;// or  time_out[node]=Time;
}
bool isAnchestor(int anch,int node){
    return time_in[anch]&lt;=time_in[node] and time_out[anch]&gt;=time_out[node];
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin&gt;&gt;n;
    time_in.resize(n+1);
    time_out.resize(n+1);
    g.resize(n+1);
    vector&lt;int&gt; anchestors(n+1);
    for(int i=1; i&lt;=n; ++i){// I am thinking: the anchestors is sorted.
            cin&gt;&gt;anchestors[i];

            //
    }

    int start=1;


    dfs(start,1);


    int q,u,v;
    cin&gt;&gt;q;
    while(q--){
        cin&gt;&gt;u&gt;&gt;v;
        cout&lt;&lt;isAnchestor(u,v)&lt;&lt;&#39;\n&#39;;// The anchestor of v is u. //(u,v)--&gt;(a,b)
    }
    return 0;
}

答案1

得分: 3

在你的代码中,你有一个用于读取每个顶点的祖先值的for循环。你可以使用这个循环来构建邻接矩阵。对于每个顶点 i,如果 anchestors[i] 不等于 0(表示 i 不是根),你可以在 i 和其祖先 anchestors[i] 之间添加一条边。以下是如何修改你的for循环以构建邻接矩阵的示例:

for(int i = 1; i <= n; ++i) {
    cin >> anchestors[i];
    if(anchestors[i] != 0) {
        g[i].push_back(anchestors[i]);
        g[anchestors[i]].push_back(i);
    }
}

这将在邻接矩阵 g 中为每个顶点和其祖先之间添加一条边。

英文:

In your code, you have a for loop that reads in the ancestor values for each vertex. You can use this loop to construct the adjacency matrix. For each vertex i, you can add an edge between i and its ancestor anchestors[i] if anchestors[i] is not 0 (indicating that i is not the root). Here’s an example of how you can modify your for loop to construct the adjacency matrix:

for(int i = 1; i &lt;= n; ++i) {
    cin &gt;&gt; anchestors[i];
    if(anchestors[i] != 0) {
        g[i].push_back(anchestors[i]);
        g[anchestors[i]].push_back(i);
    }
}

This will add an edge between each vertex and its ancestor in the adjacency matrix g.

huangapple
  • 本文由 发表于 2023年6月2日 07:42:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定