QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#470722#6396. Puzzle: KusabiUESTC_DebugSimulator#WA 1ms7916kbC++172.8kb2024-07-10 16:04:032024-07-10 16:04:03

Judging History

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

  • [2024-07-10 16:04:03]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7916kb
  • [2024-07-10 16:04:03]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(i) (i&-i)
#define rand() (myrand())
using namespace std;
mt19937 myrand(time(0));
const int maxn = 2e5 + 5;
int n, _, head[maxn], cnt;
int c[maxn], use[maxn], dep[maxn], jud, fa[maxn];
string ty;
vector<pair<int, int> >ans;
struct node{
	int next, to;
}edge[maxn<<1];
void addedge(int from, int to) {
	edge[++cnt].next = head[from];
	edge[cnt].to = to;
	head[from] = cnt;
}
bool cmp(int x, int y) {return dep[x] < dep[y];}
int dfs(int u, int p) {
	if (jud) return 0;
	dep[u] = dep[p] + 1;
	vector<int>a;
	for (int i = head[u] ; i ; i = edge[i].next) {
		int v = edge[i].to, x;
		if (v == p) continue;
		x = dfs(v, u);
		if (x) a.push_back(x);
	}
	sort(a.begin(), a.end(), cmp);
	int pos = 0, len = a.size(), res = 0;
	if (!c[u]) res = u;
	while(pos < len) {
		vector<int>stk;
		stk.push_back(a[pos]);
		while(pos + 1 < len && dep[a[pos + 1]] == dep[a[pos]]) {
			pos++;
			stk.push_back(a[pos]);
		}
		int tot = stk.size();
		for (int i = 0 ; i < tot ; i += 2) ans.push_back({stk[i], stk[i + 1]});
		if (tot&1) {
			if (res) {
				jud = 1;
				return 0;
			}
			res = stk[pos - 1];
		}
		pos++;
	}
	return res;
}
int dfs1(int u, int p) {
	if (jud) return 0;
	vector<int>a[2];
	if (c[u] > 0) a[c[u] - 1].push_back(u);
	for (int i = head[u] ; i ; i = edge[i].next) {
		int v = edge[i].to, x;
		if (dep[v] < dep[u] || use[v]) continue;
		x = dfs1(v, u);
		if (x && c[x] > 0) a[c[x] - 1].push_back(x);
	}
	sort(a[0].begin(), a[0].end(), cmp);
	sort(a[1].begin(), a[1].end(), cmp);
	int len0 = a[0].size(), len1 = a[1].size(), res = 0, i, j;
	for (i = 0, j = len1 - 1 ; i < len0 && j >= 0 ; i++, --j) {
		while(j - 1 >= 0 && dep[a[0][i]] <= dep[a[1][j]]) {
			if (!res) res = a[1][j];
			else {
				jud = 1;
				return 0;
			}
			j--;
		}
		ans.push_back({a[0][i], a[1][j]});
	}
	if (len0 - i + j + (res > 0) + 1 > 1) {
		jud = 1;
		return 0;
	}
	if (i < len0) res = a[0][i];
	if (j >= 0) res = a[1][j];
	return res;
}
void solve() {
	cin >> n;
	for (int i = 1 ; i <= n ; ++i) c[i] = -1;
	for (int i = 1 ; i < n ; ++i) {
		int x, p;
		cin >> x >> p >> ty;
		fa[x] = p;
		if (ty[0] != '-') {
			if (ty[0] == 'T') c[x] = 0;
			if (ty[0] == 'C') c[x] = 1;
			if (ty[0] == 'D') c[x] = 2; 
		}
		addedge(x, p);
		addedge(p, x);
	}
	dfs(1, 0);
	for (auto [x, y] : ans) {
		while(x != y) {
			if (dep[x] > dep[y]) use[x] = 1, x = fa[x];
			else use[y] = 1, y = fa[y];
		}
	}
	use[1] = 1;
	for (int i = 1 ; i <= n ; ++i) {
		if (use[i]) dfs1(i, 0);
	}
	if (jud) {
		cout << "NO" << '\n';
		return;
	}
	cout << "YES" << '\n';
	for (auto [x, y] : ans) cout << x << ' ' << y << '\n';
}
signed main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

詳細信息

Test #1:

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

input:

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

output:

YES
5 4
8 6

result:

ok Correct.

Test #2:

score: 0
Accepted
time: 0ms
memory: 5720kb

input:

10
2 1 Duan
3 2 Duan
4 2 -
5 4 Chang
6 2 Chang
7 1 Duan
8 6 Tong
9 6 Tong
10 3 Chang

output:

YES
9 8
10 3
6 2
5 7

result:

ok Correct.

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 7768kb

input:

2
2 1 Tong

output:

YES
2 0

result:

wrong answer Only odd number of marked vertices to match.