2022“杭电杯”中国大学生算法设计超级联赛(10)
[题目链接](Search Result (hdu.edu.cn))
G Even Tree Split
题目大意
有一棵树,可以删除其中的至少一条边,使得树的每一个联通块的点数都为偶数,求有多少种方案。
题解
对每条边,如果删去该边后两个连通块的点数都为偶数,则可以删掉该边。
进一步发现,若一个点的子树大小为偶数,则这个点和其父结点之间的边就是满足条件的边。
若子树大小为偶数的点的个数为cnt,那么可以删的边一共为cnt-1个,因为1这个点没有可以删的边。
因为可以选择不同的边和不同的边数,所以答案为2cnt-1-1。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 998244353;
int t, n, cnt;
int tot, head[maxn], siz[maxn], p2[maxn];
struct Edge
{
int v;
int next;
} edge[maxn << 1];
void init()
{
memset(head, -1, sizeof head);
memset(siz, 0, sizeof siz);
tot = cnt = 0;
}
void add(int u, int v)
{
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
int dfs(int u, int fa)
{
siz[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next)
{
if (edge[i].v == fa)
continue;
siz[u] += dfs(edge[i].v, u);
}
return siz[u];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
p2[0] = 1;
for (int i = 1; i < maxn; i++)
p2[i] = (p2[i - 1] * 2) % mod;
cin >> t;
while (t--)
{
init();
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
dfs(1, 0);
for (int i = 2; i <= n; i++)
if (siz[i] % 2 == 0)
cnt++;
cout << p2[cnt] - 1 << endl;
}
return 0;
}
C Wavy Tree
题目大意
波浪数组:对于数组中的每一个数来说,要么它大于两边的数字,要么它小于两边的数字。
现有一个长度为n的数组b,可以多次对某个数加一或者减一,每次操作消耗1。
问将b数组变成波浪数组的最少消耗。
题解
贪心。
容易发现,b数组只有两种情况:
① 奇数位为大数;
② 偶数位为大数。
然后只要比较两种情况的大小即可。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
ll t, n, res1, res2;
ll a[maxn], b[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
{
res1 = res2 = 0;
cin >> n;
for (ll i = 1; i <= n; i++)
{
cin >> a[i];
b[i] = a[i];
}
for (ll i = 2; i <= n; i++)
{
if (i % 2)
{
if (a[i] >= a[i - 1])
{
res1 += (a[i] - a[i - 1] + 1);
a[i] = a[i - 1] - 1;
}
}
else
{
if (a[i] <= a[i - 1])
{
res1 += (a[i - 1] - a[i] + 1);
a[i] = a[i - 1] + 1;
}
}
}
for (ll i = 2; i <= n; i++)
{
if (i % 2)
{
if (b[i] <= b[i - 1])
{
res2 += (b[i - 1] - b[i] + 1);
b[i] = b[i - 1] + 1;
}
}
else
{
if (b[i] >= b[i - 1])
{
res2 += (b[i] - b[i - 1] + 1);
b[i] = b[i - 1] - 1;
}
}
}
cout << min(res1, res2) << endl;
}
return 0;
}
A Winner Prediction
题目大意
有n个人比赛,两两对决,已经打了m1场比赛,还有m2场比赛没有打。最后赢的次数最多的人获胜。问1号是否有可能获胜。
题解
网络流。
先让1号选手赢下所有和他有关的比赛。
设此时选手i赢了ai场比赛。如果存在某个ai>a1,则1号选手不可能成为冠军。否则选手i至多还能再赢bi=a1-ai场比赛。
建图:每场未进行的比赛在图中用一个点表示,源点向它连容量为1的边,它向它的两个参赛选手的对应点各自连容量为1的边;选手i的对应点向汇点连容量为bi的边。
然后计算该图最大流,若源点出发的边满流则答案为 YES ,否则为 NO 。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 1e4 + 5, maxm = 1e4 + 5;
ll T, n, m1, m2, s, t, num;
ll ans;
ll cnt, first[maxn], nxt[maxm], to[maxm], val[maxm];
ll a[maxn];
ll dep[maxn];
bool ok;
void add(ll u, ll v, ll w)
{
to[++cnt] = v;
val[cnt] = w;
nxt[cnt] = first[u];
first[u] = cnt;
}
bool bfs()
{
memset(dep, 0, sizeof dep);
queue<ll> q;
q.push(s);
dep[s] = 1;
while (!q.empty())
{
ll u = q.front();
q.pop();
for (ll p = first[u]; p; p = nxt[p])
{
ll v = to[p];
if (val[p] && !dep[v])
{
dep[v] = dep[u] + 1;
q.push(v);
}
}
}
return dep[t];
}
ll dfs(ll u, ll in)
{
if (u == t)
return in;
ll out = 0;
for (ll p = first[u]; p && in; p = nxt[p])
{
ll v = to[p];
if (val[p] && dep[v] == dep[u] + 1)
{
ll res = dfs(v, min(val[p], in));
val[p] -= res;
val[p ^ 1] += res;
in -= res;
out += res;
}
}
if (out == 0)
dep[u] = 0;
return out;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--)
{
cin >> n >> m1 >> m2;
s = 0, t = n + m2 + 1;
num = ans = 0;
cnt = ok = 1;
memset(first, 0, sizeof first);
memset(a, 0, sizeof a);
for (ll i = 1; i <= m1; i++)
{
ll x, y, z;
cin >> x >> y >> z;
if (z)
a[x]++;
else
a[y]++;
}
for (ll i = 1; i <= m2; i++)
{
ll x, y;
cin >> x >> y;
if (x == 1 || y == 1)
a[1]++;
else
{
num++;
add(s, i, 1);
add(i, s, 0);
add(i, m2 + x, 1);
add(m2 + x, i, 0);
add(i, m2 + y, 1);
add(m2 + y, i, 0);
}
}
for (ll i = 2; i <= n; i++)
{
if (a[i] > a[1])
{
ok = 0;
break;
}
add(m2 + i, t, a[1] - a[i]);
add(t, m2 + i, 0);
}
if (!ok)
{
cout << "NO" << endl;
continue;
}
while (bfs())
ans += dfs(s, 1e18);
if (ans == num)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
I Painting Game
题目大意
有一个网格数为n的一维网格,Alice,Bob轮流给一个格子涂黑色,要求黑色的格子不能相邻。Alice想要最小化黑色格子的数量,Bob想要最大化黑色格子的数量。
问格子数为n,Alice/Bob先手时,黑色格子的数量。
题解
通过观察可得,Alice的最优策略是:选某个连续段的左数第二个格子涂黑,这样一个黑格子占三格;Bob的最优策略是:选某个连续段的左数第三个格子涂黑,这样两个黑格子占四格。
所以每7个格子可以涂3个黑格子。不足7个格子的部分,分情况讨论。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int t, n, cnta, cntb, x, ya, yb;
string s;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
{
cin >> n >> s;
x = n / 7;
ya = yb = n % 7;
cnta = cntb = x * 3;
if (ya > 0)
{
cnta++;
ya -= 3;
}
if (ya >= 3)
cnta += 2;
else if (ya > 0)
cnta++;
if (yb >= 3)
{
cntb += 2;
yb -= 4;
}
else if (yb > 0)
{
cntb++;
yb -= 2;
}
if (yb > 0)
cntb++;
if (s == "Alice")
cout << cnta << endl;
else
cout << cntb << endl;
}
return 0;
}