QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#275541#7769. Axium Crisisgrass8cow15 860ms110768kbC++173.4kb2023-12-04 20:19:192023-12-04 20:19:20

Judging History

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

  • [2023-12-04 20:19:20]
  • 评测
  • 测评结果:15
  • 用时:860ms
  • 内存:110768kb
  • [2023-12-04 20:19:19]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n;
int oi[18][18],wc[18];
vector<int>g[18];
#define pb push_back
struct ed{int S,k,u,v;};
vector<ed>st[1<<20];
int rb;
bool of[20][20][1<<17];
void dfs(int x,int f,int S,int u,int k){
	if(f!=-1&&!of[min(x,rb)][max(x,rb)][k])
	of[min(x,rb)][max(x,rb)][k]=1,st[u].pb((ed){S,k,rb,x});
	for(int v:g[x])if(v!=f){
		int w=oi[x][v];
		if(wc[w]!=1)dfs(v,x,S|(1<<w),u<<1,k);
		if(wc[w]!=0)dfs(v,x,S|(1<<w),u<<1|1,k|(1<<w));
	}
}
int js[1<<21],E,le[1<<21],sv[1<<21],lc[1<<21],jt[1<<21],jl[1<<21],jr[1<<21];
void pr(int x,int d){
	if(d==n)return;
	for(ed t:st[x])
		js[++E]=t.S,jt[E]=t.k,le[E]=d,sv[E]=x,jl[E]=t.u,jr[E]=t.v;
	pr(x<<1,d+1),pr(x<<1|1,d+1);
}
const int I=1e9;
short dp[1<<17][20];
vector<int>o[20];
void ad(short &x,short y){x=max(x,y);}
int jb(int u,int v){
	while(u!=v){
		if(u>v)swap(u,v);
		v>>=1;
	}
	int d=0;
	while(u>1)u>>=1,d++;
	return d;
}
int tx[20100000],ty[20100000],top;
short tz[20100000];int tt[20010000];
int U[20],V[20],cc;
void sol(){
	memset(oi,0,sizeof(oi));
	scanf("%d",&n);
	for(int i=0;i<(1<<n);i++)st[i].clear();
	for(int i=0;i<n;i++)g[i].clear();
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)for(int s=0;s<(1<<(n-1));s++)
	of[i][j][s]=0;
	for(int i=0,u,v;i+1<n;i++){
		scanf("%d%d%d",&u,&v,&wc[i]);
		oi[u][v]=oi[v][u]=i;
		g[u].pb(v),g[v].pb(u);
	}
	for(int i=0;i<n;i++)rb=i,dfs(i,-1,0,1,0);
	E=0,pr(1,0);n--;
	for(int s=0;s<(1<<n);s++)for(int i=0;i<=n;i++)dp[s][i]=-1;
	for(int i=0;i<=n;i++)o[i].clear();
	dp[0][0]=0,o[0].pb(0);
	top=0;
	cc=0;
	for(int i=1;i<=E;i++){
		if(i>1){
			lc[i]=((sv[i]==sv[i-1])?le[i]:jb(sv[i],sv[i-1]));
			for(int j=lc[i]+1;j<=n;j++)if(!o[j].empty()){
				for(int so:o[j]){
					if(dp[so][lc[i]]<dp[so][j]){
						tx[++top]=so,ty[top]=lc[i],tz[top]=dp[so][lc[i]];
						tt[top]=i*2-1;
						if(dp[so][lc[i]]==-1)o[lc[i]].pb(so);
						dp[so][lc[i]]=dp[so][j];
					}
					tx[++top]=so,ty[top]=j,tz[top]=dp[so][j],tt[top]=i;
					tt[top]=i*2-1;
					dp[so][j]=-1;
				}
				o[j].clear();
			}
		}
		int S=((1<<n)-1)^js[i],s2=S;
		while(1){
			for(int j=0;j<=n;j++)if(dp[s2][j]>=0){
				int zd=dp[s2][j]+le[i]-j;
				if(dp[js[i]|s2][le[i]]>=zd)continue;
				if(dp[js[i]|s2][le[i]]<0)o[le[i]].pb(js[i]|s2);
				tx[++top]=js[i]|s2,ty[top]=le[i],tz[top]=dp[js[i]|s2][le[i]];
				dp[js[i]|s2][le[i]]=zd;
				tt[top]=i*2;
			}
			if(!s2)break;s2=(s2-1)&S;
		}
	}
	int as=-1,ai=-1;
	short ans=-1;
	for(int s=0;s<(1<<n);s++)for(int i=0;i<=n;i++)if(ans<dp[s][i])
	ans=dp[s][i],as=s,ai=i;
	for(int i=E;i;i--){
		short fk=dp[as][ai];
		while(top&&tt[top]==i*2)dp[tx[top]][ty[top]]=tz[top],top--;
		bool fl=0;
		if(((as&js[i])==js[i])&&fk>dp[as][ai]){
			for(int o=0;o<=n;o++)if(dp[as^js[i]][o]+le[i]-o==fk){
				as^=js[i],ai=o;
				fl=1;break;
			}
		}
		if(fl){
			for(int j=0;j<n;j++)
			if((js[i]>>j)&1)wc[j]=((jt[i]>>j)&1);
			U[++cc]=jl[i],V[cc]=jr[i];
		}
		fk=dp[as][ai];
		while(top&&tt[top]==i*2-1)dp[tx[top]][ty[top]]=tz[top],top--;
		if(i==1)continue;
		if(ai<lc[i])continue;
		for(int j=lc[i];j<=n;j++)if(dp[as][j]==fk){ai=j;break;}
	}
	printf("%d\n",(int)ans+1);
	printf("%d\n",cc);
	for(int i=0;i<n;i++){
		if(wc[i]==2)wc[i]=0;
		printf("%d ",wc[i]);
	}
	puts("");
	for(int i=1;i<=cc;i++)printf("%d %d\n",U[i],V[i]);
}
int main(){
	int T,tt;
	puts("1");
	scanf("%d%d",&T,&tt);while(T--)sol();
	return 0;
}

