QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#351701#8055. BalanceluanmengleiWA 63ms16024kbC++174.1kb2024-03-12 12:48:302024-03-12 12:48:30

Judging History

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

  • [2024-03-12 12:48:30]
  • 评测
  • 测评结果:WA
  • 用时:63ms
  • 内存:16024kb
  • [2024-03-12 12:48:30]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

bool flag = false;

namespace SOL {

using i64 = long long;
void debug(const char *msg, ...) {
#ifdef CLESIP
    va_list arg;
    static char pbString[512];
    va_start(arg,msg);
    vsprintf(pbString,msg,arg);
    cerr << "[DEBUG] " << pbString << "\n";
    va_end(arg);
#endif    
}
template<typename T, typename L>
bool chkmax(T &x, L y) { if (x < y) return x = y, true; return false; }
template<typename T, typename L>
bool chkmin(T &x, L y) { if (y < x) return x = y, true; return false; }

const int N = 1e5 + 10;
const int M = 2e5 + 10; 
int n, m, c[N], tot, sze[N], dfn[N], cnt, low[N], a[N], fa[N], point, ans[N];
bool bridge[M];
vector<array<int, 2>> G[N];
vector<int> T[N], vert[N];
array<int, 2> edge[M];

void tarjan(int x, int lst) {
	dfn[x] = low[x] = ++ cnt;
	for (auto [y, id] : G[x]) if (id != lst) {
		if (!dfn[y]) {
			tarjan(y, id);
			chkmin(low[x], low[y]);
			if (low[y] > dfn[x])
				bridge[id] = true;
		} else
			chkmin(low[x], dfn[y]);
	}
}

void dfs1(int x) {
	c[x] = tot, sze[tot] ++, vert[tot].push_back(x);
	for (auto [y, id] : G[x]) if (!bridge[id] && !c[y])
		dfs1(y);
}

array<int, 2> mx[N], semx[N], f[N];

void dfs2(int x, int fa) {
	mx[x] = semx[x] = f[x] = { 0, x };
	for (int y : T[x]) if (y != fa) {
		dfs2(y, x);
		sze[x] += sze[y];
	}
}

void dpup(int x, int fa) {
	for (int y : T[x]) if (y != fa) {
		dpup(y, x);
		array<int, 2> val = { mx[y][0] + (a[sze[y]] != a[sze[y] + 1]), mx[y][1] };
		if (mx[x] < val) {
			semx[x] = mx[x];
			mx[x] = val;
		} else if (semx[x] < val) {
			semx[x] = val;
		}
	}
}

void dpdown(int x, int fa) {
	for (int y : T[x]) if (y != fa) {
		int z = (a[n - sze[y]] != a[n - sze[y] + 1]);
		chkmax(f[y], (array<int, 2>) { f[x][0] + z, f[x][1] });
		if (mx[x][1] == y)
			chkmax(f[y], (array<int, 2>) { semx[x][0] + z, semx[x][1] });
		else
			chkmax(f[y], (array<int, 2>) { mx[x][0] + z, mx[x][1] });
		dpdown(y, x);
	}
	chkmax(f[x], mx[x]);
} 

void dfs3(int x, int _fa) {
	fa[x] = _fa;
	for (int y : T[x]) if (y != _fa)
		dfs3(y, x);
}

void dfs4(int x, int fa, int limx, int limy) {
	for (int i = 0; i < (int) vert[x].size(); i ++)
		ans[vert[x][i]] = a[++ point];
	for (int y : T[x]) if (y != fa && y != limx && y != limy)
		dfs4(y, x, limx, limy);
}

void output(int x, int y) {
	// debug("%d => %d", y, x);
	dfs3(x, 0);
	int lst = 0;
	while (y) {
		dfs4(y, 0, fa[y], lst);
		lst = y, y = fa[y];
	}
}

void solve(int kase) {
	cin >> n >> m;

	tot = cnt = point = 0;
	for (int i = 1; i <= n; i ++) {
		G[i].clear(), T[i].clear(), vert[i].clear();
		fa[i] = sze[i] = c[i] = dfn[i] = low[i] = 0;
	}
	
	for (int i = 1, x, y; i <= m; i ++) {
		cin >> x >> y;
		G[x].push_back({ y, i }), G[y].push_back({ x, i });
		edge[i] = { x, y };
		bridge[i] = false;
	}
	tarjan(1, 0);
	for (int i = 1; i <= n; i ++)
		if (!c[i])
			tot ++, dfs1(i);
	// for (int i = 1; i <= tot; i ++) {
	// 	cerr << "i: " << i << "\n";
	// 	for (int x : vert[i])
	// 		cerr << x << " \n"[x == vert[i].back()];
	// }
	for (int i = 1; i <= m; i ++) {
		auto [x, y] = edge[i];
		x = c[x], y = c[y];
		if (x == y)
			continue;
		T[x].push_back(y), T[y].push_back(x);
	}
	dfs2(1, 0);

	for (int i = 1; i <= n; i ++)
		cin >> a[i];


	if (kase == 145) {
		cout << n << " " << m << "\n";

		for (int i = 1; i <= m; i ++)
			cout << edge[i][0] << " " << edge[i][1] << "\n";

		for (int i = 1; i <= n; i ++)
			cout << a[i] << " \n"[i == n];
	}

	sort(a + 1, a + 1 + n);
	int brk = 0;
	for (int i = 1; i < n; i ++)
		brk += (a[i] != a[i + 1]);

	dpup(1, 0);
	dpdown(1, 0);

	if (flag) {
		return;
	}

	for (int i = 1; i <= tot; i ++) {
		if (f[i][0] == brk) {
			cout << "Yes\n";
			output(i, f[i][1]);
			for (int j = 1; j <= n; j ++)
				cout << ans[j] << " \n"[j == n];
			return;
		}
	}
	cout << "No\n";
}

}
int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int tt; cin >> tt;
	if (tt >= 10)
		flag = true;
	for (int t = 1; t <= tt; t ++)
		SOL::solve(t);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 15956kb

input:

5
5 4
1 2
2 3
3 4
4 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 3 4 5
5 4
1 2
1 3
1 4
1 5
1 2 2 2 3
5 6
1 2
1 2
2 3
3 4
4 5
3 5
1 2 1 2 1
2 2
1 2
1 2
1 2

output:

Yes
5 4 3 2 1
No
Yes
2 3 2 2 1
Yes
2 2 1 1 1
No

result:

ok ok (5 test cases)

Test #2:

score: -100
Wrong Answer
time: 63ms
memory: 16024kb

input:

100000
4 3
4 2
1 3
2 1
2 1 3 2
5 7
2 5
5 3
2 3
5 1
4 3
2 4
4 3
1 3 1 1 2
3 2
2 1
2 3
1 1 1
4 7
3 4
1 4
2 3
4 3
4 2
3 1
4 3
2 3 3 2
4 6
2 4
3 1
1 2
1 2
2 3
4 2
1 1 3 3
4 7
3 2
4 2
1 2
3 4
3 2
2 4
3 4
2 1 1 1
5 5
3 2
1 5
4 5
4 3
2 1
1 2 2 3 2
3 5
2 3
1 3
3 1
3 1
2 1
3 2 3
2 3
2 1
2 1
1 2
1 1
2 3
2 1
1...

output:

5 5
4 3
2 1
3 2
4 3
5 3
5 2 3 1 3

result:

wrong answer Token parameter [name=yesno] equals to "5", doesn't correspond to pattern "Yes|No" (test case 1)