CF1494E.A-Z Graph

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a directed graph consisting of nn vertices. Each directed edge (or arc) labeled with a single character. Initially, the graph is empty.

You should process mm queries with it. Each query is one of three types:

  • "++ uu vv cc" — add arc from uu to vv with label cc. It's guaranteed that there is no arc (u,v)(u, v) in the graph at this moment;
  • "−- uu vv" — erase arc from uu to vv. It's guaranteed that the graph contains arc (u,v)(u, v) at this moment;
  • "?? kk" — find the sequence of kk vertices v1,v2,…,vkv_1, v_2, \dots, v_k such that there exist both routes v1→v2→⋯→vkv_1 \to v_2 \to \dots \to v_k and vk→vk−1→⋯→v1v_k \to v_{k - 1} \to \dots \to v_1 and if you write down characters along both routes you'll get the same string. You can visit the same vertices any number of times.

给你一个包含 nn 个顶点的有向图。每条有向边(或称弧)上标有一个单字符。初始时,该图为空。

你需要处理 mm 个查询。每个查询为以下三种类型之一:

  • “++ uu vv cc” —— 添加一条从 uu 到 vv 的弧,其标签为 cc。保证此时图中尚不存在弧 (u,v)(u, v);
  • “−- uu vv” —— 删除从 uu 到 vv 的弧。保证此时图中存在弧 (u,v)(u, v);
  • “?? kk” —— 找出一个长度为 kk 的顶点序列 v1,v2,…,vkv_1, v_2, \dots, v_k,使得既存在路径 v1→v2→⋯→vkv_1 \to v_2 \to \dots \to v_k,也存在路径 vk→vk−1→⋯→v1v_k \to v_{k - 1} \to \dots \to v_1,且沿这两条路径依次读取边上的字符所构成的字符串完全相同。允许重复访问同一顶点任意多次。

输入格式

The first line contains two integers nn and mm (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5; 1≤m≤2⋅1051 \le m \le 2 \cdot 10^5) — the number of vertices in the graph and the number of queries.

The next mm lines contain queries — one per line. Each query is one of three types:

  • "++ uu vv cc" (1≤u,v≤n1 \le u, v \le n; u≠vu \neq v; cc is a lowercase Latin letter);
  • "−- uu vv" (1≤u,v≤n1 \le u, v \le n; u≠vu \neq v);
  • "?? kk" (2≤k≤1052 \le k \le 10^5).

It's guaranteed that you don't add multiple edges and erase only existing edges. Also, there is at least one query of the third type.

第一行包含两个整数 nn 和 mm(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5;1≤m≤2⋅1051 \le m \le 2 \cdot 10^5)—— 分别表示图中顶点的数量和查询的数量。

接下来的 mm 行每行包含一个查询,共三种类型:

  • “++ uu vv cc”(1≤u,v≤n1 \le u, v \le n;u≠vu \neq v;cc 为一个小写拉丁字母);
  • “−- uu vv”(1≤u,v≤n1 \le u, v \le n;u≠vu \neq v);
  • “?? kk”(2≤k≤1052 \le k \le 10^5)。

保证不会添加重复的边,且仅删除已存在的边。此外,至少存在一个第三类查询。

输出格式

For each query of the third type, print YES if there exist the sequence v1,v2,…,vkv_1, v_2, \dots, v_k described above, or NO otherwise.

对于每个第三种类型的查询,如果存在上述描述的序列 v1,v2,…,vkv_1, v_2, \dots, v_k,则输出 YES;否则输出 NO。

输入输出样例

  • 输入#1

    3 11
    + 1 2 a
    + 2 3 b
    + 3 2 a
    + 2 1 b
    ? 3
    ? 2
    - 2 1
    - 3 2
    + 2 1 c
    + 3 2 d
    ? 5

    输出#1

    YES
    NO
    YES

说明/提示

In the first query of the third type k=3k = 3, we can, for example, choose a sequence [1,2,3][1, 2, 3], since 1→a2→b31 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 and 3→a2→b13 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 1.

In the second query of the third type k=2k = 2, and we can't find sequence p1,p2p_1, p_2 such that arcs (p1,p2)(p_1, p_2) and (p2,p1)(p_2, p_1) have the same characters.

In the third query of the third type, we can, for example, choose a sequence [1,2,3,2,1][1, 2, 3, 2, 1], where 1→a2→b3→d2→c11 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 \xrightarrow{\text{d}} 2 \xrightarrow{\text{c}} 1.

在第三个类型的第一次查询中,k=3k = 3,例如我们可以选择序列 [1,2,3][1, 2, 3],因为 1→a2→b31 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 且 3→a2→b13 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 1。

在第三个类型的第二次查询中,k=2k = 2,我们无法找到序列 p1,p2p_1, p_2,使得弧 (p1,p2)(p_1, p_2) 和 (p2,p1)(p_2, p_1) 具有相同的字符。

在第三个类型的第三次查询中,例如我们可以选择序列 [1,2,3,2,1][1, 2, 3, 2, 1],其中 1→a2→b3→d2→c11 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 \xrightarrow{\text{d}} 2 \xrightarrow{\text{c}} 1。

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

首页