Graph Dictionary Representation Processing

Processing Graph Dictionary Representation

This solution processses a directed graph represented as dictionary strings and calculates graph metrics including vertex count, edge count, and total edge length.

Input Format

  • First line: number of input lines
  • Subsequent lines: dictionary strings representing vertices and their connected edges with lengths

Output Format

Single line output showing vertex count, edge count, and total edge length

Example Input

4
{'a':{'b':10,'c':6}}
{'b':{'c':2,'d':7}}
{'c':{'d':10}}
{'d':{}}

Example Output

4 5 35

Implementation

line_count = int(input())
vertices = line_count
edges = 0
total_length = 0

for _ in range(line_count):
    graph_data = eval(input())
    for connections in graph_data.values():
        if isinstance(connections, dict):
            edges += len(connections)
            for weight in connections.values():
                total_length += weight

print(f"{vertices} {edges} {total_length}")

String Reversal Operations

This program processes input strings and performs various reversal operations on the elements.

Input Format

Space-separated string elements (multiple spaces allowed)

Output Format

  • Reversed concatenated string
  • Original list
  • Reversed list elements with single space separation

Example Input

a b  c e   f  gh

Example Output

ghfecba
['a', 'b', 'c', 'e', 'f', 'gh']
gh f e c b a

Implementation

input_data = input().split()
filtered_items = [item for item in input_data if item]
reversed_items = filtered_items[::-1]

print(''.join(reversed_items))
print(filtered_items)
print(' '.join(reversed_items))

Class Student Information Analysis

This program processes student data from two classes and generates various statistical reports about competition participation.

Input Format

  • Line 1: Class A students (string without spaces)
  • Line 2: Class B students (space-separated)
  • Line 3: ACM competition participants (space-separated)
  • Line 4: English competition participants (space-separated)
  • Line 5: Transfer student name

Output Format

Multiple lines showing various student statistics and filtered lists

Example Input

abcdefghijab
1   2 3 4 5 6 7 8 9  10
1 2 3 a b c
1 5 10 a d e f
a

Example Output

Total: 20
Not in race: ['4', '6', '7', '8', '9', 'g', 'h', 'i', 'j'], num: 9
All racers: ['1', '10', '2', '3', '5', 'a', 'b', 'c', 'd', 'e', 'f'], num: 11
ACM + English: ['1', 'a'], num: 2
Only ACM: ['2', '3', 'b', 'c']
Only English: ['10', '5', 'd', 'e', 'f']
ACM Or English: ['10', '2', '3', '5', 'b', 'c', 'd', 'e', 'f']
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Implementation

class_a = set(input())
class_b = set(input().split())
acm_participants = set(input().split())
english_participants = set(input().split())
transfer_student = input()

all_students = class_a | class_b
competitors = acm_participants | english_participants
non_competitors = all_students - competitors
both_competitions = acm_participants & english_participants
acm_only = acm_participants - english_participants
english_only = english_participants - acm_participants
exclusive_participants = acm_participants ^ english_participants

print(f"Total: {len(all_students)}")
print(f"Not in race: {sorted(non_competitors)}, num: {len(non_competitors)}")
print(f"All racers: {sorted(competitors)}, num: {len(competitors)}")
print(f"ACM + English: {sorted(both_competitions)}, num: {len(both_competitions)}")
print(f"Only ACM: {sorted(acm_only)}")
print(f"Only English: {sorted(english_only)}")
print(f"ACM Or English: {sorted(exclusive_participants)}")

if transfer_student in class_a:
    class_a.remove(transfer_student)
    print(sorted(class_a))
elif transfer_student in class_b:
    class_b.remove(transfer_student)
    print(sorted(class_b))

Tags: graph-processing string-manipulation set-operations python-dictionary data-analysis

Posted on Mon, 28 Sep 2026 16:53:14 +0000 by lee2732