Problem: New Year is getting near. So it's time to change handles on codeforces. Mishka wants to change his handle but in such a way that people would not forget who he is.
Approach: 1. Build a directed graph of handle changes
2. Track which handles are "targets" (received a new handle from someone)
3. Sources (original handles) = all handles that initiated changes - targets
4. Fo
Key considerations:
- Architectural Decisions & Reasoning
Problem Analysis:
- We need to track a sequence of handle changes where each change maps `old_handle → new_handle
- Build a directed graph of handle changes
- For each source, follow the chain to its final handle
Complexity:
- Time: O(n) for building graph + O(n) for traversal = O(n)
- Space: O(n) for stori
Let me implement this solution.
Codeforces Handle Change Feature
Looking at this problem, it appears to be about tracking handle changes so users can still identify people after they change their Codeforces handles during the New Year tradition.
Architectural Decisions & Reasoning
Problem Analysis:
- We need to track a sequence of handle changes where each change maps
old_handle → new_handle
- A person might change their handle multiple times, creating a chain
- We want to find all unique users by determining their original handle and their final handle
- Original handles are those that were never the target of a change (source nodes in a DAG)
Data Structure Choice:
- Hash Map (Dictionary): O(1) lookups for following handle change chains
- Set: O(1) membership checks for identifying source handles
Algorithm:
- Build a directed graph of handle changes
- Track which handles are "targets" (received a new handle from someone)
- Sources (original handles) = all handles that initiated changes - targets
- For each source, follow the chain to its final handle
Complexity:
- Time: O(n) for building graph + O(n) for traversal = O(n)
- Space: O(n) for storing the graph
Implementation
def solve():
import sys
input = sys.stdin.readline
n = int(input())
# Graph: old_handle -> new_handle
handle_graph = {}
# Set of all handles that received a new handle (were "old" at some point)
was_old = set()
# Set of handles that became someone's new handle
became_new = set()
for _ in range(n):
old_handle, new_handle = input().split()
handle_graph[old_handle] = new_handle
was_old.add(old_handle)
became_new.add(new_handle)
# Original handles: handles that were never the target of a change
# These are "source" nodes in our graph
original_handles = was_old - became_new
# For each original handle, find the final handle by following the chain
result = []
for original in original_handles:
current = original
# Follow the chain until we reach a handle with no outgoing edge
while current in handle_graph:
current = handle_graph[current]
final_handle = current
result.append((original, final_handle))
# Output
print(len(result))
for original, final in sorted(result):
print(original, final)
if __name__ == "__main__":
solve()
Example
Input:
5
mishka potato
tourist gena
potato keyboard
gena petr
keyboard mouse
Visualization:
mishka → potato → keyboard → mouse
tourist → gena → petr
Output:
2
keyboard mouse
mishka mouse
petr petr
tourist petr
Wait, let me fix that output - it should be:
Output:
2
mishka mouse
tourist petr
The algorithm correctly:
- Identifies
mishka and tourist as original handles (never targets)
- Follows chains:
mishka → potato → keyboard → mouse and tourist → gena → petr
- Outputs 2 unique users with their original→final handle mappings