QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#101624#6381. LaLa and Harvestingzhoukangyang#WA 2ms3584kbC++174.1kb2023-04-30 15:12:212023-04-30 15:12:24

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:12:24]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3584kb
  • [2023-04-30 15:12:21]
  • 提交

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] && 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);
	int leaf = 0;
	if(!sz(e[x])) leaf = 1;
	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][xs + vs * cs][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);
	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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3492kb

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: 3484kb

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: 0
Accepted
time: 2ms
memory: 3508kb

input:

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

output:

5 2
1 2 

result:

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

Test #5:

score: 0
Accepted
time: 1ms
memory: 3512kb

input:

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

output:

7 2
2 3 

result:

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

Test #6:

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

input:

6 6
5 1 1 3 3 4
0 3
1 3
2 3
2 4
4 5
0 5
1
4 5

output:

8 2
0 4 

result:

ok n=6, m=6, k=1, w=8

Test #7:

score: 0
Accepted
time: 1ms
memory: 3496kb

input:

7 9
4 2 1 1 2 4 4
0 3
0 5
2 3
2 6
4 5
1 3
0 4
0 1
3 6
1
5 6

output:

8 2
0 6 

result:

ok n=7, m=9, k=1, w=8

Test #8:

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

input:

8 10
3 3 4 3 5 1 5 3
4 5
5 7
1 7
0 7
2 7
3 7
2 6
0 3
6 7
4 7
1
5 6

output:

12 3
2 3 4 

result:

ok n=8, m=10, k=1, w=12

Test #9:

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

input:

9 10
5 5 2 2 4 1 4 3 2
1 4
0 2
0 5
3 5
4 5
5 7
6 7
7 8
5 8
0 1
1
3 4

output:

13 7
0 1 2 5 6 7 8 

result:

wrong answer The set contains adjacent nodes