#P15530. [ROIR 2015 Day 2] transform 魔法传送门

[ROIR 2015 Day 2] transform 魔法传送门

Problem Description

In a kingdom, there are nn cities connected by magic portals.

For every pair of different cities, there is exactly one magic portal, which allows instant travel from one city to the other.

Due to the special nature of the magic portals, each portal can only be used in one direction. For each pair of cities AA and BB, it is known whether the portal can be used from AA to BB or from BB to AA.

Because of this, residents sometimes need to use multiple portals to travel from one city to another. Also, it is possible that some pairs of cities are not reachable from each other using any sequence of portals.

Residents call a city a “perfect city” if it is possible to reach all other cities in the kingdom from that city using only magic portals. Suppose initially there are kk perfect cities in the kingdom.

Recently, the king decided to choose one pair of cities and reverse the direction of the portal connecting them. To choose the best plan, the king wants to know how the number of perfect cities in the kingdom may change after modifying one portal.

The report should include, for each integer mm with m≥Lm \geq L, the number of ordered city pairs (A,B)(A, B) that satisfy:

  • In the original portal system, it is possible to travel directly from city AA to city BB.
  • If this portal direction is reversed so that it is possible to travel directly from city BB to city AA, then the number of perfect cities in the kingdom becomes mm.

Therefore, the partial report contains only those changes that strictly increase the number of perfect cities. The full report contains all outcomes obtained by reversing the direction of exactly one portal.

To obtain this information, the king plans to request a report from the Ministry of Transportation. The king can request a partial report or a full report. The content of the report depends on the parameter LL: for the partial report, L=k+1L = k + 1, and for the full report, L=1L = 1.

Task: Write a program that generates the required report based on the given portal directions.

Input Format

The first line of the input file contains two integers: nn — the number of cities in the kingdom (2≤n≤20002 \leq n \leq 2000), and pp, where p=0p = 0 means you need to output the partial report, and p=1p = 1 means you need to output the full report.

The next nn lines contain nn characters each. The jj-th character in the ii-th line describes the portal between city ii and city jj:

  • “+” means you can travel from city ii to city jj.
  • “-” means you can travel from city jj to city ii.
  • “.” means there is no portal (i.e. i=ji = j).

Output Format

The first line of the output file should contain one integer kk — the number of perfect cities in the kingdom.

If a partial report is required (p=0p = 0), then the second line should contain n−kn - k non-negative integers separated by spaces. The ii-th number represents how many portal reversals on a city pair (A,B)(A, B) make the number of perfect cities become k+ik + i. If k=nk = n, the second line may be empty.

If a full report is required (p=1p = 1), then the second line should contain nn non-negative integers separated by spaces. The ii-th number represents how many portal reversals on a city pair (A,B)(A, B) make the number of perfect cities become ii.

5 0
.-+++
+.+++
--.+-
---.+
--+-.
1
0 0 0 3
5 1
.-+++
+.+++
--.+-
---.+
--+-.

1
7 0 0 0 3

Hint

Example Explanation

In this example, initially only city 22 is perfect. By reversing the portal directions connecting city pair (2,3)(2, 3), (2,4)(2, 4), and (2,5)(2, 5), all cities become perfect. Reversing any other portal makes only one city perfect.

Grading System and Subtasks

Subtask 1 (20 points)

  • 2≤n≤502 \leq n \leq 50, p=0p = 0.

Subtask 2 (30 points)

  • 2≤n≤3002 \leq n \leq 300, p=0p = 0.

Subtask 3 (20 points)

  • 2≤n≤20002 \leq n \leq 2000, p=0p = 0.

Subtask 4 (30 points)

  • 2≤n≤20002 \leq n \leq 2000, p=1p = 1.

Translation source: GPT 5.2.

Translated by ChatGPT 5