[USACO 2018.12 Platinum] Problem-Solving Report
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)\).
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. Hand-wavy intuition: suppose everything has been covered; we find that our valid, feasible nodes can definitely form a unique subgraph, unaffected by the root.
// 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 godlike. 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.
// 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;
}