EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2021
  3. Closed Cateogry

Mamak

https://www.hackerrank.com/contests/codenection-2021-closed-category/challenges/mamak

PreviousConcertNextFair Contest

Last updated 2 months ago

Question

MMU is setting up a new menu for N restaurants in the campus. Each restaurant can only serve one type of food and there are 4 different types of food available numbered as 1,2,3,4 (Mee Goreng, Nasi Goreng, Nasi Lemak, Roti). The MMU authority wants the students to have a varied diet. Each student has 2 favorite restaurants, the authority wants these two restaurants to have different menus so that the student can choose between at least two types of food. No restaurant is a favorite of more that 3 students.

Input Format

Input consists of two integers N and M. Next M lines contain two different integers a and b, the favorite restaurant of the ith student.

Constraints

2≤N≤1002 \le N \le 1002≤N≤100
1≤M≤1501 \le M \le 1501≤M≤150

Output Format

Output an N digit number, each digit in range (1..4) which describes the type of food to be included in the menu of ith restaurant. Print the smallest possible such number.

Sample Input 0

Sample Inputs:

Input

5 6
4 1
4 2
4 3
2 5
1 2
1 5

Output

12133

Solution — Graph Coloring

Small rant: this question is confusing AF, which had to passed to ChatGPT to understand the actual question.

Besides the poor description, we can still understand the actual requirement, which is to build a typical graph coloring problem.

from collections import defaultdict

def assign_menus(N, M, preferences):
    # Step 1: Create graph (adjacency list)
    graph = defaultdict(set)
    for a, b in preferences:
        graph[a].add(b)
        graph[b].add(a)
    
    # Step 2: Assign food types (1 to 4) using a greedy approach
    food_assignment = {}  # Stores restaurant -> food type mapping
    food_types = {1, 2, 3, 4}  # Available food types
    
    for restaurant in range(1, M + 1):
        # Get the food types used by neighbors (adjacent restaurants)
        used_foods = set()
        for neighbor in graph[restaurant]:  
            if neighbor in food_assignment:  
                used_foods.add(food_assignment[neighbor])

        # Assign the first available food type that is not used by neighbors
        for food in food_types:
            if food not in used_foods:
                food_assignment[restaurant] = food
                break
    
    # Step 3: Print the menu assignment
    for restaurant in range(1, M + 1):
        print(food_assignment[restaurant], end="")

# User Input
N, M = map(int, input().split())
preferences = []

for _ in range(N):
    a, b = map(int, input().split())
    preferences.append((a, b))

assign_menus(N, M, preferences)

Note that I purposedly added the comment for each segment so that I could still remember what I am doing after few years, lol.

🇲🇾