D. Leha and another game about graph

Codeforces
IDCF841D
Time3000ms
Memory256MB
Difficulty
dfs and similargraphs
English · Original
Chinese · Translation
Formal · Original
Leha plays a computer game, where is on each level is given a connected graph with _n_ vertices and _m_ edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer _d__i_, which can be equal to 0, 1 or  - 1. To pass the level, he needs to find a «good» subset of edges of the graph or say, that it doesn't exist. Subset is called «good», if by by leaving only edges from this subset in the original graph, we obtain the following: for every vertex i, _d__i_ =  - 1 or it's degree modulo 2 is equal to _d__i_. Leha wants to pass the game as soon as possible and ask you to help him. In case of multiple correct answers, print any of them. ## Input The first line contains two integers _n_, _m_ (1 ≤ _n_ ≤ 3·105, _n_ - 1 ≤ _m_ ≤ 3·105) — number of vertices and edges. The second line contains _n_ integers _d_1, _d_2, ..., _d__n_ ( - 1 ≤ _d__i_ ≤ 1) — numbers on the vertices. Each of the next _m_ lines contains two integers _u_ and _v_ (1 ≤ _u_, _v_ ≤ _n_) — edges. It's guaranteed, that graph in the input is connected. ## Output Print  - 1 in a single line, if solution doesn't exist. Otherwise in the first line _k_ — number of edges in a subset. In the next _k_ lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1. [samples] ## Note In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.
Leha 玩一个电脑游戏,每关给出一个具有 #cf_span[n] 个顶点和 #cf_span[m] 条边的连通图。图中可以包含重边,但不能包含自环。每个顶点有一个整数 #cf_span[di],其值可以为 #cf_span[0]、#cf_span[1] 或 #cf_span[ - 1]。要通过该关卡,他需要找到图的一个“好”边子集,或判定其不存在。一个子集被称为“好”的,当仅保留该子集中的边时,满足:对于每个顶点 i,#cf_span[di] =  - 1 或其度数对 2 取模的结果等于 #cf_span[di]。Leha 希望尽快通过游戏,因此请你帮助他。若有多个正确答案,输出任意一个即可。 第一行包含两个整数 #cf_span[n], #cf_span[m] (#cf_span[1 ≤ n ≤ 3·105], #cf_span[n - 1 ≤ m ≤ 3·105]) —— 顶点数和边数。 第二行包含 #cf_span[n] 个整数 #cf_span[d1, d2, ..., dn] (#cf_span[ - 1 ≤ di ≤ 1]) —— 每个顶点上的数值。 接下来的 #cf_span[m] 行每行包含两个整数 #cf_span[u] 和 #cf_span[v] (#cf_span[1 ≤ u, v ≤ n]) —— 边。保证输入图是连通的。 如果无解,请在一行中输出 #cf_span[ - 1]。否则,第一行输出 #cf_span[k] —— 子集中边的数量;接下来的 #cf_span[k] 行输出这些边的编号。边按输入顺序编号,从 #cf_span[1] 开始。 在第一个样例中,我们有一个没有边的单顶点。其度数为 0,无法得到 1。 ## Input 第一行包含两个整数 #cf_span[n], #cf_span[m] (#cf_span[1 ≤ n ≤ 3·105], #cf_span[n - 1 ≤ m ≤ 3·105]) —— 顶点数和边数。第二行包含 #cf_span[n] 个整数 #cf_span[d1, d2, ..., dn] (#cf_span[ - 1 ≤ di ≤ 1]) —— 每个顶点上的数值。接下来的 #cf_span[m] 行每行包含两个整数 #cf_span[u] 和 #cf_span[v] (#cf_span[1 ≤ u, v ≤ n]) —— 边。保证输入图是连通的。 ## Output 如果无解,请在一行中输出 #cf_span[ - 1]。否则,第一行输出 #cf_span[k] —— 子集中边的数量;接下来的 #cf_span[k] 行输出这些边的编号。边按输入顺序编号,从 #cf_span[1] 开始。 [samples] ## Note 在第一个样例中,我们有一个没有边的单顶点。其度数为 0,无法得到 1。
**Definitions** Let $ n, m \in \mathbb{Z}^+ $ denote the number of vertices and edges, respectively. Let $ D = (d_1, d_2, \dots, d_n) \in \{-1, 0, 1\}^n $ be the target degree parity vector for vertices. Let $ G = (V, E) $ be an undirected graph with $ V = \{1, 2, \dots, n\} $ and $ E = \{e_1, e_2, \dots, e_m\} $, where each edge $ e_j = (u_j, v_j) $, $ u_j \ne v_j $, and no self-loops. Let $ x_j \in \{0, 1\} $ be a binary variable indicating whether edge $ e_j $ is selected ($ x_j = 1 $) or not ($ x_j = 0 $). **Constraints** 1. $ 1 \le n \le 3 \cdot 10^5 $, $ n - 1 \le m \le 3 \cdot 10^5 $ 2. For each vertex $ i \in \{1, \dots, n\} $: - If $ d_i \ne -1 $, then the degree of vertex $ i $ in the selected subgraph must satisfy: $$ \deg(i) \equiv d_i \pmod{2} $$ - If $ d_i = -1 $, no constraint is imposed on $ \deg(i) $. 3. The selected edge subset must form a subgraph of $ G $ (i.e., only edges from $ E $ may be chosen). **Objective** Find a vector $ x = (x_1, \dots, x_m) \in \{0,1\}^m $ such that for all $ i \in \{1, \dots, n\} $ with $ d_i \ne -1 $: $$ \sum_{j: e_j \text{ incident to } i} x_j \equiv d_i \pmod{2} $$ If such an $ x $ exists, output the indices $ j $ for which $ x_j = 1 $. Otherwise, output $-1$.
Samples
Input #1
1 0
1
Output #1
\-1
Input #2
4 5
0 0 0 -1
1 2
2 3
3 4
1 4
2 4
Output #2
0
Input #3
2 1
1 1
1 2
Output #3
1
1
Input #4
3 3
0 -1 1
1 2
2 3
1 3
Output #4
1
2
API Response (JSON)
{
  "problem": {
    "name": "D. Leha and another game about graph",
    "description": {
      "content": "Leha plays a computer game, where is on each level is given a connected graph with _n_ vertices and _m_ edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an inte",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF841D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Leha plays a computer game, where is on each level is given a connected graph with _n_ vertices and _m_ edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an inte...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Leha 玩一个电脑游戏,每关给出一个具有 #cf_span[n] 个顶点和 #cf_span[m] 条边的连通图。图中可以包含重边,但不能包含自环。每个顶点有一个整数 #cf_span[di],其值可以为 #cf_span[0]、#cf_span[1] 或 #cf_span[ - 1]。要通过该关卡,他需要找到图的一个“好”边子集,或判定其不存在。一个子集被称为“好”的,当仅保留该子集中的边时,...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ denote the number of vertices and edges, respectively.  \nLet $ D = (d_1, d_2, \\dots, d_n) \\in \\{-1, 0, 1\\}^n $ be the target degree parity vector for ve...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments