鉴于由N一个树节点值在范围[0,N),并在范围[0,N),由值Q的整数数组查询[]。每个查询的任务是删除值Q[i]的顶点并计算结果图中的连通分量。
例子:
Input: N = 7, Edges[][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 4}, {1, 5}, {3, 6}}, Queries[] = {0, 1, 6}
Output:
3 3 1
Explanation:
Query 1: Removal of the node valued 0 leads to removal of the edges (0, 1), (0, 2) and (0, 3). Therefore, the remaining graph has 3 connected components: [1, 4, 5], [2], [3, 6]
Query 2:Removal of the node valued 1 leads to removal of the edges (1, 4), (1, 5) and (1, 0). Therefore, remaining graph has 3 connected components: [4], [5], [2, 0, 3, 6]
Query 3:Removal of the node valued 6 leads to removal of the edges (3, 6). Therefore, the remaining graph has 1 connected component: [0, 1, 2, 3, 4, 5].
Input: N = 7, Edges[][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 4}, {1, 5}, {3, 6}}, Queries[] = {5, 3, 2}
Output:1 2 1
Explanation:
Query 1: Removal of the node valued 5 leads to removal of the edge (1, 5). Therefore, the remaining graph has 1 connected component: [0, 1, 2, 3, 4, 6]
Query 2:Removal of the node valued 3 leads to removal of the edges (0, 3), (3, 6). Therefore, the remaining graph has 2 connected components: [0, 1, 2, 4, 5], [6]
Query 3: Removal of the node valued 2 leads to removal of the edge (0, 2). Therefore, the remaining graph has 1 connected component: [0, 1, 3, 4, 5, 6]
方法:这个想法是观察在Tree 中,每当删除一个节点时,与该节点连接在一起的节点就会分离。因此,连接组件的数量变得等于删除节点的程度。
因此,该方法是预先计算并存储每个节点的度数在一个数组中。对于每个查询,连接组件的计数只是查询中相应节点的度数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define MAX 100005
// Stores degree of the nodes
int degree[MAX];
// Function that finds the degree of
// each node in the given graph
void DegreeOfNodes(int Edges[][2],
int N)
{
// Precompute degrees of each node
for (int i = 0; i < N - 1; i++) {
degree[Edges[i][0]]++;
degree[Edges[i][1]]++;
}
}
// Function to print the number of
// connected components
void findConnectedComponents(int x)
{
// Print the degree of node x
cout << degree[x] << ' ';
}
// Function that counts the connected
// components after removing a vertex
// for each query
void countCC(int N, int Q, int Queries[],
int Edges[][2])
{
// Count degree of each node
DegreeOfNodes(Edges, N);
// Interate over each query
for (int i = 0; i < Q; i++) {
// Find connected components
// after removing given vertex
findConnectedComponents(Queries[i]);
}
}
// Driver Code
int main()
{
// Given N nodes and Q queries
int N = 7, Q = 3;
// Given array of queries
int Queries[] = { 0, 1, 6 };
// Given Edges
int Edges[][2] = { { 0, 1 }, { 0, 2 },
{ 0, 3 }, { 1, 4 },
{ 1, 5 }, { 3, 6 } };
// Function Call
countCC(N, Q, Queries, Edges);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
static final int MAX = 100005;
// Stores degree of the nodes
static int []degree = new int[MAX];
// Function that finds the degree of
// each node in the given graph
static void DegreeOfNodes(int [][]Edges,
int N)
{
// Precompute degrees of each node
for (int i = 0; i < N - 1; i++)
{
degree[Edges[i][0]]++;
degree[Edges[i][1]]++;
}
}
// Function to print the number of
// connected components
static void findConnectedComponents(int x)
{
// Print the degree of node x
System.out.print(degree[x] + " ");
}
// Function that counts the connected
// components after removing a vertex
// for each query
static void countCC(int N, int Q,
int Queries[],
int [][]Edges)
{
// Count degree of each node
DegreeOfNodes(Edges, N);
// Interate over each query
for (int i = 0; i < Q; i++)
{
// Find connected components
// after removing given vertex
findConnectedComponents(Queries[i]);
}
}
// Driver Code
public static void main(String[] args)
{
// Given N nodes and Q queries
int N = 7, Q = 3;
// Given array of queries
int Queries[] = {0, 1, 6};
// Given Edges
int [][]Edges = {{0, 1}, {0, 2},
{0, 3}, {1, 4},
{1, 5}, {3, 6}};
// Function Call
countCC(N, Q, Queries, Edges);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
MAX = 100005
# Stores degree of the nodes
degree = [0] * MAX
# Function that finds the degree of
# each node in the given graph
def DegreeOfNodes(Edges, N):
# Precompute degrees of each node
for i in range(N - 1):
degree[Edges[i][0]] += 1
degree[Edges[i][1]] += 1
# Function to prthe number of
# connected components
def findConnectedComponents(x):
# Print the degree of node x
print(degree[x], end = " ")
# Function that counts the connected
# components after removing a vertex
# for each query
def countCC(N, Q, Queries, Edges):
# Count degree of each node
DegreeOfNodes(Edges, N)
# Interate over each query
for i in range(Q):
# Find connected components
# after removing given vertex
findConnectedComponents(Queries[i])
# Driver Code
if __name__ == '__main__':
# Given N nodes and Q queries
N = 7
Q = 3
# Given array of queries
Queries = [ 0, 1, 6 ]
# Given Edges
Edges = [ [ 0, 1 ], [ 0, 2 ],
[ 0, 3 ], [ 1, 4 ],
[ 1, 5 ], [ 3, 6 ] ]
# Function call
countCC(N, Q, Queries, Edges)
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
class GFG{
static readonly int MAX = 100005;
// Stores degree of the nodes
static int []degree = new int[MAX];
// Function that finds the degree of
// each node in the given graph
static void DegreeOfNodes(int [,]Edges,
int N)
{
// Precompute degrees of each node
for (int i = 0; i < N - 1; i++)
{
degree[Edges[i, 0]]++;
degree[Edges[i, 1]]++;
}
}
// Function to print the number of
// connected components
static void findConnectedComponents(int x)
{
// Print the degree of node x
Console.Write(degree[x] + " ");
}
// Function that counts the connected
// components after removing a vertex
// for each query
static void countCC(int N, int Q,
int []Queries,
int [,]Edges)
{
// Count degree of each node
DegreeOfNodes(Edges, N);
// Interate over each query
for (int i = 0; i < Q; i++)
{
// Find connected components
// after removing given vertex
findConnectedComponents(Queries[i]);
}
}
// Driver Code
public static void Main(String[] args)
{
// Given N nodes and Q queries
int N = 7, Q = 3;
// Given array of queries
int []Queries = {0, 1, 6};
// Given Edges
int [,]Edges = {{0, 1}, {0, 2},
{0, 3}, {1, 4},
{1, 5}, {3, 6}};
// Function Call
countCC(N, Q, Queries, Edges);
}
}
// This code is contributed by shikhasingrajput
Javascript
3 3 1
时间复杂度: O(E + Q),其中 E 是边的数量(E = N – 1),Q 是查询的数量。
辅助空间: O(V),其中 V 是顶点数。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。