📌  相关文章
📜  图中每个顶点都有权重的第 k 个最重的相邻节点

📅  最后修改于: 2022-05-13 01:57:54.100000             🧑  作者: Mango

图中每个顶点都有权重的第 k 个最重的相邻节点

给定一个正数k和一个由N个节点组成的无向图,编号从 0 到 N-1,每个节点都有一个与其相关的权重。请注意,这与每条边都有权重的普通加权图不同。
对于每个节点,如果我们将直接连接到它的节点(根据它们的权重)按降序排序,那么第 k 个位置的节点的编号是多少。打印每个节点的第 k 个节点号(不是权重),如果不存在,则打印 -1。
例子:

Input : N = 3, k = 2, wt[] = { 2, 4, 3 }.
edge 1: 0 2
edge 2: 0 1
edge 3: 1 2

Output : 2 0 0
Graph:
         0 (weight 2)
        / \
       /   \
      1-----2
(weight 4)  (weight 3)
For node 0, sorted (decreasing order) nodes
according to their weights are node 1(weight 4),
node 2(weight 3). The node at 2nd position for
node 0 is node 2.
For node 1, sorted (decreasing order) nodes 
according to their weight are node 2(weight 3), 
node 0(weight 2). The node at 2nd position for 
node 1 is node 0.
For node 2, sorted (decreasing order) nodes 
according to their weight are node 1(weight 4),
node 0(weight 2). The node at 2nd position for
node 2 is node 0.

这个想法是根据相邻节点的权重对每个节点的邻接列表进行排序。
首先,为所有节点创建邻接列表。现在对于每个节点,直接连接到它的所有节点都存储在一个列表中。在邻接列表中,存储节点及其权重。
现在,对于每个节点,将与其直接相连的所有节点的权重进行倒序排序,然后打印每个节点列表中第 k 个位置的节点号。
下面是这种方法的实现:

C++
// C++ program to find Kth node weight after s
// sorting of nodes directly connected to a node.
#include
using namespace std;
 
// Print Kth node number for each node after sorting
// connected node according to their weight.
void printkthnode(vector< pair > adj[],
                        int wt[], int n, int k)
{
    // Sort Adjacency List of all node on the basis
    // of its weight.
    for (int i = 0; i < n; i++)
      sort(adj[i].begin(), adj[i].end());
 
    // Printing Kth node for each node.
    for (int i = 0; i < n; i++)
    {
        if (adj[i].size() >= k)
          cout << adj[i][adj[i].size() - k].second;
        else
          cout << "-1";
    }
}
 
// Driven Program
int main()
{
    int n = 3, k = 2;
    int wt[] = { 2, 4, 3 };
 
    // Making adjacency list, storing the nodes
    // along with their weight.
    vector< pair > adj[n+1];
 
    adj[0].push_back(make_pair(wt[2], 2));
    adj[2].push_back(make_pair(wt[0], 0));
 
    adj[0].push_back(make_pair(wt[1], 1));
    adj[1].push_back(make_pair(wt[0], 0));
 
    adj[1].push_back(make_pair(wt[2], 2));
    adj[2].push_back(make_pair(wt[1], 1));
 
    printkthnode(adj, wt, n, k);
    return 0;
}


Java
// Java program to find Kth node weight after s
// sorting of nodes directly connected to a node.
import java.util.*;
 
class GFG
{
    // pair class
    static class pair
    {
        int first, second;
 
        pair(int a, int b)
        {
            first = a;
            second = b;
        }
    }
 
    // Print Kth node number for each node after sorting
    // connected node according to their weight.
    static void printkthnode(Vector adj[], int wt[], int n, int k)
    {
        // Sort Adjacency List of all node on the basis
        // of its weight.
        for (int i = 0; i < n; i++)
            Collections.sort(adj[i], new Comparator()
            {
                public int compare(pair p1, pair p2)
                {
                    return p1.first - p2.first;
                }
            });
 
        // Printing Kth node for each node.
        for (int i = 0; i < n; i++)
        {
            if (adj[i].size() >= k)
                System.out.print(adj[i].get(adj[i].size() -
                                            k).second + " ");
            else
                System.out.print("-1");
        }
    }
 
    // Driven Program
    public static void main(String[] args)
    {
        int n = 3, k = 2;
        int wt[] = { 2, 4, 3 };
 
        // Making adjacency list, storing the nodes
        // along with their weight.
        Vector[] adj = new Vector[n + 1];
        for (int i = 0; i < n + 1; i++)
            adj[i] = new Vector();
 
        adj[0].add(new pair(wt[2], 2));
        adj[2].add(new pair(wt[0], 0));
 
        adj[0].add(new pair(wt[1], 1));
        adj[1].add(new pair(wt[0], 0));
 
        adj[1].add(new pair(wt[2], 2));
        adj[2].add(new pair(wt[1], 1));
 
        printkthnode(adj, wt, n, k);
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find Kth node
# weight after sorting of nodes
# directly connected to a node.
 
# Print Kth node number for each node
# after sorting connected node
# according to their weight.
def printkthnode(adj, wt, n, k):
     
    # Sort Adjacency List of all
    # node on the basis of its weight.
    for i in range(n):
        adj[i].sort()
 
    # Printing Kth node for each node.
    for i in range(n):
        if (len(adj[i]) >= k):
            print(adj[i][len(adj[i]) -
                             k][1], end = " ")
        else:
            print("-1", end = " ")
 
# Driver Code
if __name__ == '__main__':
 
    n = 3
    k = 2
    wt = [2, 4, 3]
 
    # Making adjacency list, storing
    # the nodes along with their weight.
    adj = [[] for i in range(n + 1)]
 
    adj[0].append([wt[2], 2])
    adj[2].append([wt[0], 0])
 
    adj[0].append([wt[1], 1])
    adj[1].append([wt[0], 0])
 
    adj[1].append([wt[2], 2])
    adj[2].append([wt[1], 1])
 
    printkthnode(adj, wt, n, k)
 
# This code is contributed by PranchalK


输出:

2 0 0