ICPC 2009
ICPC 2009

H. The Ministers’ Major Mess

Input file: major.in The ministers of the remote country of Stanistan are having severe problems with their decision making. It all started a few weeks ago when a new process for deciding which bills to pass was introduced. This process works as follows. Du...

Updated May 21, 2026
Track ICPC
Year 2009
Statement Text + PDF
TeXC++Statement textStatement PDF

Problem Statement

Formatted from the contest statement text, with sample tests broken out into copyable blocks.

Input file major.in

The ministers of the remote country of Stanistan are having severe problems with their decision making. It all started a few weeks ago when a new process for deciding which bills to pass was introduced. This process works as follows. During each voting session, there are several bills to be voted on. Each minister expresses an opinion by voting either “yes” or “no” for some of these bills. Because of limitations in the design of the technical solution used to evaluate the actual voting, each minister may vote on only at most four distinct bills (though this does not tend to be a problem, as most ministers only care about a handful of issues). Then, given these votes, the bills that are accepted are chosen in such a way that each minister gets more than half of his or her opinions satisfied.

As the astute reader has no doubt already realized, this process can lead to various problems. For instance, what if there are several possible choices satisfying all the ministers, or even worse, what if it is impossible to satisfy all the ministers? And even if the ministers’ opinions lead to a unique choice, how is that choice found?

Your job is to write a program to help the ministers with some of these issues. Given the ministers’ votes, the program must find out whether all the ministers can be satisfied, and if so, determine the decision on those bills for which, given the constraints, there is only one possible choice.

Input

Input consists of multiple test cases. Each test case starts with integers B (1 ≤ B ≤ 100), which is the number of distinct bills to vote on, and M (1 ≤ M ≤ 500), which is the number of ministers. The next M lines give the votes of the ministers. Each such line starts with an integer 1 ≤ k ≤ 4, indicating the number of bills that the minister has voted on, followed by the k votes. Each vote is of the format <bill> <vote>, where <bill> is an integer between 1 and B identifying the bill that is voted on, and <vote> is either y or n, indicating that the minister’s opinion is “yes” or “no.” No minister votes on the same bill more than once. The last test case is followed by a line containing two zeros.

Output

For each test case, print the test case number (starting with 1) followed by the result of the process. If it is impossible to satisfy all ministers, the result should be impossible. Otherwise, the result should be a string of length B, where the ith character is y, n, or ?, depending on whether the decision on the ith bill should be “yes,” whether it should be “no,” or whether the given votes do not determine the decision on this bill.

Sample Tests

Sample
Sample Input
 5 2
 4 2 y 5 n 3        n 4 n
 4 4 y 3 y 5        n 2 y
 4 2
 4 1 y 2 y 3        y 4 y
 3 1 n 2 n 3        n
 0 0

This page intentionally left blank.
Sample Output
Case 1: ?y??n
Case 2: impossible

Editorial

The solution write-up is rendered from the LaTeX source, with equations kept live through MathJax.

Key Observations

  • Write the structural observations that make the problem tractable.

  • State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.

  • If the constraints matter, explain exactly which part of the solution they enable.

Algorithm

  1. Describe the data structures and the state maintained by the algorithm.

  2. Explain the processing order and why it is sufficient.

  3. Mention corner cases explicitly if they affect the implementation.

Correctness Proof

We prove that the algorithm returns the correct answer.

Lemma 1.

State the first key claim.

Proof.

Provide a concise proof.

Lemma 2.

State the next claim if needed.

Proof.

Provide a concise proof.

Theorem.

The algorithm outputs the correct answer for every valid input.

Proof.

Combine the lemmas and finish the argument.

Complexity Analysis

State the running time and memory usage in terms of the input size.

Implementation Notes

  • Mention any non-obvious implementation detail that is easy to get wrong.

  • Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.

Code

C++ solution used for this page.

C++

Clean code view with a raw-file link when you want the original source.

Raw file
#include <bits/stdc++.h>
using namespace std;

namespace {

void solve() {
    // Fill in the full solution logic for the problem here.
}

}  // namespace

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();
    return 0;
}

Source Files and Assets

Raw files are still available here when you want the original TeX, C++, or statement assets.

Show raw files