QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#69589#2443. Dense Subgraphhe_ren_shi_lyp#AC ✓488ms28404kbC++233.2kb2022-12-28 20:57:172022-12-28 20:57:19

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-28 20:57:19]
  • 评测
  • 测评结果:AC
  • 用时:488ms
  • 内存:28404kb
  • [2022-12-28 20:57:17]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 50;
const int B = 6;
const int mod = 1e9 + 7;
const int inv2 = (mod +	1) / 2;
const int inf = 0x3f3f3f3f;
 
inline int inc(int x, int y) { x += y - mod; return x + (x >> 31 & mod); }
inline int dec(int x, int y) { x -= y; return x + (x >> 31 & mod); }  
inline int mul(int x, int y) { return 1ll * x * y % mod; }
auto inc(const auto &first, const auto ...args) {
	return inc(first, inc(args...));
}
auto mul(const auto &first, const auto ...args) {
	return mul(first, mul(args...));
}
 
inline void upd(int &x, int y) { x = inc(x, y); }
inline void dpu(int &x, int y) { x = dec(x, y); }
inline void pud(int &x, int y) { x = mul(x, y); }
 

int n, X, a[N], fa[N], dp[N][3];
bool ban[N][1 << B][2];
vector<int> G[N], sons[N];

void dfs(int x, int f) {
	fa[x] = f;
	for (int y : G[x]) if (y != f) {
		sons[x].push_back(y);
		dfs(y, x);
	}
	for (int mask = 0; mask < (1 << sons[x].size()); ++mask) {
		for (int o_fa : {0, 1}) {
			int sum = 0, cnt = 0;
			for (int i = 0; i < sons[x].size(); ++i)
				if (mask >> i & 1) {
					sum += a[sons[x][i]];
					++cnt;
				}
			if (o_fa) {
				sum += a[fa[x]];
				++cnt;
			}
			sum += a[x];
			++cnt;
			if (cnt > 1 && sum > X * cnt) {
				ban[x][mask][o_fa] = true;
			}
		}
	}
}

void solve(int x) {
	for (int y : sons[x]) {
		solve(y);
	}
	// father is chosen
	// 0: x is chosen, fa[x] is NOT chosen
	// 1: x is chosen, fa[x] is chosen
	// 2: x is not chosen
	for (int o : {0, 1}) {
		if (x == 1 && o == 1) continue;
		for (int mask = 0; mask < (1 << sons[x].size()); ++mask) {
			if (ban[x][mask][o])
				continue;
			int ways = 1;
			for (int i = 0; i < sons[x].size(); ++i)
				if (mask >> i & 1) {
					pud(ways, dp[sons[x][i]][1]);
				}
				else {
					pud(ways, dp[sons[x][i]][2]);
				}
			upd(dp[x][o], ways);
		}
	}
	int ways = 1;
	for (int y : sons[x]) {
		pud(ways, inc(dp[y][0], dp[y][2]));
	}
	dp[x][2] = ways;
	//printf("dp[%d] = %d %d %d\n", x, dp[x][0], dp[x][1], dp[x][2]);
}	


void init() {
	for (int x = 1; x <= n; ++x) {
		for (int i = 0; i < sons[x].size(); ++i) {
			for (int mask = 0; mask < (1 << sons[x].size()); ++mask) {
				if (mask >> i & 1) {
					ban[x][mask][0] |= ban[x][mask ^ (1 << i)][0];
					ban[x][mask][1] |= ban[x][mask ^ (1 << i)][1];
				}
			}
		}
		for (int mask = 0; mask < (1 << sons[x].size()); ++mask) {
			ban[x][mask][1] |= ban[x][mask][0];
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> X;
		for (int i = 1; i <= n; ++i) {
			cin >> a[i];
		}
		for (int i = 1; i < n; ++i) {
			int x, y;
			cin >> x >> y;
			G[x].push_back(y);
			G[y].push_back(x);
		}
		dfs(1, 0);
		init();
		/*for (int x = 1; x <= n; ++x) {
			for (int o : {0, 1}) {
				for (int mask = 0; mask < (1 << sons[x].size()); ++mask) {
					printf("ban[%d][%d][%d] = %d\n", x, mask, o, ban[x][mask][o]);
				}
			}
		}*/
		solve(1);
		cout << inc(dp[1][0], dp[1][2]) << '\n';
		memset(ban, 0, sizeof ban);
		memset(dp, 0, sizeof dp);
		for (int i = 1; i <= n; ++i) {
			G[i].clear();
			sons[i].clear();
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 488ms
memory: 28404kb

input:

30
10 11086
10189 24947 2265 9138 27104 12453 15173 3048 30054 2382
8 1
1 4
5 10
10 4
3 5
2 10
9 7
6 10
7 1
15 9664
4127 24649 13571 8586 34629 8644 3157 33133 3713 32646 29412 8108 13583 21362 23735
14 9
7 1
15 12
10 15
2 6
3 11
9 1
1 11
6 12
4 10
13 15
8 15
12 11
5 3
20 29310
21738 9421 8412 4617 ...

output:

320
3312
1048576
60461799
663660496
831386303
528321417
945912389
820338255
58962594
643816787
354510769
532631871
280169661
533022884
475656636
892230988
381315031
40006857
376652471
288541404
546717513
976236489
615823216
419541488
840899550
546169620
634600456
825179565
169850560

result:

ok 30 lines