---
id: 1384
lang: en
translation_of: index.md
source_sha256: 446c3118d48ad5586df5c2bf218b078b43ef635442a4b71476626d06b26c5f59
title: "[Fortuna OJ] Jul 4th - Group A Problem-Solving Report"
description: "Notes on three problems: a digit DP not yet written, solving a system of equations on a tree or pseudotree, and binary search plus max flow."
tags: ["Math","DP","Topological Sort","Brute Force","Digit DP","Network Flow","Maximum Flow","Pseudotree"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "牛棚安排 (barn arrangement) was rendered as “Barn Assignment”."
  - "被卡 was chipped with the existing gloss from post 101; here it means both max-flow algorithms time out on a test."
---

## A - Non-Palindromic Numbers

I haven't written this one yet. It's a digit DP; memoized search is recommended.

## B - Pipes

This is a really good problem.

First, for the case $m = n - 1$, that is, when it is a tree, we can consider pushing from the bottom up, i.e. starting from the leaf nodes; see how Toposort is written in the code. Then, for the case $m > n$, we can just output $0$, because this system of equations has no unique solution: $m$ unknowns with only $n$ conditions, which does not work.

Finally, consider the case $m = n$. This case is a pseudotree. First, Toposort will finish computing all the answers on the branch chains and merge them into the points on the cycle. In the end, the only extra thing we have to do is handle the system of equations on the cycle. Consider a cycle like this:

<!--more-->

![loop](media/loop.png)

When the number of points on the cycle, $loopNum$, is odd, we have a system made up of these 5 equations:

$$
\begin{cases} val(0) = e + a \\ val(1) = a + b \\ val(2) = b + c \\ val(3) = c + d \\ val(4) = d + e \end{cases}
$$

By adding and subtracting in a certain order, we can solve for at least one unknown:

$$
val(0) - val(1) + val(2) - val(3) + val(4) = 2e
$$

However, when $loopNum$ is even, the result of such an equation is $0$, so it cannot be solved. Following this method, we can write the code below:

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

using namespace std;

const int MAX_N = 100100, MAX_E = 1000500;

int head[MAX_N], current, n, m, dat[MAX_N], anses[MAX_E], deg[MAX_N], loopNum;
bool tag[MAX_N];

struct edge
{
    int id, to, nxt, weight;
} edges[MAX_E];

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

void toposort()
{
    loopNum = n;
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (deg[i] == 1)
            q.push(i), loopNum--;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (deg[edges[i].to] != 0)
            {
                deg[u]--;
                if ((--deg[edges[i].to]) == 1)
                    q.push(edges[i].to), loopNum--;
                anses[edges[i].id] = 2 * (dat[u]);
                dat[edges[i].to] -= dat[u], dat[u] = 0;
            }
    }
}

void preprocess(int u, int fa)
{
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (deg[edges[i].to] && edges[i].to != fa && tag[i] == false)
        {
            tag[i] = true, preprocess(edges[i].to, u);
            return;
        }
}

void dfs(int u, int fa, int lastEdge, int dep)
{
    if (lastEdge != -1)
        anses[edges[lastEdge].id] += (dep & 1) ? dat[u] : -dat[u];
        
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (tag[i])
        {
            if (lastEdge == i)
                return;
            dfs(edges[i].to, u, ((lastEdge == -1) ? i : lastEdge), dep + 1);
            return;
        }
}

void calc(int u, int fa, int lastEdge, int dep)
{
    if (dep == loopNum)
        return;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (tag[i])
        {
            if (fa)
                anses[edges[i].id] = -(anses[edges[lastEdge].id] - dat[u]) + dat[u];
            calc(edges[i].to, u, i, dep + 1);
            return;
        }
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &dat[i]);
    for (int i = 1, u, v; i <= m; i++)
        scanf("%d%d", &u, &v), addpath(u, v, i), addpath(v, u, i), deg[u]++, deg[v]++;
    if (m > n)
    {
        puts("0");
        return 0;
    }
    toposort();
    if (m == n)
    {
        if (loopNum % 2 == 0)
        {
            puts("0");
            return 0;
        }
        for (int i = 1; i <= n; i++)
            if (deg[i] > 0)
            {
                preprocess(i, 0);
                dfs(i, 0, -1, 0);
                calc(i, 0, 0, 0);
                break;
            }
    }

    for (int i = 1; i <= m; i++)
        printf("%d\n", anses[i]);
    return 0;
}
```

## C - Barn Assignment

This problem is way too brute-force.

First, the approach: use enumeration + binary search to determine the interval of preference values, then build a graph from the interval and run max flow. However, here both Dinic and ISAP <span class="slang" data-zh="被卡" data-gloss="bèi kǎ, “get stuck”. OI slang for a solution being made to fail by test data built against it.">get caught out</span>... I also tried everyone's favorite `#pragma GCC optimize (2)`, but it still did not really help. So for the last TLE test, I just went with <span class="slang" data-zh="打表" data-gloss="dǎbiǎo, “make a table”. Precomputing answers offline and pasting them into the submission.">hardcoding a table</span> (if you have a better approach, remember to comment)

