【CodeForces】CodeForces Round #406 (Div. 1) 题解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/82871203

【比赛链接】

【题解链接】

**【A】**Berzerk

【思路要点】

  • 博弈搜索,将状态按先后手拆点,建出游戏图。
  • 若一个点存在出边指向必败态,则该点为必胜态。
  • 若一个点所有出边指向必胜态,则该点为必败态。
  • 不满足上述两点的点为平局态。
  • 用一个类似拓扑排序的过程实现即可。
  • 时间复杂度 O ( N 2 ) O(N^2)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e4 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, tot, point[MAXN][2], d[MAXN];
bool vis[MAXN], win[MAXN];
vector <int> s, t, a[MAXN];
int main() {
	read(n);
	int k; read(k);
	while (k--) {
		int x; read(x);
		s.push_back(x);
	}
	read(k);
	while (k--) {
		int x; read(x);
		t.push_back(x);
	}
	for (int i = 1; i <= n; i++) {
		point[i][0] = ++tot;
		d[tot] = s.size();
		point[i][1] = ++tot;
		d[tot] = t.size();
	}
	int l = 0, r = 1;
	static int q[MAXN];
	q[0] = point[1][0], q[1] = point[1][1];
	vis[q[0]] = vis[q[1]] = true;
	win[q[0]] = win[q[1]] = false;
	while (l <= r) {
		int tmp = q[l++], type = tmp % 2;
		int pos = (tmp + 1) / 2;
		if (win[tmp]) {
			if (type) {
				for (unsigned i = 0; i < t.size(); i++) {
					int tnp = point[(pos - t[i] + n - 1) % n + 1][1];
					if (!vis[tnp] && --d[tnp] == 0) {
						vis[tnp] = true;
						win[tnp] = false;
						q[++r] = tnp;
					}
				}
			} else {
				for (unsigned i = 0; i < s.size(); i++) {
					int tnp = point[(pos - s[i] + n - 1) % n + 1][0];
 					if (!vis[tnp] && --d[tnp] == 0) {
						vis[tnp] = true;
						win[tnp] = false;
						q[++r] = tnp;
					}
				}
			}
		} else {
			if (type) {
				for (unsigned i = 0; i < t.size(); i++) {
					int tnp = point[(pos - t[i] + n - 1) % n + 1][1];
					if (!vis[tnp]) {
						vis[tnp] = true;
						win[tnp] = true;
						q[++r] = tnp;
					}
				}
			} else {
				for (unsigned i = 0; i < s.size(); i++) {
					int tnp = point[(pos - s[i] + n - 1) % n + 1][0];
					if (!vis[tnp]) {
						vis[tnp] = true;
						win[tnp] = true;
						q[++r] = tnp;
					}
				}
			}
		}
	}
	for (int i = 2; i <= n; i++)
		if (vis[point[i][0]]) {
			if (win[point[i][0]]) printf("Win ");
			else printf("Lose ");
		} else printf("Loop ");
	printf("\n");
	for (int i = 2; i <= n; i++)
		if (vis[point[i][1]]) {
			if (win[point[i][1]]) printf("Win ");
			else printf("Lose ");
		} else printf("Loop ");
	printf("\n");
	return 0;
}

**【B】**Legacy

【思路要点】

  • 线段树建图,跑单源最短路。
  • 时间复杂度 O ( N L o g N + Q L o g 2 N ) O(NLogN+QLog^2N)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
