---
id: 2220
lang: en
translation_of: index.md
source_sha256: d9621023bb2b5122752227211d9b128ccb136e711dcf4c4da877ddd9dd07d777
title: "[USACO 2018.12 Platinum] Problem-Solving Report"
description: "Solutions to USACO 2018 December Platinum: The Cow Gathering by peeling leaves, and Sort It Out via the k-th largest longest increasing subsequence."
tags: ["OI","Graph Theory","DP","Fenwick Tree","Lexicographic Order","USACO"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "字典需调整 in the last paragraph of B is read as a typo for 字典序调整 and rendered “adjust the lexicographic order”."
  - "仙 (short for 神仙) is chipped on its own with a new gloss, since the glossary only has 神仙."
---

## A - The Cow Gathering

First, if there were no constraints, pulling nodes out starting from the leaves would actually always give a solution; you can think of an out-tree rooted at the final exit to understand this logic. Now there are $m$ directed edges. A first thought is to try each node as the last node and then check whether the new graph has a cycle, which is $\Theta(n^2)$.

<!--more-->

With $u$ as the root, we find that for a constraint edge $a \to b$, the subtree of $a$ is definitely invalid. Think about it carefully: if we have one valid node, then we can actually find all the invalid nodes using this property alone. <span class="slang" data-zh="感性理解" data-gloss="gǎnxìng lǐjiě, “perceptual understanding”. OI slang for a hand-wavy, intuitive argument instead of a rigorous proof.">Hand-wavy intuition</span>: suppose everything has been covered; we find that our valid, feasible nodes can definitely form a unique subgraph, unaffected by the root.

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

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, phead[MAX_N], current, mhead[MAX_N], deg[MAX_N];
bool vis[MAX_N], ans[MAX_N];

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

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

void clearExit()
{
    for (int i = 1; i <= n; i++)
        puts("0");
    exit(0);
}

void dfs(int u, int fa)
{
    ans[u] = true;
    for (int i = phead[u]; i != -1; i = edges[i].nxt)
        if (!vis[edges[i].to] && edges[i].to != fa)
            dfs(edges[i].to, u);
}

int main()
{
    memset(phead, -1, sizeof(phead)), memset(mhead, -1, sizeof(mhead));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v; i <= n - 1; i++)
        scanf("%d%d", &u, &v), addpath(phead, u, v), addpath(phead, v, u), deg[u]++, deg[v]++;
    for (int i = 1, u, v; i <= m; i++)
        scanf("%d%d", &u, &v), addpath(mhead, u, v), deg[v]++, vis[u] = true;
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (deg[i] == 1)
            q.push(i);
    int pt = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (deg[u] != 1)
        {
            if (deg[u] < 1)
            {
                pt = u;
                break;
            }
            clearExit();
        }
        for (int i = phead[u]; i != -1; i = edges[i].nxt)
        {
            deg[edges[i].to]--;
            if (deg[edges[i].to] == 1)
                q.push(edges[i].to);
        }
        for (int i = mhead[u]; i != -1; i = edges[i].nxt)
        {
            deg[edges[i].to]--;
            if (deg[edges[i].to] == 1)
                q.push(edges[i].to);
        }
    }
    if (pt == 0)
        clearExit();
    dfs(pt, 0);
    for (int i = 1; i <= n; i++)
        puts(ans[i] ? "1" : "0");
    return 0;
}
```

## B - Sort It Out

This problem is pretty <span class="slang" data-zh="仙" data-gloss="xiān, “immortal”, short for 神仙 (shénxiān). Said of a problem: so brilliant or tricky it seems superhuman.">godlike</span>. At first I only found one useless, graph-theoretic property, ~~which later proved to be useless indeed~~.

It actually is possible to come up with it yourself ~~(of course, everyone thinks so after reading the solution)~~. We find that taking out a relatively ordered subsequence and letting the remaining elements form the subset does indeed achieve the sorting effect described in the problem. So, to find a smallest subset, we need a longest increasing subsequence. The first question is solved.

So how do we find the $k$-th smallest subset? Since this subset gets sorted automatically when compared lexicographically, we can think greedily: if we leave more “small” elements over, the subset becomes lexicographically smaller, while the subsequence becomes lexicographically larger. Since this is a permutation, this thing definitely corresponds one to one. We just need to find the $k$-th largest longest increasing subsequence.

The way to find it is a bit like binary search on a segment tree: we can count the longest subsequences starting at position $i$, and then, by comparing sizes and subtracting sizes, decide whether to look for a larger element of the same length to adjust the lexicographic order.

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

using namespace std;

const int MAX_N = 1e5 + 200;

typedef long long ll;

int n, ai[MAX_N], head[MAX_N], current;
ll k;
bool tag[MAX_N];

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

struct node
{
    int val;
    ll sum;
} nodes[MAX_N], dp[MAX_N];

inline int lowbit(int x) { return x & (-x); }

node merge(node a, node b)
{
    if (a.val < b.val)
        swap(a, b);
    if (a.val == b.val)
        a.sum = min(a.sum + b.sum, ll(1e18));
    return a;
}

void update(int x, node d)
{
    for (; x; x -= lowbit(x))
        nodes[x] = merge(nodes[x], d);
}

node query(int x)
{
    node ret = node{0, 0};
    for (; x <= n + 1; x += lowbit(x))
        ret = merge(nodes[x], ret);
    return ret;
}

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

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%lld", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    update(n + 1, node{0, 1});
    for (int i = n; i >= 1; i--)
    {
        node tmp = query(ai[i] + 1);
        update(ai[i], dp[i] = node{tmp.val + 1, tmp.sum});
        addpath(dp[i].val, i);
    }
    for (int len = query(1).val, ptr = 0; len; len--)
        for (int i = head[len]; i != -1; i = edges[i].nxt)
        {
            if (k > dp[edges[i].to].sum)
                k -= dp[edges[i].to].sum;
            else
            {
                tag[ai[edges[i].to]] = true;
                while (ptr < edges[i].to)
                    dp[ptr++] = node{0, 0};
                break;
            }
        }
    printf("%d\n", n - query(1).val);
    for (int i = 1; i <= n; i++)
        if (!tag[i])
            printf("%d\n", i);
    return 0;
}
```
