QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334288#7222. The Great HuntjeefyWA 0ms11840kbC++143.3kb2024-02-21 17:15:022024-02-21 17:15:03

Judging History

你现在查看的是最新测评结果

  • [2024-02-21 17:15:03]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:11840kb
  • [2024-02-21 17:15:02]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <vector> 

using namespace std;
const int N = 4e4 + 7, inf = 1e9;

namespace ISAP {
	const int M = 2e7;
	int S, T, n;
	
	int head[N], now[N], nxt[M], to[M], fl[M], use = 1;
	void add(int x, int y, int f = 1) {
		to[++use] = y, fl[use] = f, nxt[use] = head[x], head[x] = use;
		to[++use] = x, fl[use] = 0, nxt[use] = head[y], head[y] = use;
	}
	
	int dep[N], gap[N], Q[N];
	void init() {
		fill_n(dep, n + 1, -1), fill_n(gap, n + n, 0);
		
		int hd = 0, tl = 0; ++gap[dep[Q[tl++] = T] = 0];
		
		while (hd ^ tl) {
			int x = Q[hd++]; now[x] = head[x];
			for (int i = head[x]; i; i = nxt[i]) {
				if (dep[to[i]] == -1) ++gap[dep[to[i]] = dep[x] + 1], Q[tl++] = to[i];
			}
		}
	}
	
	int sap(int x, int flow) {
		if (x == T) return flow;
		
		int rest = flow;
		for (int &i = now[x]; i; i = nxt[i]) {
			if (!fl[i] || dep[x] != dep[to[i]] + 1) continue;
			int push = sap(to[i], min(fl[i], rest));
			fl[i] -= push, fl[i ^ 1] += push, rest -= push;
			if (!rest) return flow;
		}
		
		if (--gap[dep[x]] == 0) dep[S] = n + 1;
		return now[x] = head[x], ++gap[++dep[x]], flow - rest;
	}
	
	int calc(int _n) {
		n = _n, init();
		
		int flow = 0;
		while (dep[S] <= n) flow += sap(S, inf);
		return flow;
	}
	
	
	int dfs(int x, int p, int lim) {
		if (x > lim && x <= 2 * lim) return x - lim;
		for (int i = head[x]; i; i = nxt[i]) {
			if (to[i] == p || (i & 1) || !fl[i ^ 1]) continue;
			--fl[i ^ 1]; ++fl[i];
			return dfs(to[i], x, lim);
		} return -1;
	}
}

vector<int> T[N];

int top[N], idx[N], stk[N], ans[N], stop = 0;
void prep(int x, int p, int t) {
	top[x] = t, idx[x] = idx[p] + 1;
	stk[++stop] = x;
	for (int y : T[x]) {
		if (y != p) prep(y, x, t);
	}
}

int rt[N], kc[N], lc[N * 4], rc[N * 4], use, n;
void build(int &p, int l, int r) {
	if (!p) p = ++use;
	if (l == r) return ISAP::add(p, stk[l] + n);
	int mid = (l + r) >> 1;
	build(lc[p], l, mid), build(rc[p], mid + 1, r);
	ISAP::add(p, lc[p], inf), ISAP::add(p, rc[p], inf);
}

void con(int i, int L, int R, int p, int l, int r) {
	if (L <= l && r <= R) return ISAP::add(i, p);
	int mid = (l + r) >> 1;
	if (L <= mid) con(i, L, R, lc[p], l, mid);
	if (R > mid) con(i, L, R, rc[p], mid + 1, r);
} 

int main() {
	cin.tie(0)->sync_with_stdio(false);
	
	cin >> n;
	for (int x, y, i = 2; i <= n; ++i) {
		cin >> x >> y; T[x].push_back(y), T[y].push_back(x);
	}
	
	use = n + n + 1;
	for (int z : T[1]) {
		prep(z, 1, z);
		build(rt[z], 1, kc[z] = stop), stop = 0;
	}
	
	for (int x, y, i = 1; i <= n; ++i) {
		cin >> x >> y;
		if (top[x] != top[y]) {
			if (x > 1) con(i, 1, idx[x], rt[top[x]], 1, kc[top[x]]);
			if (y > 1) con(i, 1, idx[y], rt[top[y]], 1, kc[top[y]]);
			ISAP::add(i, n + 1);
		} else {
			if (idx[x] > idx[y]) swap(x, y);
			con(i, idx[x], idx[y], rt[top[x]], 1, kc[top[x]]);
		}
	}
	
	for (int i = 1; i <= n; ++i) ISAP::add(0, i); 
	for (int i = 1; i <= n; ++i) ISAP::add(i + n, use + 1);
	ISAP::S = 0, ISAP::T = use + 1;
	int maxflow = ISAP::calc(use + 1);
	cerr << maxflow << " =\n";
	if (maxflow != n)  {
		cout << "No\n";
		return 0;
	} 
	cout << "Yes\n";
	for (int i = 1; i <= n; ++i) ans[ISAP::dfs(i, 0, n)] = i;
	for (int i = 1; i <= n; ++i) cout << ans[i] << " \n"[i == n];
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 11840kb

input:

10
2 1
3 1
6 1
8 2
4 3
9 6
5 4
7 8
10 7
8 7
10 2
7 2
10 1
3 1
5 2
5 1
6 1
9 2
3 4

output:

Yes
4 6 5 10 7 8 3 1 9 2

result:

wrong answer knight 1 is not on his path!