namespace ShortestPath {
	const ll INF = 1e18;
	const int MAXP = 1e6;
	struct edge {int dest, len; };
	int n; ll dist[MAXP];
	vector <edge> a[MAXP];
	set <pair <ll, int> > st;
	void addedge(int x, int y, int z) {
		a[x].push_back((edge) {y, z});
	}
	void init(int x) {
		n = x; st.clear();
		for (int i = 1; i <= n; i++) {
			dist[i] = INF;
			a[i].clear();
		}
	}
	void work(int s) {
		dist[s] = 0;
		st.insert(make_pair(0, s));
		while (!st.empty()) {
			pair <ll, int> tmp = *st.begin();
			st.erase(tmp);
			for (unsigned i = 0; i < a[tmp.second].size(); i++) {
				int dest = a[tmp.second][i].dest;
				ll newlen = tmp.first + a[tmp.second][i].len;
				if (newlen < dist[dest]) {
					st.erase(make_pair(dist[dest], dest));
					dist[dest] = newlen;
					st.insert(make_pair(dist[dest], dest));
				}
			}
		}
	}
}
int tot, n, q, s, totp, root;
int lc[MAXN], rc[MAXN], point[MAXN][2];
void build(int &root, int l, int r) {
	root = ++totp;
	point[root][0] = ++tot;
	point[root][1] = ++tot;
	if (l == r) return;
	int mid = (l + r) / 2;
	build(lc[root], l, mid);
	build(rc[root], mid + 1, r);
}
void query(int from, int root, int l, int r, int ql, int qr, int len, int type) {
	if (l == ql && r == qr) {
		if (type == 0) ShortestPath :: addedge(from, point[root][0], len);
		else ShortestPath :: addedge(point[root][1], from, len);
		return;
	}
	int mid = (l + r) / 2;
	if (mid >= ql) query(from, lc[root], l, mid, ql, min(mid, qr), len, type);
	if (mid + 1 <= qr) query(from, rc[root], mid + 1, r, max(mid + 1, ql), qr, len, type);
}
int main() {
	read(n), read(q), read(s), tot = n;
	build(root, 1, n);
	ShortestPath :: init(tot);
	for (int i = 1, j = 0; i <= totp; i++) {
		if (lc[i]) {
			ShortestPath :: addedge(point[i][0], point[lc[i]][0], 0);
			ShortestPath :: addedge(point[i][0], point[rc[i]][0], 0);
			ShortestPath :: addedge(point[lc[i]][1], point[i][1], 0);
			ShortestPath :: addedge(point[rc[i]][1], point[i][1], 0);
		} else {
			j++;
			ShortestPath :: addedge(point[i][0], j, 0);
			ShortestPath :: addedge(j, point[i][1], 0);
		}
	}
	for (int i = 1; i <= q; i++) {
		int opt; read(opt);
		if (opt == 1) {
			int x, y, z;
			read(x), read(y), read(z);
			ShortestPath :: addedge(x, y, z);
		}
		if (opt == 2) {
			int x, l, r, z;
			read(x), read(l), read(r), read(z);
			query(x, root, 1, n, l, r, z, 0);
		}
		if (opt == 3) {
			int x, l, r, z;
			read(x), read(l), read(r), read(z);
			query(x, root, 1, n, l, r, z, 1);
		}
	}
	ShortestPath :: work(s);
	for (int i = 1; i <= n; i++) {
		ll tmp = ShortestPath :: dist[i];
		if (tmp == 1e18) printf("-1 ");
		else write(tmp), putchar(' ');
	}
	return 0;
}

**【C】**Till I Collapse

【思路要点】

  • 显然所有答案的总和不超过 N + N 2 + N 3 + . . . = O ( N L o g N ) N+\frac{N}{2}+\frac{N}{3}+...=O(NLogN)
  • 从左到右考虑所有左端点,用树状数组维护右端点对应的区间内不同数的个数,二分最远点实现贪心。
  • 时间复杂度 O ( N L o g 2 N ) O(NLog^2N)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct BinaryIndexTree {
	int n, a[MAXN];
	void init(int x) {
		n = x;
		memset(a, 0, sizeof(a));
	}
	void modify(int x, int d) {
		for (int i = x; i <= n; i += i & -i)
			a[i] += d;
	}
	int query(int x) {
		int ans = 0;
		for (int i = 20; i >= 0; i--) {
			int tmp = 1 << i;
			if (ans + tmp <= n && a[ans + tmp] <= x) {
				x -= a[ans + tmp];
				ans += tmp;
			}
		}
		return ans;
	}
} BIT;
int n, val[MAXN], last[MAXN], ans[MAXN];
vector <int> a[MAXN], home[MAXN];
int main() {
	read(n);
	for (int i = 1; i <= n; i++) {
		read(val[i]);
		a[last[val[i]]].push_back(i);
		last[val[i]] = i;
	}
	for (int i = 1; i <= n; i++)
		home[1].push_back(i);
	BIT.init(n);
	for (int i = 1; i <= n; i++) {
		if (i != 1) BIT.modify(i - 1, -1);
		for (unsigned j = 0; j < a[i - 1].size(); j++)
			BIT.modify(a[i - 1][j], 1);
		for (unsigned j = 0; j < home[i].size(); j++) {
			int tmp = home[i][j];
			ans[tmp]++, home[BIT.query(tmp) + 1].push_back(tmp);
		}
	}
	for (int i = 1; i <= n; i++)
		write(ans[i]), putchar(' ');
	return 0;
}

