[Fortuna OJ] Jul 6th - Group B Problem-Solving Report
A - Tweak
In the contest I coded up a wrong solution for this problem, and still scraped 30 points with it.
The normal line of thinking is: find a few edges on the shortest path graph and modify them. However, the optimal solution doesn't necessarily lie on edges of the shortest path graph; it can also be reached by modifying suboptimal edges. So this problem actually doesn't have all that much to do with shortest paths, even though Djisktra will be used later to compute shortest paths.
We can copy a \(G(V, E), |V| = n, |E| = m\) into \(n\) copies, and then for the \(k\)-th copy of the graph, from its directed edge \((u_k, v_k, w_k)\) we can generate a duplicate that connects to the target vertex in the next layer; that is, every edge spawns a \((u_k, v_{k + 1}, 0)\). When we finally compute the shortest path, passing through such an edge can be regarded as modifying one edge, and, this edge goes one way, downward, so modifying several times within the same layer can't happen. In the end we only need to compute the shortest paths in this graph, then check for \(dist[n_k] \leq c\) and take the smallest such \(k\), and that's it.
// A.cpp
#include <bits/stdc++.h>
#define pr pair<int, int>
using namespace std;
const int MAX_N = 1100;
int n, m, c, dist[MAX_N * MAX_N], current, head[MAX_N * MAX_N];
bool vis[MAX_N * MAX_N];
struct edge
{
int to, nxt, weight;
} edges[1010 + MAX_N * MAX_N];
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 djisktra()
{
memset(dist, 0x3f, sizeof(dist));
priority_queue<pr> q;
q.push(make_pair(0, 1));
dist[1] = 0;
while (!q.empty())
{
int u = q.top().second;
q.pop();
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (dist[edges[i].to] > dist[u] + edges[i].weight)
dist[edges[i].to] = dist[u] + edges[i].weight, q.push(make_pair(-dist[edges[i].to], edges[i].to));
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d%d", &n, &m, &c);
for (int i = 1; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
for (int layer = 0; layer < n; layer++)
addpath(layer * n + u, layer * n + v, w);
for (int layer = 0; layer < n - 1; layer++)
addpath(layer * n + u, (layer + 1) * n + v, 0);
}
djisktra();
for (int layer = 0; layer < n; layer++)
if (dist[layer * n + n] <= c)
{
printf("%d", layer);
return 0;
}
return 0;
}
B - Tree A
A stupid problem, a problem that's just heavy-light decomposition template code, not going to explain it.
// B.cpp
#include <bits/stdc++.h>
#define lson (p << 1)
#define rson ((p << 1) | 1)
#define mid ((l + r) >> 1)
using namespace std;
const int MAX_N = 31000;
int head[MAX_N], current, pt_weight[MAX_N], dfn[MAX_N], antidfn[MAX_N], m;
int top[MAX_N], son[MAX_N], siz[MAX_N], dep[MAX_N], fa[MAX_N], dfntot, n, opt;
int tree[MAX_N << 2];
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++;
}
void build(int l, int r, int p)
{
if (l == r)
return (void)(tree[p] = pt_weight[antidfn[l]]);
build(l, mid, lson), build(mid + 1, r, rson);
tree[p] = tree[lson] + tree[rson];
}
void update(int qx, int l, int r, int p, int val)
{
if (l == r && qx == l)
return (void)(tree[p] = val);
if (qx <= mid)
update(qx, l, mid, lson, val);
else
update(qx, mid + 1, r, rson, val);
tree[p] = tree[lson] + tree[rson];
}
int query(int ql, int qr, int l, int r, int p)
{
if (ql <= l && r <= qr)
return tree[p];
int ans = 0;
if (ql <= mid)
ans += query(ql, qr, l, mid, lson);
if (mid < qr)
ans += query(ql, qr, mid + 1, r, rson);
return ans;
}
int query(int x, int y)
{
if (dep[top[x]] > dep[top[y]])
swap(x, y);
int ans = 0;
while (top[x] != top[y])
{
ans += query(dfn[top[y]], dfn[y], 1, n, 1);
y = fa[top[y]];
if (dep[top[x]] > dep[top[y]])
swap(x, y);
}
if (dep[x] > dep[y])
swap(x, y);
ans += query(dfn[x], dfn[y], 1, n, 1);
return ans;
}
void dfs1(int u)
{
siz[u] = 1, dep[u] = dep[fa[u]] + 1;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa[u])
{
fa[edges[i].to] = u, dfs1(edges[i].to);
siz[u] += siz[edges[i].to];
if (siz[edges[i].to] > siz[son[u]])
son[u] = edges[i].to;
}
}
void dfs2(int u, int org)
{
dfn[u] = ++dfntot, antidfn[dfntot] = u, top[u] = org;
if (son[u] == 0)
return;
dfs2(son[u], org);
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa[u] && edges[i].to != son[u])
dfs2(edges[i].to, edges[i].to);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1, u, v; i <= n - 1; i++)
scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
dfs1(1), dfs2(1, 1);
for (int i = 1; i <= n; i++)
scanf("%d", &pt_weight[i]);
build(1, n, 1);
while (m--)
{
int A, B;
scanf("%d%d%d", &opt, &A, &B);
if (opt == 1)
update(dfn[A], 1, n, 1, B);
else
printf("%d\n", query(A, B));
}
return 0;
}
C - Tree B
Another stupid problem, if I hadn't made the edge array too small, I could have AC’d one more problem in the contest, and it wouldn't have come to just 50 points.
This problem is quite obviously a tree DP. Consider marking the vertices to be isolated; then the tree DP equations are as follows:
\[ \begin{cases} dp[u][0] = \sum_{v \in son(u)} min\{ dp[v][0], dp[v][1] \}[tag[v] = false] + dp[v][1][tag[v] = true] \\ dp[u][1] = e(e, fa[u]).weight \end{cases} \]
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 200;
int n, m, head[MAX_N], current, fa[MAX_N], upward[MAX_N];
int dp[MAX_N][2];
bool tag[MAX_N];
struct edge
{
int to, nxt, weight;
} edges[MAX_N << 1];
void addpath(int src, int dst, int weight)
{
edges[current].to = dst, edges[current].weight = weight;
edges[current].nxt = head[src], head[src] = current++;
}
void preprocess(int u)
{
dp[u][1] = edges[upward[u]].weight;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa[u])
{
fa[edges[i].to] = u, upward[edges[i].to] = i;
preprocess(edges[i].to);
if (tag[edges[i].to])
dp[u][0] += dp[edges[i].to][1];
else
dp[u][0] += min(dp[edges[i].to][0], dp[edges[i].to][1]);
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1, u, v, c; i <= n - 1; i++)
scanf("%d%d%d", &u, &v, &c), addpath(u, v, c), addpath(v, u, c);
for (int i = 1, tmp; i <= m; i++)
scanf("%d", &tmp), tag[tmp] = true;
preprocess(1);
printf("%d", dp[1][0]);
return 0;
}
D - Epoch-Making
This problem is really nasty.
My initial approach: consider bitmasking; store every length the sticks can be joined into in buckets, then enumerate the lengths of the two sides, enumerate the stick states under the current length, and check with AND, that is, like this:
// D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 20;
int n, li[MAX_N], mxlen, S = -0x3f3f3f3f, sizes[20 * 15];
int bucket[20 * 15][20 * 150];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &li[i]), mxlen += li[i];
for (int i = 0; i < (1 << n); i++)
{
int len = 0;
for (int j = 1; j <= n; j++)
if (i & (1 << (j - 1)))
len += li[j];
bucket[len][sizes[len]++] = i;
}
for (int lenA = mxlen; lenA >= 1; lenA--)
{
int sizA = sizes[lenA];
if (sizA == 0)
continue;
if (lenA * mxlen <= S)
break;
for (int lenB = mxlen; lenB >= 1; lenB--)
{
int sizB = sizes[lenB];
if (sizB == 0)
continue;
if (lenA * lenB <= S)
break;
for (int x = 0; x < sizA; x++)
{
int statX = bucket[lenA][x];
for (int y = 0; y < sizA; y++)
{
int statY = bucket[lenA][y];
if ((statX & statY) != 0)
continue;
for (int z = 0; z < sizB; z++)
{
int statZ = bucket[lenB][z];
if((statX & statZ) != 0 || (statY & statZ) != 0)
continue;
for (int w = 0; w < sizB; w++)
{
int statW = bucket[lenB][w];
if ((statW & statX) != 0 || (statW & statY) != 0 || (statW & statZ) != 0)
continue;
S = max(S, lenA * lenB);
}
}
}
}
}
}
if (S < 0)
printf("No Solution");
else
printf("%d", S);
return 0;
}
Then, um, TLE.
In the end, the approach I saw from others goes like this: consider bitmasking one set of sides, that is, a pair of lengths or a pair of widths; while doing the bitmask, run a knapsack DP to check whether this set of sides can be split into two parts, and if it can, put a mark on it. Then, do a search: in the DFS, the time complexity is \(O(3^n)\), because every side has the state of being chosen or not chosen, and then just consider allocating lengths and widths.
// D.cpp
#include <bits/stdc++.h>
const int MAX_N = 17;
using namespace std;
int n, ai[MAX_N], dp[MAX_N * 20], ans = -1;
bool flag[(1 << MAX_N)];
void dfs(int dep, int statA, int lenA, int statB, int lenB)
{
if (dep == n)
{
if (flag[statA] && flag[statB])
ans = max(ans, (lenA * lenB) / 4);
return;
}
dfs(dep + 1, statA, lenA, statB, lenB);
dfs(dep + 1, statA | (1 << dep), lenA + ai[dep + 1], statB, lenB);
dfs(dep + 1, statA, lenA, statB | (1 << dep), lenB + ai[dep + 1]);
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]);
for (int stat = 1; stat < (1 << n); stat++)
{
int sum = 0;
for (int i = 1; i <= n; i++)
if ((stat >> (i - 1)) & 1)
sum += ai[i];
if (sum & 1)
continue;
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 1; i <= n; i++)
if ((stat >> (i - 1)) & 1)
for (int k = sum; k >= ai[i]; k--)
dp[k] |= dp[k - ai[i]];
if (dp[sum >> 1])
flag[stat] = true;
}
dfs(0, 0, 0, 0, 0);
if (ans > 0)
printf("%d", ans);
else
puts("No Solution");
return 0;
}