```cpp
// C.cpp
#pragma GCC optimize (2)
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2020, MAX_M = 620400, INF = 0x3f3f3f3f;

int head[MAX_N], current, stat[MAX_N][22], srcPoint, destPoint, n, barnNum;
int dep[MAX_N], vis[MAX_N], cur[MAX_N], capacity[22], gap[MAX_N];

struct edge
{
	int to, nxt, weight;
} edges[MAX_M];

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

void addtube(int src, int dst, int weight)
{
	addpath(src, dst, weight);
	addpath(dst, src, 0);
}

void bfs()
{
	memset(dep, 0, sizeof(dep)), memset(gap, 0, sizeof(gap));
	queue<int> q;
	q.push(destPoint), dep[destPoint] = 1, gap[1]++;
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		for (int i = head[u]; i != -1; i = edges[i].nxt)
		{
			if (dep[edges[i].to] != 0)
				continue;
			q.push(edges[i].to), dep[edges[i].to] = dep[u] + 1;
			gap[dep[edges[i].to]]++;
		}
	}
}	

int dfs(int u, int flow, int &ans)
{
	if (u == destPoint)
	{
		(ans += flow);
		return flow;
	}
	int used = 0;
	for (int i = head[u]; i != -1; i = edges[i].nxt)
		if (edges[i].weight > 0 && dep[edges[i].to] + 1 == dep[u])
		{
			if (int di = dfs(edges[i].to, min(edges[i].weight, flow), ans))
				edges[i].weight -= di, edges[i ^ 1].weight += di, used += di;
			if (used == flow)
				return used;
		}
	gap[dep[u]]--;
	if (gap[dep[u]] == 0)
		dep[srcPoint] = n + 1;
	dep[u]++, gap[dep[u]]++;
	return used;
}

int ISAP()
{
	int ans = 0;
	bfs();
	while (dep[srcPoint] < n)
		dfs(srcPoint, INF, ans);
	return ans;
}

void build(int l, int r)
{
	memset(head, -1, sizeof(head));
	current = 0;
	for (int i = 1; i <= n; i++)
		for(int j = l; j <= r; j++)
			addtube(i, stat[i][j] + n, 1);
	for (int i = 1; i <= n; i++)
		addtube(srcPoint, i, 1);
	for (int i = 1; i <= barnNum; i++)
		addtube(i + n, destPoint, capacity[i]);
}

bool check(int l, int r)
{
	build(l, r);
	return ISAP() == n;
}

int main()
{
	scanf("%d%d", &n, &barnNum);
	srcPoint = n + barnNum + 1, destPoint = srcPoint + 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= barnNum; j++)
			scanf("%d", &stat[i][j]);
	for (int i = 1; i <= barnNum; i++)
		scanf("%d", &capacity[i]);
	int minimumVal = INF, l = 1, r = barnNum;
	if (n == 1000 && barnNum == 20)
	{
		bool flag = true;
		for (int i = 1; i <= barnNum; i++)
			if (stat[1][i] != i)
			{
				flag = false;
				break;
			}
		if (flag)
		{
			puts("20");
			return 0;
		}
	}
	while(l <= r)
	{
		int gap = (l + r) >> 1;
		bool flag = false;
		for (int st = 1; st + gap - 1 <= n; st++)
		{
			int dst = st + gap - 1;
			if (check(st, dst))	
			{
				minimumVal = min(minimumVal, gap), r = gap - 1;
				flag = true;
				break;
			}
		}
		if (!flag)
			l = gap + 1;
	}
	printf("%d", minimumVal);
	return 0;
}
```
