2019CCSP A. 摘水果(拓扑排序)
看到数据量1e2,直接暴力即可。每一轮暴力检查两棵树上度为0的点,寻找最优答案输出,同时更新deg数组。
#include <bits/stdc++.h>
#define N 405
#define M 10005
using namespace std;
int n, head[N], ver[2 * M], Next[2 * M], tot = 0, deg[N], a[2 * N], b[2 * N];
void add(int x, int y) {
ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}
bool chosen[N];
int main() {
memset(deg, 0, sizeof(deg));
cin >> n;
for(int i = 1; i <= 2 * n; i++) {
cin >> a[i];
}
for(int i = 1; i <= 2 * n; i++) {
cin >> b[i];
add(i, b[i]);
deg[b[i]]++;
}
for(int t = 1; t <= n; t++) {
int val = 0, aa = 0, id1 = 0, id2 = 0;
for(int i = 1; i <= n; i++) {
for(int j = n + 1; j <= 2 * n; j++) {
if(!(deg[i] == 0 && deg[j] == 0 && chosen[i] == 0 && chosen[j] == 0)) continue;
int tmp = a[i] ^ a[j];
if(tmp > val) {
val = tmp;
aa = a[i];
id1 = i;
id2 = j;
} else if(tmp == val) {
if(a[i] > aa) {
aa = a[i];
id1 = i;
id2 = j;
} else if(a[i] == aa) {
if(i > id1) {
id1 = i;
id2 = j;
} else if(i == id1) {
if(j > id2) {
id2 = j;
}
}
}
}
}
}
cout << val << (t == n ? "" : " ");
//cout << id1 << " " << id2 << endl;
chosen[id1] = chosen[id2] = 1;
for(int i = head[id1]; i; i = Next[i]) {
int y = ver[i];
if(deg[y]) deg[y]--;
}
for(int i = head[id2]; i; i = Next[i]) {
int y = ver[i];
if(deg[y]) deg[y]--;
}
}
}
// 3
// 3 9 4 1 3 4
// 0 0 2 0 4 4
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!