And BFS tree traversal --DFS

posted on 2019-08-28 16:45:21

A. tree traversal

Title Description

Given a \ (n-\) unrooted tree nodes (node number \ (0 \) to \ (. 1-n-\) ) and a node \ (X \) , please \ (X \) No. Node is the root, do a DFS with a BFS .

Input Format

Data is read from the standard input.

The first line enter a positive integer \ (n-\) ( \ (. 1 \ n-Leq \ Leq 200000 \) ), representative of the number of nodes of this tree.

$ N-1 $ next row (row number from $ 1 to $ $ n-1 $), the first \ (I \) line enter a positive integer a_i $ \ ((\) 0 \ Leq a_i \ I $ Leq) , represents the \ (I \) attached there is an edge between the node and the second node $ $ a_i.

The last line of input X $ \ ((\) 0 \ Leq X <n-$), representative of the root node number.

Output Format

Output to standard output.

$ 2 $ output lines of $ $ n-number, the first row represents $ 1 $ DFS sequence, the second row represents $ 2 $ BFS order.

Note: If a node has multiple son, then the son should be in the order of decreasing number to traverse.

Sample input 1

7
0
1
0
0
1
4
1

Sample output

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

solution:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> to[200005];
int n,a,vis[200005],q[200005],start;
int cmp(int b,int c)
{
    return b>c;
}
void ad(int u,int v)
{
    to[u].push_back(v);
}
void dfs(int k)
{
    cout << k << ' ';
    for(int i=0;i<to[k].size();i++)
    {
        if(vis[to[k][i]]==0)
        {
            vis[to[k][i]]=1;
            dfs(to[k][i]);
        }
            
    }
}
void bfs(int k)
{
    int head=1,tail=1;
    q[tail]=k;
    tail++;
    while(head<tail)
    {
        int now=q[head];
        cout << now << " ";
        head++;
        for(int i=0;i<to[now].size();i++)
        {
            if(vis[to[now][i]]==0)
            {
                vis[to[now][i]]=1;
                q[tail]=to[now][i];
                tail++;
            }
        }
    }
}
int main()
{
    cin >> n;
    for(int i=1;i<=n-1;i++)
    {
        cin >> a;
        ad(a,i);
        ad(i,a);
    }
    cin >> start;
    for(int i=0;i<n;i++)
        sort(to[i].begin(),to[i].end(),cmp);
    /*for(int i=0;i<n;i++)
    {
        for(int j=0;j<to[i].size();j++)
        cout << to[i][j] << " ";
        cout << endl;
    }*/
   //对于有根树,将start换成根节点即可
    vis[start]=1;
    dfs(start);
    memset(vis,0,sizeof(vis));
    cout << endl;
    vis[start]=1;
    bfs(start);
return 0;
}

B. center and diameter of the tree

Title Description

Given a \ (n-\) unrooted tree nodes (node number \ (0 \) to \ (1-n-\) ), all side lengths are \ (1 \) diameter, determined the tree length.

Definition tree of the center of the tree node with the smallest maximum value of distances from all nodes (the center of a tree may be more than one), the output of the center of the tree.

Input Format

Data is read from the standard input.

The first line enter a positive integer \ (n-\) ( \ (. 1 \ n-Leq \ Leq 200000 \) ), representative of the number of nodes of this tree.

Next \ (n-1 \) (line numbered \ (1 \) to \ (n-1 \) ), the first \ (I \) line enter a positive integer \ (a_i \) ( \ (0 \ a_i Leq <I \) ), represents the \ (I \) node and the second \ (a_i \) attached there is an edge between the nodes.

Output Format

Output to standard output.

Output \ (2 \) th, \ (1 \) line an integer, representative of the diameter of the tree; second \ (2 \) line outputs in accordance with a number of integers in ascending numerical order, node number representative of the center of the tree.

Sample input 1

6
0
1
0
0
1

Sample output

3
0 1 

solution:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> to[200005];
int n, a, vis[200005], q[200005], step[200005][2], head = 1, tail = 1, cen[3];
int cmp(int b, int c) { return b > c; }
void ad(int u, int v) { to[u].push_back(v); }
void bfs(int k) {
    head = 1;
    tail = 1;
    q[tail] = k;
    tail++;
    while (head < tail) {
        int now = q[head];
        head++;
        for (int i = 0; i < to[now].size(); i++) {
            if (vis[to[now][i]] == 0) {
                vis[to[now][i]] = 1;
                q[tail] = to[now][i];
                step[tail][0] = step[head - 1][0] + 1;
                step[tail][1] = head - 1;
                tail++;
            }
        }
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n - 1; i++) {
        cin >> a;
        ad(a, i);
        ad(i, a);
    }
    for (int i = 0; i < n; i++) sort(to[i].begin(), to[i].end(), cmp);
    vis[0] = 1;
    bfs(0);
    int point = q[head - 1];
    memset(vis, 0, sizeof(vis));
    memset(step, 0, sizeof(step));
    vis[point] = 1;
    bfs(point);
    int lenth = step[tail - 1][0];
    cout << lenth << endl;
    if (lenth % 2 == 0) {
        for (int i = tail - 1; i > 0; i = step[i][1]) {
            if (step[i][0] == lenth / 2) {
                cen[1] = q[i];
                cout << cen[1] << endl;
                return 0;
            }
        }
    } else {
        for (int i = tail - 1; i > 0; i = step[i][1]) {
            if (step[i][0] == (lenth + 1) / 2) {
                cen[1] = q[i];
            }
            if (step[i][0] == (lenth - 1) / 2) {
                cen[2] = q[i];
                if (cen[2] < cen[1])
                    swap(cen[2], cen[1]);
                cout << cen[1] << " " << cen[2] << endl;
                return 0;
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/xuanfly/p/11808544.html