QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#62228#4539. Independent Feedback Vertex Setqinjianbin#AC ✓1027ms85996kbC++172.5kb2022-11-17 17:23:472022-11-17 17:23:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-17 17:23:49]
  • 评测
  • 测评结果:AC
  • 用时:1027ms
  • 内存:85996kb
  • [2022-11-17 17:23:47]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<map>

using namespace std;

typedef long long LL;

const int maxn = 1e6+5; 

int n;
int a[maxn];

map<int,int> num[maxn]; 

struct edgeType
{
	int u,v;
	edgeType(){}
	edgeType(int _u,int _v):u(_u),v(_v){}
}edge[maxn];
int cntEdge; 

int hp[maxn];
int he[maxn];
struct OppositeType
{
	int to,nxt;
	void add_connect(int u,int v,int x,int *head)
	{
		to=v;
		nxt=head[u];
		head[u]=x;
	}
}pt[maxn],eg[maxn];
int cntConnect;

void standing_by()
{
	int i;
	int u,v,w;
	
	scanf("%d",&n);
	for(i=1;i<=n;i++) scanf("%d",&a[i]);
	for(i=1;i<=3*n-6;i++) hp[i]=he[i]=-1;
	
	edge[1]=edgeType(2,3);
	edge[2]=edgeType(1,3);
	edge[3]=edgeType(1,2);
	
	pt[1].add_connect(1,1,1,hp);
	pt[2].add_connect(2,2,2,hp);
	pt[3].add_connect(3,3,3,hp);
	
	eg[1].add_connect(1,1,1,he);
	eg[2].add_connect(2,2,2,he);
	eg[3].add_connect(3,3,3,he);
	
	num[2][3]=num[3][2]=1;
	num[1][3]=num[3][1]=2;
	num[1][2]=num[2][1]=3;
	
	cntEdge=3;
	cntConnect=3;
	for(i=4;i<=n;i++)
	{
		scanf("%d%d",&u,&v);
		w=num[u][v];
		
		edge[cntEdge+1]=edgeType(u,i);
		edge[cntEdge+2]=edgeType(i,v);
		num[u][i]=num[i][u]=cntEdge+1;
		num[v][i]=num[i][v]=cntEdge+2;
		
		
		pt[cntConnect+1].add_connect(w,i,cntConnect+1,hp);
		eg[cntConnect+1].add_connect(i,w,cntConnect+1,he);
		
		pt[cntConnect+2].add_connect(cntEdge+1,v,cntConnect+2,hp);
		eg[cntConnect+2].add_connect(v,cntEdge+1,cntConnect+2,he);
		
		pt[cntConnect+3].add_connect(cntEdge+2,u,cntConnect+3,hp);
		eg[cntConnect+3].add_connect(u,cntEdge+2,cntConnect+3,he);
		cntEdge+=2;
		cntConnect+=3;
	}
}

bool fp[maxn];
bool fe[maxn];

LL cur;

void dfs(int x,bool isPoint)
{
	int i;
	
	if (isPoint)
	{
		fp[x]=true;
		cur+=a[x];
		for(i=he[x];~i;i=eg[i].nxt)
		{
			if (fe[eg[i].to]) continue;
			dfs(eg[i].to,false);
		}
	}else {
		fe[x]=true;
		for(i=hp[x];~i;i=pt[i].nxt)
		{
			if (fp[pt[i].to]) continue;
			dfs(pt[i].to,true);
		}
	}
}

void complete()
{
	int i;
	LL ans;
	for(i=1;i<=n;i++) fp[i]=false;
	for(i=1;i<=cntEdge;i++) fe[i]=false;
	cur=0;
	dfs(1,true);
	ans=cur;
	
	for(i=1;i<=n;i++) fp[i]=false;
	for(i=1;i<=cntEdge;i++) fe[i]=false;
	cur=0;
	dfs(2,true);
	ans=max(ans,cur);
	
	for(i=1;i<=n;i++) fp[i]=false;
	for(i=1;i<=cntEdge;i++) fe[i]=false;
	cur=0;
	dfs(3,true);
	ans=max(ans,cur);
	
	printf("%lld\n",ans);
	
	for(i=1;i<=n;i++) num[i].clear();
}

int main()
{
	int t;
	scanf("%d",&t);
	for(;t;t--)
	{
		standing_by();
		complete();
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1027ms
memory: 85996kb

input:

1000
1561
14 78 18 74 80 97 70 49 39 81 79 65 55 1 29 32 91 73 94 41 69 41 79 34 62 81 25 33 76 26 57 69 66 48 37 78 67 50 21 8 27 58 58 32 8 42 31 52 35 36 99 56 61 27 4 64 50 59 43 55 73 53 41 80 94 32 56 59 19 57 28 56 35 73 70 13 18 6 96 47 71 29 16 73 65 27 61 68 50 11 21 96 19 18 33 97 6 43 19...

output:

26477
730
133872218725
206
139
26838
225784601561
409
163907525588
65
27568981002
26830437
612
206
647
3808763
392
39765006630
9823066
71309193
126
10667
93591615
12028
113
12356789
2420359
213
418
223
166
40
5324
219
1419
562
663
22941
14
39
102638547
14199134
58
201
12653791
101
10447
16511
171245...

result:

ok 1000 lines