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
标签:aa,2019CCSP,int,拓扑,id2,tot,id1,排序,deg 来源: https://www.cnblogs.com/lipoicyclic/p/15409940.html