CF BUDDY
← Problems·

1494E · A-Z Graph

2400 · constructive algorithms, data structures, graphs

Problem: 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 v1v2vkv_1 \to v_2 \to \dots \to v_k and vkvk1v1v_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.

Input Format: The first line contains two integers nn and mm (2n21052 \le n \le 2 \cdot 10^5; 1m21051 \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" (1u,vn1 \le u, v \le n; uvu \neq v; cc is a lowercase Latin letter);
  • "- uu vv" (1u,vn1 \le u, v \le n; uvu \neq v);
  • "?? kk" (2k1052 \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.

Output Format: 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.

Note: 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 1a2b31 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 and 3a2b13 \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 1a2b3d2c11 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 \xrightarrow{\text{d}} 2 \xrightarrow{\text{c}} 1.

Sample Cases

Case 1

Input

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

Output

YES
NO
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback