QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#101621#6381. LaLa and Harvestingzhoukangyang#WA 2ms3588kbC++173.5kb2023-04-30 15:01:462023-04-30 15:01:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-30 15:01:49]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3588kb
  • [2023-04-30 15:01:46]
  • 提交

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) {
	for(auto v : e[x]) {
		dfs(v);
		if(sv[v] != x) {
			su[x] = su[v];
			sv[x] = sv[v];
		}
	}
	for(auto t : e[x]) {
		if(dep[t] < dep[x]) {
			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] != 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(!cs && xa && vs) continue;
						int &p = ndp[xa][xs + vs * cs][cL ? xl : vl][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;
	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);
	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;
		}
		if(ok && solve() == ans) {
			nig[i] = 0;
		} else {
			nig[i] = 1;
		}
	}
	
	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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3492kb

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

output:

2 2
1 3 

result:

ok n=6, m=7, k=1, w=2

Test #2:

score: 0
Accepted
time: 2ms
memory: 3588kb

input:

2 1
2 5
0 1
1
0 1

output:

5 1
1 

result:

ok n=2, m=1, k=1, w=5

Test #3:

score: 0
Accepted
time: 2ms
memory: 3548kb

input:

3 3
1 1 2
1 2
0 1
0 2
1
1 2

output:

2 1
2 

result:

ok n=3, m=3, k=1, w=2

Test #4:

score: -100
Wrong Answer
time: 2ms
memory: 3488kb

input:

4 4
2 3 2 4
0 2
1 3
2 3
0 3
1
2 3

output:

5 2
0 1 

result:

wrong answer The set contains adjacent nodes