---
id: 2653
lang: en
translation_of: index.md
source_sha256: 5a7f7d96d6756578390ad8eadb86c7de13e6ce3b6efc9445a1b49cae83ba3044
title: "AtCoder Grand Contest 035 - Problem-Solving Report"
description: "Solutions to AGC 035 A to D: try every ordering, greedy over a DFS tree, an XOR chain construction, and a plain search."
tags: ["OI","DFS","AGC"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "强心接上去 read as a typo for 强行接上去 (forcibly attach) and rendered “forcibly attaching it”."
  - "一条半链 rendered as “a half chain” (one half-chain, contrasted with 整链 “the full chain”), not “one and a half chains”."
  - "判就完事了 (just check and it's over) rendered as “then checking, and you're done”, without a chip."
---

## A - XOR Circle

Consider trying every order of the only $s \leq 3$ elements and then checking, and you're done.

```cpp
// A.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;

int n, ai[MAX_N], si[MAX_N];
map<int, int> mp, tmp;

int main()
{
    scanf("%d", &n);
    for (int i = 1, x; i <= n; i++)
        scanf("%d", &ai[i]), mp[ai[i]]++;
    if (mp.size() <= 3)
    {
        vector<int> vi;
        for (auto x : mp)
            vi.push_back(x.first);
        bool flag = false;
        do
        {
            int len = vi.size();
            tmp.clear();
            for (int i = 1; i <= 2; i++)
                si[i] = vi[(i - 1) % len];
            for (int i = 3; i <= n; i++)
                si[i] = si[i - 1] ^ si[i - 2];
            for (int i = 1; i <= n; i++)
                tmp[si[i]]++;
            bool cflag = true;
            for (auto x : tmp)
                cflag &= mp[x.first] == x.second;
            flag |= cflag;
        } while (next_permutation(vi.begin(), vi.end()));
        puts(flag ? "Yes" : "No");
    }
    else
        puts("No");
    return 0;
}
```

<!--more-->

## B - Even Degrees

A <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard-trick</span> problem: first get a DFS tree, then greedily try to make the current subtree valid, because when the current subtree affects the parent of the subtree's root, there will also be an edge back to the parent that adjusts it. Of course, if it still doesn't work after adjusting all the way to the root, then there's definitely no solution.

```cpp
// B.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;

typedef pair<int, int> pii;

int n, m, head[MAX_N], current, parity[MAX_N];
bool inst[MAX_N], vis[MAX_N];
vector<pii> ansBox;

struct edge
{
    int to, nxt;
} edges[MAX_N << 1];

void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}

bool dfs(int u, int fa)
{
    vis[u] = inst[u] = true;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (!vis[edges[i].to] && edges[i].to != fa)
        {
            bool res = dfs(edges[i].to, u);
            parity[u] ^= !res;
            if (res)
                ansBox.push_back(make_pair(edges[i].to, u));
            else
                ansBox.push_back(make_pair(u, edges[i].to));
        }
    inst[u] = false;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (inst[edges[i].to] && edges[i].to != fa)
            if (parity[u])
                parity[u] ^= 1, ansBox.push_back(make_pair(u, edges[i].to));
            else
                parity[edges[i].to] ^= 1, ansBox.push_back(make_pair(edges[i].to, u));
    return parity[u];
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v; i <= m; i++)
        scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
    if (!dfs(1, 0))
        for (pii x : ansBox)
            printf("%d %d\n", x.first, x.second);
    else
        puts("-1");
    return 0;
}
```

## C - Skolem XOR Tree

First, refer to the figure below:

![Screenshot 2020-07-22 at 8.21.19 PM](media/截屏2020-07-22-下午8.21.19.png)

Consider the property $2i \oplus (2i + 1) = 1$, so we can just build it this way.

If $n$ is even, we can only get a half chain, so we consider directly enumerating the junction point on the full chain and forcibly attaching it.

```cpp
// C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;

int n;

int main()
{
    scanf("%d", &n);
    if (__builtin_popcount(n) == 1)
        puts("No"), exit(0);
    puts("Yes"), printf("1 2\n2 3\n3 %d\n%d %d\n%d %d\n", n + 1, n + 1, n + 2, n + 2, n + 3);
    for (int i = 4; i < n; i += 2)
        printf("1 %d\n%d %d\n1 %d\n%d %d\n", i, i, i + 1 + n, i + 1, i + 1, i + n);
    if (!(n & 1))
    {
        for (int i = 2; i <= n; i++)
        {
            int u = i ^ n ^ 1;
            if (i != 3 && u != 3 && u <= n)
            {
                printf("%d %d\n%d %d\n", i, n, u, 2 * n);
                break;
            }
        }
    }
    return 0;
}
```

## D - Add and Remove

Just search.

```cpp
// D.cpp
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MAX_N = 20, INF = 0x3f3f3f3f3f3f3f3f;

int n;
ll ai[MAX_N];

ll solve(int l, int r, ll cL, ll cR)
{
    if (l + 1 > r - 1)
        return 0;
    ll ret = INF;
    for (int i = l + 1; i <= r - 1; i++)
        ret = min(ret, solve(l, i, cL + cR, cR) + solve(i, r, cL, cL + cR) + 1LL * (cL + cR) * ai[i]);
    return ret;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &ai[i]);
    printf("%lld\n", solve(1, n, 1, 1) + ai[1] + ai[n]);
    return 0;
}
```
