QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#620202#2443. Dense Subgraphuntitledtwo#AC ✓245ms13948kbC++172.4kb2024-10-07 16:59:282024-10-07 16:59:51

Judging History

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

  • [2024-10-07 16:59:51]
  • 评测
  • 测评结果:AC
  • 用时:245ms
  • 内存:13948kb
  • [2024-10-07 16:59:28]
  • 提交

answer

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
typedef long long ll;
#define int ll
int a[35010];
struct Edge
{
	int to, nxt;
} e[70010];
int head[35010], ecnt = 0;
inline void addedge(int from, int to)
{
	e[ecnt].to = to;
	e[ecnt].nxt = head[from];
	head[from] = ecnt++;
}
int deg[35010], fa[35010], son[35010][4], scnt[35010];
int st[35010][16]; // 0: very safe, 1: half safe, 2: g
inline void dfs(int u)
{
	for(int i = head[u]; i != -1; i = e[i].nxt)
	{
		int v = e[i].to;
		if(v == fa[u])
			continue;
		son[u][scnt[u]++] = v, fa[v] = u;
		dfs(v);
	}
}
int dp[35010][3]; // 0: not chosen, 1: cannot choose fa, 2: can choose fa
inline void dfs2(int u)
{
	int m = scnt[u];
	for(int i = 0; i < m; i++)
		dfs2(son[u][i]);
	dp[u][0] = 1, dp[u][1] = dp[u][2] = 0;
	for(int i = 0; i < m; i++)
	{
		int v = son[u][i];
		dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1] + dp[v][2]) % mod;
	}
	for(int i = 0; i < (1 << m); i++)
	{
		if(st[u][i] == 2)
			continue;
		int res = 1;
		for(int j = 0; j < m; j++)
		{
			int v = son[u][j];
			if((i >> j) & 1)
				res = res * dp[v][2] % mod;
			else
				res = res * dp[v][0] % mod;
		}
		if(st[u][i])
			dp[u][1] = (dp[u][1] + res) % mod;
		else
			dp[u][2] = (dp[u][2] + res) % mod;
	}
}
signed main()
{
	int T;
	scanf("%lld", &T);
	while(T--)
	{
		int n, x;
		scanf("%lld %lld", &n, &x);
		for(int i = 1; i <= n; i++)
			scanf("%lld", &a[i]), a[i] -= x;
		for(int i = 1; i <= n; i++)
			head[i] = -1, deg[i] = 0, scnt[i] = 0;
		ecnt = 0;
		for(int i = 1; i < n; i++)
		{
			int u, v;
			scanf("%lld %lld", &u, &v);
			addedge(u, v), addedge(v, u);
			deg[u]++, deg[v]++;
		}
		int rt = 1;
		for(int i = 1; i <= n; i++)
			if(deg[i] < deg[rt])
				rt = i;
		fa[rt] = rt;
		dfs(rt);
		for(int i = 1; i <= n; i++)
		{
			int m = scnt[i];
			for(int j = 0; j < (1 << m); j++)
			{
				int sum = a[i];
				for(int k = 0; k < m; k++)
					if((j >> k) & 1)
						sum += a[son[i][k]];
				if(sum > 0 && j)
					st[i][j] = 2;
				else if(i != rt && sum + a[fa[i]] > 0)
					st[i][j] = 1;
				else
					st[i][j] = 0;
			}
			for(int j = 0; j < (1 << m); j++)
				for(int k = 0; k < (1 << m); k++)
					if((j & k) == j)
						st[i][k] = max(st[i][k], st[i][j]);
		}
		dfs2(rt);
		int ans = (dp[rt][0] + dp[rt][1] + dp[rt][2]) % mod;
		printf("%lld\n", ans);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 245ms
memory: 13948kb

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