QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710660#7898. I Just Want... One More...frogmanWA 6ms9868kbC++142.0kb2024-11-04 20:56:122024-11-04 20:56:13

Judging History

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

  • [2024-11-04 20:56:13]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:9868kb
  • [2024-11-04 20:56:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define N 200010
int head[N],nxt[N<<1],v[N<<1],w[N<<1],now[N],d[N],vis[N];
int n,m,s,t,tot;
inline void add(int x,int y,int z){
	v[++tot]=y;w[tot]=z;
	nxt[tot]=head[x];head[x]=tot;
	v[++tot]=x;w[tot]=0;
	nxt[tot]=head[y];head[y]=tot;
}
inline int bfs(){
	queue<int> q;
	for(int i=0;i<=t;++i){
		d[i]=-1;
	}
	d[s]=0;
	now[s]=head[s];
	q.push(s);
	while(!q.empty()){
		int x=q.front();
		q.pop();
		for(int i=head[x];i;i=nxt[i])if(d[v[i]]==-1&&w[i]){
			q.push(v[i]);
			d[v[i]]=d[x]+1;
			now[v[i]]=head[v[i]];
			if(v[i]==t){
				return 1;
			}
		}
	}
	return 0;
}
int dfs(int x,int flow){
	if(x==t){
		return flow;
	}
	int k,res=0;
	for(int i=now[x];i&&flow;i=nxt[i]){
		now[x]=i;
		if(w[i]&&d[v[i]]==d[x]+1){
			k=dfs(v[i],min(flow,w[i]));
			if(!k){
				d[v[i]]=-1;
			}
			w[i]-=k;
			w[i^1]+=k;
			flow-=k;
			res+=k;
		}
	}
	return res;
}
void Dfs(int x,int y){
	if(vis[x]){
		return;
	}
	vis[x]=1;
	for(int i=head[x];i;i=nxt[i]){
		if(w[i]==y){
			Dfs(v[i],y);
		}
	}
}
inline void solve(){
	tot=1;
	cin>>n>>m;
	s=n+n+1;
	t=n+n+2;
	for(int i=1;i<=n;++i){
		add(s,i,1);
	}
	for(int i=n+1;i<=n+n;++i){
		add(i,t,1);
	}
	for(int i=1;i<=m;++i){
		int x,y;
		cin>>x>>y;
		add(x,y+n,1);
	}
	int ans=0;
	while(bfs()){
		ans+=dfs(s,0x3f3f3f3f);
	}
//	cout<<ans<<"!!!";
	for(int i=head[s];i;i=nxt[i]){
		if(!w[i]){
			continue;
		}
		Dfs(v[i],1);
	}
	int c1=0;
	for(int i=1;i<=n;++i){
		if(vis[i]){
			++c1;
		}
	}
	memset(vis,0,sizeof(int)*(n+n+1));
	for(int i=head[t];i;i=nxt[i]){
		if(w[i]){
			continue;
		}
		Dfs(v[i],0);
	}
	int c2=0;	
	for(int i=n+1;i<=n+n;++i){
		if(vis[i]){
			++c2;
		}
	}
	memset(vis,0,sizeof(int)*(n+n+1));
	memset(head,0,sizeof(int)*(n+n+3));
	cout<<1ll*c1*c2<<'\n';
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int T;
	cin>>T;
	while(T--){
		solve();
	}
}
/*
1
4 5
1 2
3 2
4 3
1 2
1 2
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
4 3
1 2
3 2
4 3
3 3
1 3
2 2
3 1
3 2
1 2
1 2

output:

6
0
4

result:

ok 3 number(s): "6 0 4"

Test #2:

score: -100
Wrong Answer
time: 6ms
memory: 9652kb

input:

10000
5 4
2 2
3 1
5 3
1 1
1 1
1 1
1 1
1 1
2 2
2 2
1 2
1 1
1 1
1 1
1 1
1 1
1 1
2 3
2 1
1 2
1 1
5 5
1 4
2 2
3 1
1 3
1 2
2 4
2 2
2 1
1 2
1 1
5 1
5 3
3 1
2 2
1 1
1 1
3 2
3 1
2 1
5 2
1 2
2 3
5 3
1 5
4 2
1 2
4 1
1 1
2 3
1 1
2 2
2 1
4 1
1 4
3 1
1 1
1 1
1 1
2 1
2 2
3 3
1 3
2 3
2 2
3 3
1 3
3 3
1 2
3 3
2 2
1 ...

output:

6
0
0
2
0
0
0
0
6
0
16
4
0
6
9
9
9
0
9
4
0
1
1
1
0
4
16
12
3
2
16
0
2
2
20
1
0
0
0
0
16
4
4
16
4
9
0
9
0
2
3
0
9
4
9
16
20
0
0
1
12
0
1
2
0
0
1
0
0
2
2
4
0
12
1
0
0
2
1
2
2
3
0
4
1
6
0
0
0
0
9
16
2
0
1
2
0
12
2
4
0
12
1
1
9
4
6
9
9
12
3
16
15
16
9
4
9
0
1
16
9
9
1
9
16
9
12
4
9
2
0
4
0
6
0
3
0
0
0
0...

result:

wrong answer 198th numbers differ - expected: '4', found: '2'