**【D】**Rap God

【思路要点】

  • 标算是点分治。
  • 我们用树上倍增 + + 哈希实现快速比较字符串。
  • 运用点分治,我们将问题转化到了询问 ( x , y ) (x,y) 中的 x x 在分治重心的情况。
  • x x 所在分治子树的字符串通过哈希比较排序,询问时二分即可。
  • 时间复杂度 O ( N L o g 3 N + Q L o g 3 N ) O(NLog^3N+QLog^3N)
  • 但上面的做法比较难写,下面笔者的代码是暴力的精细实现,时间复杂度 O ( N 2 ) O(N^2)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
char from[MAXN];
int n, m, father[MAXN], ans[MAXN];
vector <pair <int, int> > q[MAXN];
vector <pair <int, char> > a[MAXN];
void dfs(int pos, int fa) {
	for (unsigned i = 0; i < a[pos].size(); i++)
		if (a[pos][i].first != fa) {
			father[a[pos][i].first] = pos;
			from[a[pos][i].first] = a[pos][i].second;
			dfs(a[pos][i].first, pos);
		}
}
int solve(int pos, int to) {
	static int vis[MAXN], depth[MAXN], tag = 0;
	static char val[MAXN]; static int cmp[MAXN]; tag++;
	int tmp = n, ans = -1;
	for (int now = to; now != pos; now = father[now], tmp--) {
		val[tmp] = from[now];
		depth[now] = tmp;
		vis[now] = tag;
		cmp[now] = 1;
		ans++;
	}
	vis[pos] = tag, cmp[pos] = 1, depth[pos] = tmp;
	for (int i = 1; i <= n; i++)
		if (vis[i] != tag) {
			int top = 0, now = i;
			static int st[MAXN];
			while (vis[now] != tag) st[top++] = now, now = father[now];
			for (int j = top - 1; j >= 0; j--) {
				int now = st[j];
				depth[now] = depth[father[now]] + 1;
				vis[now] = tag;
				if (cmp[father[now]] != 1) cmp[now] = cmp[father[now]];
				else if (val[depth[now]] > from[now]) cmp[now] = 0;
				else if (val[depth[now]] == from[now]) cmp[now] = 1;
				else cmp[now] = 2;
				ans += (cmp[now] == 0) || (cmp[now] == 1 && depth[now] < n);
			}
		}
	return ans;
}
void work(int pos, int fa) {
	for (unsigned i = 0; i < q[pos].size(); i++)
		ans[q[pos][i].second] = solve(pos, q[pos][i].first);
	for (unsigned i = 0; i < a[pos].size(); i++)
		if (a[pos][i].first != fa) {
			int dest = a[pos][i].first;
			father[pos] = dest;
			father[dest] = 0;
			from[pos] = a[pos][i].second;
			work(dest, pos);
			father[dest] = pos;
			father[pos] = 0;
			from[dest] = a[pos][i].second;
		}
}
int main() {
	read(n), read(m);
	for (int i = 1; i <= n - 1; i++) {
		int x, y; char c;
		scanf("%d%d %c", &x, &y, &c);
		a[x].push_back(make_pair(y, c));
		a[y].push_back(make_pair(x, c));
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) {
		int x, y; read(x), read(y);
		q[x].push_back(make_pair(y, i));
	}
	work(1, 0);
	for (int i = 1; i <= m; i++)
		writeln(ans[i]);
	return 0;
}

**【E】**ALT

