AtCoder ABC285 Contest Solutions and Analysis

Problem A: Parent Node Verification

Problem Statement:
Given a specific tree structure, you need to determine if node a is the parent of node b. Both nodes are numbered between 1 and 15.

Solution Approach:
The tree follows a simple binary structure where: - The left child of any node n has the value n * 2- The right child of any node n has the value n * 2 + 1

Thus, to verify if a is the parent of b, we simply need to check if b equals either a * 2 or a * 2 + 1.

#include <iostream>
using namespace std;

int main() {
    int parent, child;
    cin >> parent >> child;
    
    if (child == parent * 2 || child == parent * 2 + 1) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    
    return 0;
}

Problem B: Maximum Uncommon Prefix Length

Problem Statement:
Given a string S of length N, for each position i (1 ≤ i < N), find the maximum value j such that for all k (1 ≤ k ≤ j), the characters at positions k and k+i are different.

Solution Approach:
Instead of checking all possible combinations, we can iterate through each position i and find the maximum j where the characters differ. The solution involves checking each character position until we find a match.

#include <iostream>
#include <string>
using namespace std;

int main() {
    int length;
    string text;
    cin >> length >> text;
    
    // Adjust for 1-based indexing
    text = " " + text;
    
    for (int i = 1; i < length; i++) {
        int maxDiff = 0;
        for (int j = 1; j + i <= length; j++) {
            if (text[j] != text[j + i]) {
                maxDiff = j;
            } else {
                break;
            }
        }
        cout << maxDiff << endl;
    }
    
    return 0;
}

Problem C: Problem ID to Number Conversion

Problem Statement:
Convert a problem ID (following the pattern A, B, ..., Z, AA, AB, ..., BA, ..., AAA, ...) to its corresponding numerical position.

Solution Approach:
This problem is essentially a base-26 number conversion, where A=1, B=2, ..., Z=26. We can process the string from left to right, multiplying the accumulated value by 26 and adding the value of the current character.

#include <iostream>
#include <string>
using namespace std;
typedef long long ll;

int main() {
    string problemId;
    cin >> problemId;
    
    ll result = 0;
    for (char c : problemId) {
        result *= 26;
        result += c - 'A' + 1;
    }
    
    cout << result << endl;
    
    return 0;
}

Problem D: Username Change Sequence

Problem Statement:
Given N users who want to change their usernames, determine if there exists a sequence of changes such that no two users have the same username at any time. Only one username can be changed at a time.

Solution Approach:
This problem can be modeled as a directed graph where nodes rperesent usernames and edges represent the desired changes from one username to another. The key insight is that if the graph contains any cycles, it's impossible to find a valid sequence (output "No"). Otherwise, a valid sequence exists (output "Yes").

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;

const int MAX_NODES = 200005;

vector<int> graph[MAX_NODES];
int visited[MAX_NODES];

bool hasCycle(int node) {
    visited[node] = 0;  // Mark as currently visiting
    
    for (int neighbor : graph[node]) {
        if (visited[neighbor] == 0) {
            return true;  // Found a back edge, cycle exists
        } else if (visited[neighbor] == -1) {
            if (hasCycle(neighbor)) {
                return true;
            }
        }
    }
    
    visited[node] = 1;  // Mark as fully visited
    return false;
}

int main() {
    memset(visited, -1, sizeof(visited));
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int userCount;
    cin >> userCount;
    
    map<string, int> nameToId;
    vector<string> idToName;
    int nodeCount = 0;
    
    for (int i = 0; i < userCount; i++) {
        string currentName, newName;
        cin >> currentName >> newName;
        
        if (nameToId.find(currentName) == nameToId.end()) {
            nameToId[currentName] = nodeCount;
            idToName.push_back(currentName);
            nodeCount++;
        }
        
        if (nameToId.find(newName) == nameToId.end()) {
            nameToId[newName] = nodeCount;
            idToName.push_back(newName);
            nodeCount++;
        }
        
        graph[nameToId[currentName]].push_back(nameToId[newName]);
    }
    
    bool possible = true;
    for (int i = 0; i < nodeCount; i++) {
        if (visited[i] == -1) {
            if (hasCycle(i)) {
                possible = false;
                break;
            }
        }
    }
    
    cout << (possible ? "Yes" : "No") << endl;
    
    return 0;
}

Tags: AtCoder Competitive Programming algorithm graph theory String Manipulation

Posted on Wed, 21 Oct 2026 16:05:53 +0000 by Goofan