詳細信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 5ms
memory: 60268kb

input:

1000 0
4
0 2 0
2 3 0
2 1 0
4
3 2 1
0 2 1
1 2 2
4
0 2 2
0 1 0
3 0 0
4
1 2 1
3 2 0
2 0 1
4
0 2 0
0 3 0
2 1 0
4
0 2 1
0 3 1
0 1 1
4
3 1 0
2 1 2
3 0 2
4
3 1 1
3 0 1
2 3 0
4
1 0 0
2 0 2
2 3 2
4
1 2 0
3 0 0
2 3 2
3
2 1 0
0 2 1
4
3 0 1
1 2 1
2 3 0
4
2 1 0
3 0 1
1 0 1
4
3 2 1
3 1 1
0 1 1
4
1 2 1
1 3 0
3 0 1...

output:

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

result:

ok Accepted. Good Job!

Test #2:

score: 10
Accepted
time: 4ms
memory: 62712kb

input:

1000 0
4
2 0 0
2 1 0
0 3 0
4
2 1 0
2 0 0
1 3 0
4
1 3 0
1 0 0
2 1 0
4
0 1 2
2 1 2
1 3 2
4
0 2 2
3 2 0
1 3 1
4
1 3 0
2 3 0
3 0 0
4
1 2 1
3 0 0
0 2 0
4
3 2 1
2 1 1
0 1 0
4
2 1 0
3 2 0
2 0 0
4
1 3 0
2 3 0
3 0 2
4
2 0 0
3 0 1
1 2 1
4
0 2 2
3 1 2
2 1 2
4
1 3 2
3 0 2
2 0 2
4
2 0 0
2 1 2
2 3 1
4
0 1 2
2 3 1...

output:

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

result:

ok Accepted. Good Job!

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 12ms
memory: 71576kb

input:

3000 3
4
0 1 1
0 3 1
0 2 0
4
3 2 0
0 1 1
1 2 0
4
1 0 0
2 3 1
3 1 0
4
2 1 0
2 0 1
3 0 0
4
2 3 1
3 0 1
2 1 0
4
2 3 1
2 1 1
2 0 1
4
0 2 0
1 0 0
3 0 0
4
3 1 1
0 2 0
2 3 0
6
4 0 0
3 1 1
2 3 0
0 5 1
1 5 0
4
2 3 1
3 0 0
3 1 1
4
0 3 0
1 2 0
0 2 1
4
0 2 1
3 1 0
2 1 1
4
2 0 0
2 3 1
1 3 0
6
3 1 0
3 4 1
4 0 1
2...

output:

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

result:

wrong answer Wrong Answer.

Subtask #3:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #2:

0%

Subtask #5:

score: 0
Skipped

Dependency #3:

0%

Subtask #6:

score: 0
Skipped

Dependency #4:

0%

Subtask #7:

score: 5
Accepted

Test #13:

score: 5
Accepted
time: 860ms
memory: 110768kb

input:

3000 1
11
2 5 0
10 2 0
6 2 0
2 8 0
0 2 0
2 1 0
2 4 0
2 9 0
2 3 0
7 2 0
11
7 8 0
6 4 0
1 6 0
2 8 0
8 0 0
6 3 0
9 5 0
5 8 0
1 2 0
9 10 0
8
1 4 0
2 3 0
6 5 0
6 7 0
2 4 0
7 3 0
1 0 0
8
4 0 0
0 5 0
7 2 0
0 2 0
0 6 0
0 1 0
0 3 0
11
5 1 0
7 2 0
9 2 0
4 9 0
0 2 0
8 5 0
0 6 0
3 6 0
4 10 0
1 7 0
7
6 2 0
0 5 0...

output:

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

result:

ok Accepted. Good Job!

Subtask #8:

score: 0
Runtime Error

Test #14:

score: 0
Runtime Error

input:

3000 2
8
4 7 2
4 3 2
3 2 2
4 5 2
1 4 2
6 4 2
0 1 2
8
1 5 2
0 7 2
3 2 2
3 1 2
5 7 2
4 0 2
6 4 2
8
1 3 2
5 3 2
7 6 2
2 6 2
0 7 2
4 6 2
0 5 2
8
5 7 2
2 6 2
1 6 2
4 5 2
4 0 2
0 1 2
7 3 2
11
2 7 2
0 9 2
8 9 2
10 7 2
6 9 2
9 3 2
4 10 2
7 5 2
7 9 2
1 9 2
8
2 6 2
1 5 2
4 1 2
1 3 2
6 1 2
0 1 2
6 7 2
14
2 6 2...

output:

1
7
2
0 0 0 1 0 0 0 
5 7
0 2
8
1
0 0 0 0 0 0 0 
2 6
8
2
0 0 0 1 0 0 0 
2 6
1 4
8
1
0 0 0 0 0 0 0 
2 3
9
4
0 1 1 0 0 0 0 1 0 1 
0 8
1 6
2 5
3 4
6
2
0 0 0 1 0 0 0 
3 5
2 4
7
4
1 0 1 0 1 0 0 1 0 0 0 0 0 
0 6
3 8
7 12
10 13
6
1
0 0 0 0 0 
0 5
6
2
0 0 0 0 1 0 
1 3
2 6
10
2
0 0 1 0 0 0 0 0 0 0 
0 8
3 7
8
...

result:


Subtask #9:

score: 0
Skipped

Dependency #6:

0%

Subtask #10:

score: 0
Skipped

Dependency #5:

0%