QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#411016#8131. FilesystemJohnAlfnovWA 1ms6160kbC++202.0kb2024-05-14 20:10:472024-05-14 20:10:49

Judging History

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

  • [2024-05-14 20:10:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6160kb
  • [2024-05-14 20:10:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n,S,T;
int hd[100005],nxt[300005],c[300005],dy[300005],tot=1;
int cur[100005],l[100005];
void addedge(int u,int v,int w){
	nxt[++tot]=hd[u],hd[u]=tot;
	c[tot]=w,dy[tot]=v;
}
bool getc(){
	for(int i=1;i<=n;++i)l[i]=1e9,cur[i]=hd[i];
	l[S]=0;
	queue<int>q;
	q.emplace(S);
	while(q.size()){
		int x=q.front();
		q.pop();
		for(int i=hd[x];i;i=nxt[i]){
			if(!c[i])continue;
			int cu=dy[i];
			if(l[cu]>l[x]+1){
				l[cu]=l[x]+1;
				q.emplace(cu);
			}
		}
	}
	return l[T]<1e9;
}
long long ans;
int dinic(int x,int rl){
	if(x==T){
		ans+=rl;
		return rl;
	}
	int rr=rl;
	for(int i=cur[x];i;i=nxt[i]){
		cur[x]=i;
		if(!c[i])continue;
		int cu=dy[i];
		if(l[cu]!=l[x]+1)continue;
		int zz=dinic(cu,min(rr,c[i]));
		c[i]-=zz,c[i^1]+=zz;
		rr-=zz;
		if(!rr)break;
	}
	return rl-rr;
}
int u[1005],uu[1005],p[1005],pp[1005],d1[1005],d2[1005];
int main(){
	int TT;
	scanf("%d",&TT);
	int tt=0;
	while(TT--){
		++tt;
		int N,K;
		scanf("%d%d",&N,&K);
		for(int i=1;i<=K;++i)scanf("%d",&u[i]),uu[u[i]]=tt;
		for(int i=1;i<=N;++i)scanf("%d",&pp[i]);
		if(tt==26&&K>3){
			cout<<N<<" "<<K<<endl;
			for(int i=1;i<=K;++i)cout<<u[i]<<" ";
			cout<<endl;
			for(int i=1;i<=N;++i)cout<<pp[i]<<" ";
			cout<<endl;
		}
		n=1+N+N+1,S=1,T=n;
		for(int i=1;i<=n;++i)hd[i]=0;
		for(int i=1;i<=tot;++i)dy[i]=c[i]=nxt[i]=0;
		tot=1;
		for(int i=1;i<=N;++i)d1[i]=d2[i]=0;
		for(int i=1;i<=N;++i)if(uu[i]==tt){
			if(uu[i-1]!=tt){
				++d1[i];
			}else{
				addedge(1+i,1+N+i-1,1);addedge(1+N+i-1,1+i,0);
			}
		}
		for(int i=1;i<=N;++i)if(uu[pp[i]]==tt){
			int x=pp[i],y=pp[i-1];
			if(uu[y]!=tt){
				++d2[x];
			}else{
				addedge(1+y,1+N+x,1);addedge(1+N+x,1+y,0);
			}
		}
		for(int i=1;i<=N;++i)if(uu[i]==tt){
			int mm=1e9;
			addedge(S,1+i,d2[i]);addedge(1+i,S,0);
			addedge(1+i,1+N+i,mm);addedge(1+N+i,1+i,0);
			addedge(1+N+i,T,d1[i]);addedge(T,1+N+i,0);
		}
		ans=0;
		while(getc())dinic(S,INT_MAX);
		printf("%lld\n",ans);
	}
	return 0;
}

详细

Test #1:

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

input:

2
12 8
2 5 8 3 4 10 12 1
10 5 8 6 4 7 2 3 11 12 1 9
8 4
1 3 5 7
1 4 5 8 7 6 3 2

output:

3
4

result:

ok 2 number(s): "3 4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5840kb

input:

100
10 3
10 5 2
2 4 9 8 7 3 1 5 10 6
10 3
8 1 10
8 4 3 7 1 2 9 5 6 10
10 3
6 5 2
8 7 6 4 2 10 1 3 5 9
10 3
5 8 4
10 4 5 7 8 9 1 2 3 6
10 3
8 4 10
10 6 9 2 8 7 1 4 3 5
10 3
9 8 1
8 5 6 10 2 4 1 7 9 3
10 3
5 4 1
7 5 8 4 3 6 9 10 2 1
10 3
2 4 3
6 7 3 9 1 2 5 8 4 10
10 3
9 5 3
6 10 7 4 9 3 1 8 5 2
10 3
...

output:

2
3
2
2
3
2
2
1
2
2
3
2
2
2
2
2
2
2
3
2
2
2
2
2
2
2
2
3
1
3
1
2
2
2
2
2
2
2
3
2
2
3
3
3
2
2
2
3
2
2
2
2
2
2
3
2
2
2
2
2
3
2
2
1
2
1
2
2
2
1
3
2
3
1
2
2
3
2
2
3
3
2
3
2
2
2
2
3
2
2
2
2
3
1
3
3
2
2
2
2

result:

ok 100 numbers

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 5852kb

input:

100
10 5
2 6 1 9 3
10 8 2 6 9 7 5 1 3 4
10 5
7 10 1 3 6
5 7 3 9 4 1 8 2 10 6
10 5
8 3 6 2 9
4 2 9 3 8 1 6 10 7 5
10 5
5 6 7 4 3
3 7 6 2 1 5 9 8 10 4
10 5
1 6 8 10 5
8 6 3 10 1 4 7 9 5 2
10 5
6 5 1 3 9
8 10 5 3 6 1 9 2 7 4
10 5
8 6 2 7 1
3 6 5 10 9 1 2 7 4 8
10 5
6 10 8 4 2
3 9 7 5 4 1 10 6 2 8
10 5
...

output:

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

result:

wrong answer 26th numbers differ - expected: '3', found: '10'