📜  名人问题 |第 2 组

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

名人问题 |第 2 组

在 N 人的聚会中,每个人都只认识一个人。这样的人可能出现聚会上,如果是,(s)他不认识聚会中的任何人。我们只能问“ A知道B吗? “。在最少的问题中找到陌生人(名人)。
我们可以将问题输入描述为代表聚会中人员的数字/字符数组。我们还有一个假设函数HaveAcquaintance(A, B)如果 A 知道 B,则返回true ,否则返回false 。如何解决问题?

还给出了一个列表know[] ,其中know[i]{a, b}的形式表示,表示人 a知道人 b

例子:

方法:在该问题的Set-1中讨论了朴素的方法和一些优化的方法。

贪心方法:可以使用贪心方法来解决。上面的问题可以通过创建一个bool的向量来解决,遍历给定的矩阵如下:

  • 遍历矩阵一次,找到谁都不认识的人。
  • 将此存储为当前名人。如果当前名人不是人,则返回-1,否则进行下一步。
  • 再次遍历矩阵,现在检查聚会上的每个人是否都知道当前的名人。
  • 如果当前名人也符合此标准,则将其作为派对名人返回。
  • 否则返回-1。

下面是上述方法的实现

C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to find the celebrity
int findPartyCelebrity(int N,
                       vector >& know)
{
 
    vector celebrity(N + 1, false);
    for (int i = 0; i < know.size(); i++)
        celebrity[know[i][0]] = true;
 
    int party_celebrity = -1;
    for (int i = 1; i <= N; i++)
        if (celebrity[i] == false)
            party_celebrity = i;
 
    celebrity.assign(N + 1, false);
    for (int i = 0; i < know.size(); i++)
        if (know[i][1] == party_celebrity)
            celebrity[know[i][0]] = true;
 
    for (int i = 1; i <= N; i++)
        if (i != party_celebrity && celebrity[i] == false)
            party_celebrity = -1;
 
    return party_celebrity;
}
 
// Driver code
int main()
{
    int N = 4;
    vector > know = { { 1, 3 }, { 2, 3 }, { 4, 3 } };
    cout << findPartyCelebrity(N, know);
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
 
class GFG {
 
  // Function to find the celebrity
  static int findPartyCelebrity(int N, int[][] know)
  {
 
    boolean[] celebrity = new boolean[N + 1];
    for (int i = 0; i < know.length; i++)
      celebrity[know[i][0]] = true;
 
    int party_celebrity = -1;
    for (int i = 1; i <= N; i++)
      if (celebrity[i] == false)
        party_celebrity = i;
 
    celebrity = new boolean[N + 1];
    for (int i = 0; i < know.length; i++)
      if (know[i][1] == party_celebrity)
        celebrity[know[i][0]] = true;
 
    for (int i = 1; i <= N; i++)
      if (i != party_celebrity
          && celebrity[i] == false)
        party_celebrity = -1;
 
    return party_celebrity;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 4;
    int[][] know = { { 1, 3 }, { 2, 3 }, { 4, 3 } };
    System.out.println(findPartyCelebrity(N, know));
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python code to implement above approach
 
# Function to find the celebrity
def findPartyCelebrity(N, know):
 
    celebrity = [False] * (N + 1)
    for i in range(len(know)):
        celebrity[know[i][0]] = True
 
    party_celebrity = -1
    for i in range(1, N + 1):
        if celebrity[i] == False:
            party_celebrity = i
 
    celebrity = [False] * (N + 1)
    for i in range(len(know)):
        if know[i][1] == party_celebrity:
            celebrity[know[i][0]] = True
 
    for i in range(1, N + 1):
        if i != party_celebrity and celebrity[i] == False:
            party_celebrity = -1
 
    return party_celebrity
 
# Driver code
N = 4
know = [[1, 3], [2, 3], [4, 3]]
print(findPartyCelebrity(N, know))
 
# This code is contributed by gfgking


C#
// C# code for the above approach
using System;
 
class GFG
{
 
  // Function to find the celebrity
  static int findPartyCelebrity(int N, int[,] know)
  {
 
    bool[] celebrity = new bool[N + 1];
    for (int i = 0; i < know.GetLength(0); i++)
      celebrity[know[i, 0]] = true;
 
    int party_celebrity = -1;
    for (int i = 1; i <= N; i++)
      if (celebrity[i] == false)
        party_celebrity = i;
     
    for(int i = 0; i < N + 1; i++){
        celebrity[i] = false;
    }
     
    for (int i = 0; i < know.GetLength(0); i++)
      if (know[i, 1] == party_celebrity)
        celebrity[know[i, 0]] = true;
 
    for (int i = 1; i <= N; i++)
      if (i != party_celebrity
          && celebrity[i] == false)
        party_celebrity = -1;
 
    return party_celebrity;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 4;
    int[,] know = { { 1, 3 }, { 2, 3 }, { 4, 3 } };
        Console.Write(findPartyCelebrity(N, know));
  }
}
 
// This code is contributed by Samim Hossain Mondal


Javascript



输出:
3

时间复杂度: O(N)
辅助空间: O(N)