给定一个包含N个节点的图。对于节点P 1 ,P 2 ,P 3 ,…,P N的任何置换,置换的值定义为在其左侧至少有1个具有边缘的节点的索引数。在所有排列中找到最大值。
例子:
Input: N = 3, edges[] = {{1, 2}, {2, 3}}
Output: 2
Consider the permutation 2 1 3
Node 1 has node 2 on the left of it and there is an edge connecting them in the graph.
Node 3 has node 2 on the left of it and there is an edge connecting them in the graph.
Input: N = 4, edges[] = {{1, 3}, {2, 4}}
Output: 2
Consider the permutation 1 2 3 4
Node 3 has node 1 on the left of it and there is an edge connecting them in the graph.
Node 4 has node 2 on the left of it and there is an edge connecting them in the graph.
方法:让我们从开始的任何节点开始,我们可以跟着其附近的任何节点进行跟踪,然后重复该过程。这类似于dfs遍历,在该遍历中,除第一个节点外,每个节点之前都有一个与之共享边的节点。因此,对于每个连接的组件,我们可以从该组件的节点排列中获得的最大值是组件的大小– 1 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the number of nodes
// in the current connected component
int dfs(int x, vector adj[], int vis[])
{
// Initialise size to 1
int sz = 1;
// Mark the node as visited
vis[x] = 1;
// Start a dfs for every unvisited
// adjacent node
for (auto ch : adj[x])
if (!vis[ch])
sz += dfs(ch, adj, vis);
// Return the number of nodes in
// the current connected component
return sz;
}
// Function to return the maximum value
// of the required permutation
int maxValue(int n, vector adj[])
{
int val = 0;
int vis[n + 1] = { 0 };
// For each connected component
// add the corresponding value
for (int i = 1; i <= n; i++)
if (!vis[i])
val += dfs(i, adj, vis) - 1;
return val;
}
// Driver code
int main()
{
int n = 3;
vector adj[n + 1] = { { 1, 2 }, { 2, 3 } };
cout << maxValue(n, adj);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int vis[];
// Function to return the number of nodes
// in the current connected component
static int dfs(int x, Vector> adj)
{
// Initialise size to 1
int sz = 1;
// Mark the node as visited
vis[x] = 1;
// Start a dfs for every unvisited
// adjacent node
for (int i = 0; i < adj.get(x).size(); i++)
if (vis[adj.get(x).get(i)] == 0)
sz += dfs(adj.get(x).get(i), adj);
// Return the number of nodes in
// the current connected component
return sz;
}
// Function to return the maximum value
// of the required permutation
static int maxValue(int n, Vector> adj)
{
int val = 0;
vis = new int[n + 1];
for (int i = 0; i < n; i++)
vis[i] = 0;
// For each connected component
// add the corresponding value
for (int i = 0; i < n; i++)
if (vis[i] == 0)
val += dfs(i, adj) - 1;
return val;
}
// Driver code
public static void main(String args[])
{
int n = 3;
Vector> adj = new Vector>() ;
// create the graph
Vector v = new Vector();
v.add(0);
v.add(1);
Vector v1 = new Vector();
v1.add(1);
v1.add(2);
adj.add(v);
adj.add(v1);
adj.add(new Vector());
System.out.println( maxValue(n, adj));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
# Function to return the number of nodes
# in the current connected component
def dfs(x, adj, vis):
# Initialise size to 1
sz = 1
# Mark the node as visited
vis[x] = 1
# Start a dfs for every unvisited
# adjacent node
for ch in adj:
if (not vis[ch]):
sz += dfs(ch, adj, vis)
# Return the number of nodes in
# the current connected component
return sz
# Function to return the maximum value
# of the required permutation
def maxValue(n, adj):
val = 0
vis = [0] * (n + 1)
# For each connected component
# add the corresponding value
for i in range(1, n + 1):
if (not vis[i]):
val += dfs(i, adj, vis) - 1
return val
# Driver Code
if __name__ == '__main__':
n = 3
adj = [1, 2 , 2, 3]
print(maxValue(n, adj))
# This code is contributed by
# SHUBHAMSINGH10
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
static int []vis ;
// Function to return the number of nodes
// in the current connected component
static int dfs(int x, List> adj)
{
// Initialise size to 1
int sz = 1;
// Mark the node as visited
vis[x] = 1;
// Start a dfs for every unvisited
// adjacent node
for (int i = 0; i < adj[x].Count; i++)
if (vis[adj[x][i]] == 0)
sz += dfs(adj[x][i], adj);
// Return the number of nodes in
// the current connected component
return sz;
}
// Function to return the maximum value
// of the required permutation
static int maxValue(int n, List> adj)
{
int val = 0;
vis = new int[n + 1];
for (int i = 0; i < n; i++)
vis[i] = 0;
// For each connected component
// add the corresponding value
for (int i = 0; i < n; i++)
if (vis[i] == 0)
val += dfs(i, adj) - 1;
return val;
}
// Driver code
public static void Main(String []args)
{
int n = 3;
List> adj = new List>() ;
// create the graph
List v = new List();
v.Add(0);
v.Add(1);
List v1 = new List();
v1.Add(1);
v1.Add(2);
adj.Add(v);
adj.Add(v1);
adj.Add(new List());
Console.WriteLine( maxValue(n, adj));
}
}
// This code is contributed by Rajput-Ji
2
时间复杂度: O(N)