QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#166989#6738. CoverPhantomThreshold#TL 1ms32160kbC++202.8kb2023-09-06 22:17:332023-09-06 22:17:34

Judging History

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

  • [2023-09-06 22:17:34]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:32160kb
  • [2023-09-06 22:17:33]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define int long long
#define lowbit(x) ((x)&(-x))
using namespace std;

const int maxn = 100005;
const int maxm = 500005;
const int maxd = 18;
const int mask = 1<<11;
inline void up(int &a,const int &b){ if(a<b)a=b; }

int n,m,K;
vector<int>V[maxn];
int fa[maxn][maxd],dep[maxn],d[maxn];

void dfs(const int x)
{
	for(int i=1;i<maxd;i++) fa[x][i]=fa[ fa[x][i-1] ][i-1];
	for(auto y:V[x]) if(y!=fa[x][0])
	{
		d[x]++;
		dep[y]=dep[x]+1;
		fa[y][0]=x;
		dfs(y);
	}
}
int lca(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=maxd-1;i>=0;i--) if(dep[x]-dep[y]>=(1<<i))
		x=fa[x][i];
	if(x==y) return x;
	for(int i=maxd-1;i>=0;i--) if(fa[x][i]!=fa[y][i])
		x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}

int ty[maxm],ec[maxm];
vector<int>pr[maxm];

vector<int>Va[maxn],Vb[maxn];
set< pair<int,int> >S[maxn]; int flag[maxn];

int yid[20];
int f[mask],g[maxn];
void dp(const int x)
{
	for(auto y:V[x]) if(y!=fa[x][0])
	{
		dp(y);
	}
	for(auto i:Vb[x])
	{
		int idx=0;
		for(auto y:V[x]) if(y!=fa[x][0])
		{
			yid[idx]= y;
			auto it= S[y].lower_bound( make_pair(i,0) );
			if( it!=S[y].end() && (it->first)==i )
				pr[i].push_back(idx);
			idx++;
		}
	}
	
	memset(f,0,sizeof f);
	int uS = 1<<d[x];
	//f[x][0]=0;
	for(int s=1;s<uS;s++)
	{
		/*for(auto y:V[x]) if(y!=fa[x][0])
		{
			if(s>>idx&1) up(f[x][s], f[x][s^1<<idx]+f[y][(1<<d[y])-1]);
			idx++;
		}*/
		{
			int yi=31-__builtin_clz(s);
			int y= yid[yi];
			up(f[s], f[s^(1<<yi)]+g[y]);
		}
		
		for(auto i:Vb[x])
		{
			int ts=s;
			int ok=1;
			int temp= ec[i];
			for(auto j:pr[i])
			{
				if(ts>>j&1) 
				{
					ts^=1<<j;
					auto it= S[yid[j]].upper_bound( make_pair(i,0) );
					temp+= (it->second) + flag[yid[j]];
				}
				else ok=0;
			}
			temp+= f[ts];
			if(ok) up(f[s], temp);
		}
	}
	
	int idx=0;
	for(auto y:V[x]) if(y!=fa[x][0])
	{
		flag[y]+= f[(uS-1)^1<<idx];
		
		if( S[x].size()<S[y].size() )
			swap(S[x],S[y]),swap(flag[x],flag[y]);
		
		for(auto it:S[y])
		{
			S[x].insert( make_pair( it.first,it.second+flag[y]-flag[x] ) );
		}
		
		idx++;
	}
	
	for(auto i:Va[x])
	{
		S[x].insert( make_pair(i, f[uS-1]-flag[x]) );
	}
	g[x]=f[(1<<d[x])-1];
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin>>n>>m>>K;
	for(int i=1;i<n;i++)
	{
		int x,y; cin>>x>>y;
		V[x].push_back(y);
		V[y].push_back(x);
	}
	
	dep[1]=1; dfs(1);
	
	for(int i=1;i<=m;i++)
	{
		int ai,bi,wi; cin>>ai>>bi>>wi;
		ec[i]=wi;
		int ff=lca(ai,bi);
		if(ff==ai || ff==bi)
		{
			ty[i]=1;
			Va[ai^bi^ff].push_back(i);
			Vb[ff].push_back(i);
		}
		else
		{
			ty[i]=2;
			Va[ai].push_back(i);
			Va[bi].push_back(i);
			Vb[ff].push_back(i);
		}
	}
	
	dp(1);
	cout<<f[(1<<d[1])-1]<<endl;
	
	return 0;
}


详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 32160kb

input:

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

output:

19

result:

ok 1 number(s): "19"

Test #2:

score: -100
Time Limit Exceeded

input:

100000 500000 12
2 1
3 2
4 2
5 2
6 5
7 2
8 5
9 3
10 2
11 2
12 5
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 12
25 2
26 2
27 2
28 2
29 2
30 15
31 30
32 23
33 26
34 22
35 30
36 26
37 3
38 3
39 3
40 3
41 3
42 3
43 3
44 3
45 3
46 3
47 20
48 21
49 4
50 4
51 4
52 4
53 4
54 4
55 4
56 4
57 4
5...

output:


result: