【LOJ3056】「HNOI2019」多边形

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

【题目链接】

【思路要点】

  • 不难发现唯一的终止状态就是所有点均与 N N 直接相连的状态。
  • 因此,使用最少的操作达到终止状态的方案一定是每次选择一个不与 N N 直接相连,且与 N N 连线只与一条边相交的节点,旋转对应的四边形。
  • 因此最少次数即为不与 N N 直接相连的点的个数。
  • 考虑计算方案数,不难发现各个与 N N 直接相连的点将多边形分成了若干块,每一块的方案数相互独立,只需在最后用组合数合并,以下考虑不存在非平凡的与 N N 直接相连的点的情况。
  • 定义三角形的编号为三个顶点中编号中等的点的编号, L x L_x 表示三角形 x x 中较小点的编号, R x R_x 表示三角形 x x 中较大点的编号,则 x x 可以通过一次操作与 N N 直接相连当且仅当 L x , R x L_x,R_x 均已经与 N N 直接相连,因此合法的操作序列数本质是一个有向图的拓扑排序数。
  • 同时,注意到 L x , R x L_x,R_x 通过一条边直接相连,他们之间应当存在祖孙关系,因此我们要计算的实际上是一棵外向树的拓扑排序数,可以用组合数简单计算。
  • 考虑如何回答修改,若一次修改导致了一个新的点直接连向 N N ,那么操作数 1 -1 ,且对应部分的外向树根节点被删除;否则,这次修改将改动对应部分的外向树上一对父子处的祖先关系,均可 O ( 1 ) O(1) 计算新的贡献。
  • 时间复杂度 O ( N L o g N + M L o g N ) O(NLogN+MLogN) O ( N + M ) O(N+M)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int P = 1e9 + 7;
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 fac[MAXN], inv[MAXN];
int power(int x, int y) {
	if (y == 0) return 1;
	int tmp = power(x, y / 2);
	if (y % 2 == 0) return 1ll * tmp * tmp % P;
	else return 1ll * tmp * tmp % P * x % P;
}
int binom(int x, int y) {
	if (y > x) return 0;
	else return 1ll * fac[x] * inv[y] % P * inv[x - y] % P;
}
void init(int n) {
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = 1ll * fac[i - 1] * i % P;
	inv[n] = power(fac[n], P - 2);
	for (int i = n - 1; i >= 0; i--)
		inv[i] = inv[i + 1] * (i + 1ll) % P;
}
int type, n, ans, cnt, size[MAXN], lp[MAXN], rp[MAXN];
vector <int> a[MAXN], t[MAXN];
map <pair <int, int>, int> mp, home;
void addedge(int x, int y) {
	a[x].push_back(y);
	a[y].push_back(x);
}
int getinv(int x) {
	return 1ll * inv[x] * fac[x - 1] % P;
}
void work(int pos, bool type) {
	size[pos] = 1;
	for (auto x : t[pos]) {
		work(x, true);
		size[pos] += size[x];
	}
	if (type) {
		ans = 1ll * ans * getinv(size[pos]) % P;
		for (auto x : t[pos]) {
			pair <int, int> t = make_pair(lp[x], pos);
			if (t.first > t.second) swap(t.first, t.second);
			mp[t] = 1ll * size[x] * getinv(size[pos] - size[x] + size[home[make_pair(x, rp[x])]]) % P;
		}
	}
}
int main() {
	read(type), read(n);
	init(n), addedge(1, n);
	for (int i = 1; i <= n - 1; i++)
		addedge(i, i + 1);
	for (int i = 1; i <= n - 3; i++) {
		int x, y; read(x), read(y);
		addedge(x, y);
	}
	for (int i = 1; i <= n; i++)
		sort(a[i].begin(), a[i].end());
	int last = 1; ans = 1, cnt = 0;
	for (int i = 2; i <= n - 1; i++) {
		if (a[i].back() != n) continue;
		static int d[MAXN];
		static vector <int> g[MAXN];
		for (int j = last + 1; j <= i - 1; j++) {
			int x = a[j][0];
			int y = a[j].back();
			lp[j] = x, rp[j] = y;
			home[make_pair(x, y)] = j;
			if (x != last) g[x].push_back(j), d[j]++;
			g[y].push_back(j), d[j]++;
		}
		deque <int> q; q.push_back(i);
		while (!q.empty()) {
			int pos = q.front(); q.pop_front();
			for (auto x : g[pos])
				if (--d[x] == 0) {
					q.push_back(x);
					t[pos].push_back(x);
				}
		}
		work(i, 0);
		cnt += i - last - 1, last = i;
	}
	ans = 1ll * ans * fac[cnt] % P;
	if (type) printf("%d %d\n", cnt, ans);
	else printf("%d\n", cnt);
	int m; read(m);
	for (int i = 1; i <= m; i++) {
		int x, y; read(x), read(y);
		if (x > y) swap(x, y);
		pair <int, int> t = make_pair(x, y);
		if (mp.count(t)) {
			if (type) printf("%d %lld\n", cnt, 1ll * ans * mp[t] % P);
			else printf("%d\n", cnt);
		} else {
			int pos = home[t];
			int tans = 1ll * ans * getinv(cnt) % P * size[pos] % P;
			if (type) printf("%d %d\n", cnt - 1, tans);
			else printf("%d\n", cnt - 1);
		}
	}
	return 0;
}

猜你喜欢

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