CF1499G.Graph Coloring

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a bipartite graph consisting of n1n_1 vertices in the first part, n2n_2 vertices in the second part, and mm edges, numbered from 11 to mm. You have to color each edge into one of two colors, red and blue. You have to minimize the following value: ∑v∈V∣r(v)−b(v)∣\sum \limits_{v \in V} |r(v) - b(v)|, where VV is the set of vertices of the graph, r(v)r(v) is the number of red edges incident to vv, and b(v)b(v) is the number of blue edges incident to vv.

Sounds classical and easy, right? Well, you have to process qq queries of the following format:

  • 11 v1v_1 v2v_2 — add a new edge connecting the vertex v1v_1 of the first part with the vertex v2v_2 of the second part. This edge gets a new index as follows: the first added edge gets the index m+1m + 1, the second — m+2m + 2, and so on. After adding the edge, you have to print the hash of the current optimal coloring (if there are multiple optimal colorings, print the hash of any of them). Actually, this hash won't be verified, you may print any number as the answer to this query, but you may be asked to produce the coloring having this hash;
  • 22 — print the optimal coloring of the graph with the same hash you printed while processing the previous query. The query of this type will only be asked after a query of type 11, and there will be at most 1010 queries of this type. If there are multiple optimal colorings corresponding to this hash, print any of them.

Note that if an edge was red or blue in some coloring, it may change its color in next colorings.

The hash of the coloring is calculated as follows: let RR be the set of indices of red edges, then the hash is (∑i∈R2i) mod 998244353(\sum \limits_{i \in R} 2^i) \bmod 998244353.

Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.

你被给定一个二分图,其中第一部分包含 n1n_1 个顶点,第二部分包含 n2n_2 个顶点,共有 mm 条边,编号从 11 到 mm。你需要将每条边染成两种颜色之一:红色或蓝色。目标是最小化如下值:

∑v∈V∣r(v)−b(v)∣,\sum \limits_{v \in V} |r(v) - b(v)|,

其中 VV 是图的顶点集,r(v)r(v) 表示与顶点 vv 关联的红色边的数量,b(v)b(v) 表示与顶点 vv 关联的蓝色边的数量。

听起来很经典、很简单,对吧?但事实上,你需要处理 qq 个如下格式的查询:

  • 1 v1 v2 — 添加一条新边,连接第一部分的顶点 v1v_1 和第二部分的顶点 v2v_2。该边获得一个新的编号:第一个添加的边编号为 m+1m + 1,第二个为 m+2m + 2,依此类推。添加该边后,你必须输出当前最优染色方案的哈希值(若存在多个最优染色方案,则输出其中任意一个的哈希值)。实际上,该哈希值不会被验证,你可对这一查询输出任意整数作为答案;但后续可能要求你构造出具有该哈希值的染色方案;
  • 2 — 输出当前图的一个最优染色方案,其哈希值需与上一次类型 1 查询中输出的哈希值相同。此类查询仅在上一次查询为类型 1 后出现,且最多出现 1010 次。若存在多个对应于该哈希值的最优染色方案,则输出其中任意一个即可。

注意:若某条边在某个染色方案中为红色(或蓝色),它在后续的染色方案中可能变为蓝色(或红色)。

染色方案的哈希值定义如下:设 RR 为所有红色边的编号构成的集合,则哈希值为

(∑i∈R2i) mod 998244353.\left(\sum \limits_{i \in R} 2^i\right) \bmod 998244353.

注意:你需要以在线模式求解本题。这意味着你不能一次性读入全部输入。你只能在输出上一查询的答案之后,才能读入下一个查询。在 C++ 中,请使用 fflush 函数;在 Java 中,请使用 BufferedWriter.flush 方法,在每次输出后刷新输出缓冲区。

输入格式

The first line contains three integers n1n_1, n2n_2 and mm (1≤n1,n2,m≤2⋅1051 \le n_1, n_2, m \le 2 \cdot 10^5).

Then mm lines follow, the ii-th of them contains two integers xix_i and yiy_i (1≤xi≤n11 \le x_i \le n_1; 1≤yi≤n21 \le y_i \le n_2) meaning that the ii-th edge connects the vertex xix_i from the first part and the vertex yiy_i from the second part.

The next line contains one integer qq (1≤q≤2⋅1051 \le q \le 2 \cdot 10^5) — the number of queries you have to process.

The next qq lines contain the queries in the format introduced in the statement.

Additional constraints on the input:

  • at any moment, the graph won't contain any multiple edges;
  • the queries of type 22 are only asked if the previous query had type 11;
  • there are at most 1010 queries of type 22.

第一行包含三个整数 n1n_1、n2n_2 和 mm(1≤n1,n2,m≤2⋅1051 \le n_1, n_2, m \le 2 \cdot 10^5)。

接下来 mm 行,其中第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi≤n11 \le x_i \le n_1;1≤yi≤n21 \le y_i \le n_2),表示第 ii 条边连接第一部分中的顶点 xix_i 和第二部分中的顶点 yiy_i。

下一行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)—— 表示你需要处理的查询数量。

接下来的 qq 行按题目描述中规定的格式给出查询。

输入的额外约束条件:

  • 在任意时刻,图中均不包含重边;
  • 类型为 22 的查询仅在前一个查询类型为 11 时才会出现;
  • 类型为 22 的查询至多有 1010 个。

输出格式

To answer a query of type 11, print one integer — the hash of the optimal coloring.

To answer a query of type 22, print one line. It should begin with the integer kk — the number of red edges. Then, kk distinct integer should follow — the indices of red edges in your coloring, in any order. Each index should correspond to an existing edge, and the hash of the coloring you produce should be equal to the hash you printed as the answer to the previous query.

If there are multiple answers to a query, you may print any of them.

对于类型为 11 的查询,输出一个整数——最优染色方案的哈希值。

对于类型为 22 的查询,输出一行。该行应以整数 kk 开头——即红色边的数量;随后是 kk 个互不相同的整数——即你所构造的染色方案中红色边的编号(顺序任意)。每个编号必须对应一条实际存在的边,且你所构造的染色方案的哈希值应等于你在上一个查询中输出的哈希值。

若某个查询存在多个合法答案,你可以输出其中任意一个。

输入输出样例

  • 输入#1

    3 4 2
    1 2
    3 4
    10
    1 1 3
    1 2 3
    2
    1 3 3
    2
    1 2 4
    2
    1 2 1
    1 1 1
    2

    输出#1

    8
    8
    1 3
    40
    2 3 5
    104
    3 5 6 3
    104
    360
    4 5 6 3 8

输入解题思路,AI测评打分。不知道怎么写?

首页