QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100 Communication

# 141. 8 染色

统计

这是一道通信题。

一场考试正在进行。

这次考试的内容非常简单——给定一张 $N$ 个点 $M$ 条边的无向图 $G=(V,E)$,第 $i$ 条边($0 \leq i < M$)的两端点为 $(U_i, V_i)$($0 \leq U_i, V_i < N$)。保证图中不含有重边与自环。考试的任务是给每个点赋一个 $0 \sim 7$ 中的颜色 $C_i$,满足任意一条边的两个端点的颜色不同。

Alice 通过她的神力很快得到了一组合法的染色方案通过了考试。

Bob 也想要得到这样一个方案,于是它决定偷偷与 Alice 进行交流。Alice 可以向 Bob 发送一个长为 $L$ 的 0/1 串 $X$,向 Bob 提示一些信息。随后,Bob 需要构造出任意一个合法的 8 - 染色方案。

实现细节

这是一道通信题,本题仅支持 C++ 语言。你需要提交两个程序。

Alice

你需要提交的第一个程序为 Alice,其描述 Alice 的策略。

你需要实现以下函数:

std::vector <int> Alice(int N, int M, std::vector <int> U, std::vector <int> V, std::vector <int> C);
  • N 表示图 $G$ 的点数 $N$。
  • M 表示图 $G$ 的边数 $M$。
  • U, V 均为长为 $M-1$ 的数组,描述图中的边。
  • C 为一个长为 $N$ 的数组 $C_0, C_1, \cdots, C_{N-1}$($0 \leq C_i < 8$),描述 Alice 所构造出的 8 - 染色中,第 $i$ 个点的颜色。
  • 该函数需要返回一个数组 $X$:
    • 设 $L = |X|$。
    • 你需要保证 $0 \leq L < 6 \times 10^5$。
    • 你需要保证对任意 $0 \leq i < L$,均有 $X_i \in \{0, 1\}$
  • 在每组测试数据中,该函数会被调用恰好一次。

Bob

你需要提交的第二个程序为 Bob,其描述 Bob 的策略。

你需要实现以下函数:

std::vector <int> Bob(int N, int M, std::vector <int> U, std::vector <int> V, std::vector <int> X);
  • N 表示图 $G$ 的点数 $N$。
  • M 表示图 $G$ 的边数 $M$。
  • U, V 均为长为 $M-1$ 的数组,描述图中的边。
  • X 为 Alice 所返回的数组 $X$。
  • 该函数需要返回一个数组 $C'$:
    • 你需要保证 $|C'| = N$。
    • 你需要保证对任意 $0 \leq i < N$,均有 $0 \leq C'_i < 8$。
    • 你需要保证对任意 $0 \leq i < M$,均有 $C'_{U_i} \ne C'_{V_i}$。
  • 在每组测试数据中,该函数会被调用恰好一次。

样例交互库

下发文件中含有样例交互库 grader.cpp,你可以通过以下命令编译得到可执行文件 answer

g++ grader.cpp Alice.cpp Bob.cpp -o answer -O2

可执行文件将从标准输入(stdin)中读取以下格式的输入数据:

  • 输入的第一行包含两个整数 $N, M$。
  • 接下来 $M$ 行,每行两个整数 $U_i, V_i$。
  • 接下来一行,包含 $N$ 个整数 $C_0, C_1, \cdots, C_{N-1}$。

若你的程序正常结束,则可执行文件将向标准输出中按照以下格式输出:

  • 输出一行,包含 $N$ 个整数 $C'_0, C'_1, \cdots, C'_{N-1}$。

请注意,在最终评测时,使用的交互库与该样例交互库有所不同。选手不应依赖该交互库的实现。

样例数据

样例输入

10 14
2 8
3 2
2 4
6 5
3 5
4 8
8 6
7 6
2 7
0 6
4 7
1 4
3 0
4 3
1 5 2 0 4 5 2 1 7 2

样例输出

0 0 0 1 2 0 1 3 3 0

子任务

对于所有数据,$1 \leq N \leq 2 \times 10^{5}, 0 \leq M \leq 5 \times 10^5$。

