给定两个数组u和v ,表示一个图,使得从u[i]到v[i]有一条无向边(0 ≤ v[i],u[i] < N)并且每个节点都有一些值val[ i] (0 ≤ i < N)。对于每个节点,如果直接连接到它的节点按照它们的值降序排列(如果值相等,则按照它们的索引升序排列),打印第k个位置的节点编号。如果总节点数< k则打印-1 。
例子:
Input: u[] = {0, 0, 1}, v[] = {2, 1, 2}, val[] = {2, 4, 3}, k = 2
Output:
2
0
0
For 0 node, the nodes directly connected to it are 1 and 2
having values 4 and 3 respectively, thus node with 2nd largest value is 2.
For 1 node, the nodes directly connected to it are 0 and 2
having values 2 and 3 respectively, thus node with 2nd largest value is 0.
For 2 node, the nodes directly connected to it are 0 and 1
having values 2 and 4 respectively, thus node with 2nd largest value is 0.
Input: u[] = {0, 2}, v[] = {2, 1}, val[] = {2, 4, 3}, k = 2
Output:
-1
-1
0
方法:这个想法是将直接连接到节点的节点连同它们的值一起存储在一个向量中,并按升序对它们进行排序,对于一个有n个节点直接连接到它的节点的第 k 个最大值将是( n – k)从最后一个节点。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print Kth node for each node
void findKthNode(int u[], int v[], int n, int val[], int V, int k)
{
// Vector to store nodes directly
// connected to ith node along with
// their values
vector > g[V];
// Add edges to the vector along with
// the values of the node
for (int i = 0; i < n; i++) {
g[u[i]].push_back(make_pair(val[v[i]], v[i]));
g[v[i]].push_back(make_pair(val[u[i]], u[i]));
}
// Sort neighbors of every node
// and find the Kth node
for (int i = 0; i < V; i++) {
if (g[i].size() > 0)
sort(g[i].begin(), g[i].end());
// Get the kth node
if (k <= g[i].size())
printf("%d\n", g[i][g[i].size() - k].second);
// If total nodes are < k
else
printf("-1\n");
}
return;
}
// Driver code
int main()
{
int V = 3;
int val[] = { 2, 4, 3 };
int u[] = { 0, 0, 1 };
int v[] = { 2, 1, 2 };
int n = sizeof(u) / sizeof(int);
int k = 2;
findKthNode(u, v, n, val, V, k);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// pair class
static class pair
{
int first,second;
pair(int a,int b)
{
first = a;
second = b;
}
}
// Function to print Kth node for each node
static void findKthNode(int u[], int v[], int n,
int val[], int V, int k)
{
// Vector to store nodes directly
// connected to ith node along with
// their values
Vector> g = new Vector>();
for(int i = 0; i < V; i++)
g.add(new Vector());
// Add edges to the Vector along with
// the values of the node
for (int i = 0; i < n; i++)
{
g.get(u[i]).add(new pair(val[v[i]], v[i]));
g.get(v[i]).add(new pair(val[u[i]], u[i]));
}
// Sort neighbors of every node
// and find the Kth node
for (int i = 0; i < V; i++)
{
if (g.get(i).size() > 0)
Collections.sort(g.get(i),new Comparator()
{
public int compare(pair p1, pair p2)
{
return p1.first - p2.first;
}
});
// Get the kth node
if (k <= g.get(i).size())
System.out.printf("%d\n", g.get(i).get(g.get(i).size() - k).second);
// If total nodes are < k
else
System.out.printf("-1\n");
}
return;
}
// Driver code
public static void main(String args[])
{
int V = 3;
int val[] = { 2, 4, 3 };
int u[] = { 0, 0, 1 };
int v[] = { 2, 1, 2 };
int n = u.length;
int k = 2;
findKthNode(u, v, n, val, V, k);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
# Function to print Kth node for each node
def findKthNode(u, v, n, val, V, k):
# Vector to store nodes directly connected
# to ith node along with their values
g = [[] for i in range(V)]
# Add edges to the vector along
# with the values of the node
for i in range(0, n):
g[u[i]].append((val[v[i]], v[i]))
g[v[i]].append((val[u[i]], u[i]))
# Sort neighbors of every node
# and find the Kth node
for i in range(0, V):
if len(g[i]) > 0:
g[i].sort()
# Get the kth node
if k <= len(g[i]):
print(g[i][-k][1])
# If total nodes are < k
else:
print("-1")
return
# Driver code
if __name__ == "__main__":
V = 3
val = [2, 4, 3]
u = [0, 0, 1]
v = [2, 1, 2]
n, k = len(u), 2
findKthNode(u, v, n, val, V, k)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// pair class
class pair
{
public int first,second;
public pair(int a,int b)
{
first = a;
second = b;
}
}
class sortHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
pair first = (pair)a;
pair second = (pair)b;
return first.first - second.first;
}
}
// Function to print Kth node for each node
static void findKthNode(int []u, int []v, int n,
int []val, int V, int k)
{
// Vector to store nodes directly
// connected to ith node along with
// their values
ArrayList g = new ArrayList();
for(int i = 0; i < V; i++)
g.Add(new ArrayList());
// Add edges to the Vector along with
// the values of the node
for (int i = 0; i < n; i++)
{
((ArrayList)g[u[i]]).Add(new pair(val[v[i]], v[i]));
((ArrayList)g[v[i]]).Add(new pair(val[u[i]], u[i]));
}
// Sort neighbors of every node
// and find the Kth node
for (int i = 0; i < V; i++)
{
if (((ArrayList)g[i]).Count > 0)
{
ArrayList tmp = (ArrayList)g[i];
tmp.Sort(new sortHelper());
g[i] = tmp;
}
// Get the kth node
if (k <= ((ArrayList)g[i]).Count)
Console.Write(((pair)((ArrayList)g[i])[((ArrayList)g[i]).Count - k]).second+"\n");
// If total nodes are < k
else
Console.Write("-1\n");
}
return;
}
// Driver code
public static void Main(string []args)
{
int V = 3;
int []val = { 2, 4, 3 };
int []u = { 0, 0, 1 };
int []v = { 2, 1, 2 };
int n = u.Length;
int k = 2;
findKthNode(u, v, n, val, V, k);
}
}
// This code is contributed by Pratham76
Javascript
2
0
0
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。