QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#101628 | #6381. LaLa and Harvesting | zhoukangyang# | RE | 0ms | 0kb | C++17 | 4.1kb | 2023-04-30 15:16:59 | 2023-04-30 15:17:04 |
Judging History
answer
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
#define uint unsigned int
#define ull unsigned long long
#define bs bitset < N >
using namespace std;
const int N = 1007, inf = 1e9 + 114514;
int n, m, k, T[N];
vi G[N], e[N];
int vis[N], dep[N];
inline void Dfs(int x) {
vis[x] = 1;
for(auto v : G[x]) if(!vis[v]) {
dep[v] = dep[x] + 1;
Dfs(v);
e[x].emplace_back(v);
}
}
int dp[N][2][2][2][2]; // x; su[x]; leftmost leaf; rightmost leaf;
int chs[N];
int su[N], sv[N];
inline void dfs(int x, int fa) {
for(auto v : e[x]) {
dfs(v, x);
if(sv[v] && sv[v] != x) {
su[x] = su[v];
sv[x] = sv[v];
}
}
for(auto t : G[x]) if(t != fa) {
if(dep[t] < dep[x]) {
if(su[x]) {
assert(false);
}
su[x] = x;
sv[x] = t;
}
}
}
int tu[N], tv[N], deg[N];
int good[N], top;
int ndp[2][2][2][2];
inline void work(int x) {
me(dp[x], -0x3f);
L(o, 0, 1) {
if(chs[x] != -1 && chs[x] != o) continue;
dp[x][o][su[x] == x ? o : 0][sz(e[x]) ? 0 : o][sz(e[x]) ? 0 : o] = T[x] * o;
}
L(d, 0, sz(e[x]) - 1) {
int cL = d == 0;
int v = e[x][d];
work(v);
me(ndp, -0x3f);
int cs = sv[v] && sv[v] != x;
L(xa, 0, 1)
L(xs, 0, 1)
L(xl, 0, 1)
L(xr, 0, 1) if(dp[x][xa][xs][xl][xr] >= 0)
L(va, 0, 1 - xa)
L(vs, 0, 1)
L(vl, 0, 1 - xr)
L(vr, 0, 1) if(dp[v][va][vs][vl][vr] >= 0) {
if(sv[v] == x && xa && vs) continue;
int &p = ndp[xa][cs ? vs : xs][cL ? vl : xl][vr];
p = max(p, dp[x][xa][xs][xl][xr] + dp[v][va][vs][vl][vr]);
}
swap(ndp, dp[x]);
}
}
inline int solve() {
work(1);
int mx = 0;
if(sz(e[1]) == 1) {
L(a, 0, 1) {
L(b, 0, 1) {
L(c, 0, 1) {
L(d, 0, 1) {
if(a && c) continue;
if(a && d) continue;
mx = max(mx, dp[1][a][b][c][d]);
}
}
}
}
} else {
L(a, 0, 1) {
L(b, 0, 1) {
L(c, 0, 1) {
L(d, 0, 1) {
if(c && d) continue;
mx = max(mx, dp[1][a][b][c][d]);
}
}
}
}
}
return mx;
}
vector < pair < int, int > > edges;
int nig[N];
int main() {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
L(i, 1, n) {
cin >> T[i];
}
L(i, 1, m) {
int u, v;
cin >> u >> v;
++u, ++v;
su[i] = u;
sv[i] = v;
G[u].emplace_back(v);
G[v].emplace_back(u);
edges.emplace_back(u, v);
}
dep[1] = 1, Dfs(1), dfs(1, 0);
cin >> k;
L(i, 1, k) {
cin >> tu[i] >> tv[i];
++tu[i], ++tv[i];
edges.emplace_back(tu[i], tv[i]);
++deg[tu[i]];
++deg[tv[i]];
}
L(i, 1, n) if(deg[i] >= 1) {
if(deg[i] > 1 || k <= 1)
good[++top] = i;
}
int ans = -1;
L(sub, 0, (1 << top) - 1) {
L(i, 1, n) chs[i] = -1;
L(i, 1, top)
chs[good[i]] = (sub >> (i - 1) & 1);
int ok = 1;
for(auto e : edges) {
int x = e.first;
int y = e.second;
if(chs[x] == 1 && chs[y] == 1)
ok = 0;
else if(chs[x] == 1) chs[y] = 0;
else if(chs[y] == 1) chs[x] = 0;
}
if(ok) {
int nsolve = solve();
if(nsolve > ans) {
ans = nsolve;
L(i, 1, n)
nig[i] = chs[i];
}
}
}
L(i, 1, n) if(nig[i] == -1) {
L(i, 1, n) {
chs[i] = nig[i];
}
chs[i] = 0;
int ok = 1;
for(auto e : edges) {
int x = e.first;
int y = e.second;
if(chs[x] == 1 && chs[y] == 1) ok = 0;
else if(chs[x] == 1) chs[y] = 0;
else if(chs[y] == 1) chs[x] = 0;
}
// cout << "solve = " << ok << ' ' << solve() << endl;
if(ok && solve() == ans) {
nig[i] = 0;
} else {
nig[i] = 1;
}
// L(i, 1, n) {
// cout << nig[i] << ' ';
// }
// cout << endl;
}
// L(i, 1, n) {
// cout<<nig[i];
// }
// cout<<endl;
vi pos;
L(i, 1, n)
if(nig[i] == 1)
pos.emplace_back(i);
cout << ans << ' ' << sz(pos) << '\n';
for(auto u : pos)
cout << u - 1 << ' ';
cout << '\n';
return 0;
}
/*
6 7
1 1 1 1 1 1
0 1
1 2
2 3
2 4
1 5
1 4
1 3
1
2 5
*/
详细
Test #1:
score: 0
Dangerous Syscalls
input:
6 7 1 1 1 1 1 1 0 1 1 2 2 3 2 4 1 5 1 4 0 5 1 2 5