在每个测试点中,如果你超出了时间限制(2.0 秒),超出了空间限制(256 MiB),或发生了运行时错误,或最终构造的方案不合法,则你的得分为 $0$。

  • 请注意:时间限制指你的两个程序的运行时间的最大值不能超过 2.0 秒。空间限制指你的两个程序所使用的空间的最大值不能超过 256 MiB。
  • 在任何合法(可以取得分数)的交互过程中,交互库在两次调用时所使用的内存不会超过 12 MiB,时间不会超过 0.05 秒。因此,你实际可用的时间至少有 1.95 秒,空间至少有 244 MiB。

否则,你的得分取决于 $L = |X|$ 的值、你的程序所用的时间 $t$ (单位:秒)与你的程序所用的空间 $m$ (单位:MB):

  • 参数 $S$ 与 $L$ 的值有关:
    • 若 $L \leq 2.5 \times 10^5$,则 $S = 100$。
    • 若 $2.5 \times 10^5 < L \leq 6 \times 10^5$,则 $S = 2 + 95 \times \left(\frac{600\,000 - L}{350\,000}\right)^2$。
    • 否则, $S = 0$。
  • 参数 $K_1$ 与 $t$ 的值有关:
    • 若 $t \leq 0.5$,则 $K_1=1$。
    • 若 $0.5 < t \leq 2.0$,则 $K_2 = (1-\frac{1}{2}t) + 0.05$。
    • 否则,$t = 0$。
  • 参数 $K_2$ 与 $m$ 的值有关:
    • 若 $m \leq 32$,则 $K_2=1$。
    • 若 $32 < t \leq 256$,则 $K_2 = (1-\frac{1}{256}m)$。
    • 否则,$K_2 = 0$。
  • 最终你的得分为 $K_1 \times K_2 \times S$。

即,为了获得满分,你最多只能传递长为 $2.5 \times 10^5$ 的二进制串,使用的时间不能超过 $0.5$ 秒(含交互库所用时间),空间不能超过 $32$ MiB(含交互库所用空间,即你应当使用不超过 $20$ MiB 空间)。

This is a communication problem.

An exam is taking place.

The content of this exam is quite simple—given an undirected graph $ G = (V, E) $ with $ N $ vertices and $ M $ edges, where the $ i $-th edge ($ 0 \leq i < M $) connects the two vertices $ (U_i, V_i) $ ($ 0 \leq U_i, V_i < N $). It is guaranteed that the graph contains no multiple edges or self-loops. The task is to assign each vertex a color from the set {0, 1, 2, 3, 4, 5, 6, 7} such that the two endpoints of any edge have different colors.

Alice, using her magical powers, quickly finds a valid coloring solution and passes the exam.

Bob also wants to find such a solution, so he decides to secretly communicate with Alice. Alice can send Bob a binary string $ X $ of length $ L $ to give him some hints. Subsequently, Bob needs to construct any valid 8-coloring solution.

Implementation Details

This is a communication problem, and the problem only supports the C++ language. You need to submit two programs.

Alice

The first program you need to submit is Alice, which describes Alice's strategy.

You need to implement the following function:

std::vector <int> Alice(int N, int M, std::vector <int> U, std::vector <int> V, std::vector <int> C);
  • N represents the number of vertices $ N $ in the graph $ G $.
  • M represents the number of edges $ M $ in the graph $ G $.
  • U, V are arrays of length $ M-1 $ that describe the edges of the graph.
  • C is an array of length $ N $, where $ C_0, C_1, \cdots, C_{N-1} $ ($ 0 \leq C_i < 8 $) describes the colors assigned by Alice in the 8-coloring.
  • This function needs to return an array $ X $:
    • Let $ L = |X| $.
    • You need to ensure $ 0 \leq L < 6 \times 10^5 $.
    • You need to ensure that for any $ 0 \leq i < L $, $ X_i $ is either 0 or 1.
  • In each test case, this function will be called exactly once.

Bob

The second program you need to submit is Bob, which describes Bob's strategy.