【思路要点】

  • 最大权闭合子图 + + 树上倍增建图。
  • 时间复杂度 O ( D i n i c ( N + Q , N + Q L o g N ) ) O(Dinic(N+Q,N+QLogN))

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXP = 4e5 + 5;
const int MAXN = 2e4 + 5;
const int MAXLOG = 16;
const int INF = 2e9;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct edge {int dest, flow; unsigned pos; };
vector <edge> a[MAXP];
int n, m, s, t, dist[MAXP];
unsigned curr[MAXP];
vector <int> b[MAXN], home[MAXN];
int tot, point[MAXN][MAXLOG], pos[MAXN];
int depth[MAXN], father[MAXN][MAXLOG];
void addedge(int x, int y, int z) {
	a[x].push_back((edge) {y, z, a[y].size()});
	a[y].push_back((edge) {x, 0, a[x].size() - 1});
}
int dinic(int pos, int limit) {
	if (pos == t) return limit;
	int used = 0, tmp;
	for (unsigned &i = curr[pos]; i < a[pos].size(); i++)
		if (a[pos][i].flow != 0 && dist[pos] + 1 == dist[a[pos][i].dest] && (tmp = dinic(a[pos][i].dest, min(limit - used, a[pos][i].flow)))) {
			used += tmp;
			a[pos][i].flow -= tmp;
			a[a[pos][i].dest][a[pos][i].pos].flow += tmp;
			if (used == limit) return used;
		}
	return used;
}
bool bfs() {
	static int q[MAXP];
	int l = 0, r = 0;
	memset(dist, 0, sizeof(dist));
	dist[s] = 1, q[0] = s;
	while (l <= r) {
		int tmp = q[l];
		for (unsigned i = 0; i < a[tmp].size(); i++)
			if (dist[a[tmp][i].dest] == 0 && a[tmp][i].flow != 0) {
				q[++r] = a[tmp][i].dest;
				dist[q[r]] = dist[tmp] + 1;
			}
		l++;
	}
	return dist[t] != 0;
}
void work(int pos, int fa) {
	depth[pos] = depth[fa] + 1;
	father[pos][0] = fa;
	for (int i = 1; i < MAXLOG; i++) {
		father[pos][i] = father[father[pos][i - 1]][i - 1];
		if (father[pos][i]) {
			point[pos][i] = ++tot;
			addedge(point[pos][i], point[pos][i - 1], INF);
			addedge(point[pos][i], point[father[pos][i - 1]][i - 1], INF);
		}
	}
	for (unsigned i = 0; i < b[pos].size(); i++)
		if (b[pos][i] != fa) {
			point[b[pos][i]][0] = home[pos][i];
			work(b[pos][i], pos);
		}
}
int lca(int x, int y) {
	if (depth[x] < depth[y]) swap(x, y);
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (depth[father[x][i]] >= depth[y]) x = father[x][i];
	if (x == y) return x;
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (father[x][i] != father[y][i]) {
			x = father[x][i];
			y = father[y][i];
		}
	return father[x][0];
}
int main() {
	read(n), read(m);
	for (int i = 1; i <= n - 1; i++) {
		int x, y; read(x), read(y);
		b[x].push_back(y);
		home[x].push_back(i);
		b[y].push_back(x);
		home[y].push_back(i);
	}
	s = 0, tot = n - 1;
	work(1, 0);
	for (int i = 1; i <= m; i++) {
		int x, y; read(x), read(y);
		int z = lca(x, y);
		pos[i] = ++tot;
		addedge(s, pos[i], 1);
		for (int j = MAXLOG - 1; j >= 0; j--)
			if (depth[father[x][j]] >= depth[z]) {
				addedge(pos[i], point[x][j], INF);
				x = father[x][j];
			}
		for (int j = MAXLOG - 1; j >= 0; j--)
			if (depth[father[y][j]] >= depth[z]) {
				addedge(pos[i], point[y][j], INF);
				y = father[y][j];
			}
	}
	t = ++tot;
	for (int i = 1; i <= n - 1; i++)
		addedge(i, t, 1);
	int ans = 0;
	while (bfs()) {
		memset(curr, 0, sizeof(curr));
		ans += dinic(s, INF);
	}
	printf("%d\n", ans);
	bfs(); int size = 0;
	for (int i = 1; i <= m; i++)
		if (dist[pos[i]] == 0) size++;
	printf("%d ", size);
	for (int i = 1; i <= m; i++)
		if (dist[pos[i]] == 0) printf("%d ", i);
	printf("\n");
	size = 0;
	for (int i = 1; i <= n - 1; i++)
		if (dist[i] != 0) size++;
	printf("%d ", size);
	for (int i = 1; i <= n - 1; i++)
		if (dist[i] != 0) printf("%d ", i);
	printf("\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39972971/article/details/82871203