You need to implement the following function:

std::vector <int> Bob(int N, int M, std::vector <int> U, std::vector <int> V, std::vector <int> X);
  • N represents the number of vertices $ N $ in the graph $ G $.
  • M represents the number of edges $ M $ in the graph $ G $.
  • U, V are arrays of length $ M-1 $ that describe the edges of the graph.
  • X is the array returned by Alice.
  • This function needs to return an array $ C' $:
    • You need to ensure that $ |C'| = N $.
    • You need to ensure that for any $ 0 \leq i < N $, $ 0 \leq C'_i < 8 $.
    • You need to ensure that for any $ 0 \leq i < M $, $ C'_{U_i} \neq C'_{V_i} $.
  • In each test case, this function will be called exactly once.

Sample Interaction Library

The provided files include a sample interaction library grader.cpp. You can compile the executable answer using the following command:

g++ grader.cpp Alice.cpp Bob.cpp -o answer -O2

The executable will read the following input format from standard input (stdin):

  • The first line of input contains two integers $ N $ and $ M $.
  • The next $ M $ lines each contain two integers $ U_i $ and $ V_i $.
  • The next line contains $ N $ integers $ C_0, C_1, \cdots, C_{N-1} $.

If your program runs correctly, the executable will output the following format:

  • One line containing $ N $ integers $ C'_0, C'_1, \cdots, C'_{N-1} $.

Please note that the interaction library used in the final evaluation will differ from this sample interaction library. Contestants should not rely on the specific implementation of this sample library.

Sample Data

Sample Input

10 14
2 8
3 2
2 4
6 5
3 5
4 8
8 6
7 6
2 7
0 6
4 7
1 4
3 0
4 3
1 5 2 0 4 5 2 1 7 2

Sample Output

0 0 0 1 2 0 1 3 3 0

Subtasks

For all data, $ 1 \leq N \leq 2 \times 10^5 $, $ 0 \leq M \leq 5 \times 10^5 $.

In each test point, if you exceed the time limit (2.0 seconds), the space limit (256 MiB), encounter a runtime error, or the final constructed solution is invalid, your score will be 0.

  • Please note: The time limit means that the maximum runtime of both of your programs combined cannot exceed 2.0 seconds. The space limit means that the maximum space used by both of your programs combined cannot exceed 256 MiB.
  • In any valid (scorable) interaction, the interaction library will use no more than 12 MiB of memory and no more than 0.05 seconds between two calls. Therefore, you actually have at least 1.95 seconds and at least 244 MiB of space available.

Otherwise, your score depends on the value of $ L = |X| $, the time $ t $ (in seconds) used by your program, and the memory $ m $ (in MB) used by your program:

  • The parameter $ S $ depends on the value of $ L $:
    • If $ L \leq 2.5 \times 10^5 $, then $ S = 100 $.
    • If $ 2.5 \times 10^5 < L \leq 6 \times 10^5 $, then $ S = 2 + 95 \times \left(\frac{600\,000 - L}{350\,000}\right)^2 $.
    • Otherwise, $ S = 0 $.
  • The parameter $ K_1 $ depends on the value of $ t $:
    • If $ t \leq 0.5 $, then $ K_1 = 1 $.
    • If $ 0.5 < t \leq 2.0 $, then $ K_1 = (1-\frac{1}{2}t) + 0.05 $.
    • Otherwise, $ K_1 = 0 $.
  • The parameter $ K_2 $ depends on the value of $ m $:
    • If $ m \leq 32 $, then $ K_2 = 1 $.
    • If $ 32 < m \leq 256 $, then $ K_2 = (1-\frac{1}{256}m) $.
    • Otherwise, $ K_2 = 0 $.
  • Finally, your score is $ K_1 \times K_2 \times S $.

In summary, to achieve a perfect score, the binary string you transmit should be no longer than $ 2.5 \times 10^5 $ bits, and the time used by your programs (including the interaction library) should not exceed 0.5 seconds, with memory usage not exceeding 32 MiB (including the interaction library, which means your programs should use no more than 20 MiB of space).