QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#799871#7898. I Just Want... One More...Tom22lWA 6ms20092kbC++172.3kb2024-12-05 19:09:392024-12-05 19:09:39

Judging History

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

  • [2024-12-05 19:09:39]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:20092kb
  • [2024-12-05 19:09:39]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
int Read(){
	int x=0;
	char ch=getchar();bool f=0;
	while(ch<'0'||ch>'9') if(ch=='-')f=1,ch=getchar(); else if(ch==EOF)return 0; else ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
int h[800005],nxt[800005],to[800005],val[800005];
int cnt=1;
int n,m,s,t;
int ls[800005],lt[800005];
void link(int x,int y,int z){
	nxt[++cnt]=h[x];
	h[x]=cnt;
	to[cnt]=y;
	val[cnt]=z;
	if(x==2*n+1) ls[y]=cnt;
	if(y==2*n+2) lt[x]=cnt;
	nxt[++cnt]=h[y];
	h[y]=cnt;
	to[cnt]=x;
	val[cnt]=0;
	return;
}
const int inf=0x3f3f3f3f3f3f3f3f;
int dis[800005],now[800005];
queue<int> q;
bool bfs(){
	bool flag=0;
	for(int i=1;i<=2*n+2;i++) dis[i]=inf;
	q.push(s);
	dis[s]=0;
	now[s]=h[s];
	while(!q.empty()){
		int x=q.front();
//		cout<<x<<endl;
		q.pop();
		for(int i=h[x];i;i=nxt[i]){
			if(dis[to[i]]==inf&&val[i]>0){
				dis[to[i]]=dis[x]+1;
				now[to[i]]=h[to[i]];
				q.push(to[i]);
				if(to[i]==t) flag=1;
			}
		}
	}
	return flag;
}
int dfs(int x,int fl){
	if(x==t) return fl;
	int ans=0;
	for(int i=now[x];i&&fl;i=nxt[i]){
		now[x]=i;
		if(val[i]>0&&dis[to[i]]==dis[x]+1){
			int k=dfs(to[i],min(fl,val[i]));
			if(k==0) dis[to[i]]=inf;
			val[i]-=k;
			val[i^1]+=k;
			fl-=k;
			ans+=k;
		}
	}
	return ans;
}
bool vis1[800005];
void dfs(int x){
	vis1[x]=1;
	for(int i=h[x];i;i=nxt[i]){
		int p=to[i];
		if((!val[i])||vis1[p])continue;
		dfs(p);
	}return;
}
void dfs1(int x){
	vis1[x]=1;
	for(int i=h[x];i;i=nxt[i]){
		int p=to[i];
		if(val[i]||vis1[p])continue;
		dfs(p);
	}return;
}
bool ans[800005];
signed main() {
	int T=Read();
	while(T--){
		n=Read(),m=Read();
		for(int i=1;i<=2*n+2;i++)h[i]=0,ans[i]=0;
		while(m--){
			int x=Read(),y=Read();
			link(x,y+n,1);
		}
		for(int i=1;i<=n;i++){
			link(2*n+1,i,1);
		}for(int i=n+1;i<=2*n;i++){
			link(i,2*n+2,1);
		}
		s=2*n+1,t=2*n+2;
		int ans=0;
		while(bfs())ans+=dfs(s,inf);
//		cout<<ans<<endl;
		int ans1=0,ans2=0;
		for(int i=1;i<=2*n+2;i++)vis1[i]=0;
		dfs(2*n+1);
		for(int i=1;i<=n;i++) ans1+=vis1[i],vis1[i]=0;
		for(int i=n+1;i<=2*n+2;i++) vis1[i]=0;
		dfs1(2*n+2);
		for(int i=n+1;i<=2*n;i++) ans2+=vis1[i];
		printf("%lld\n",ans1*ans2);
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 20092kb

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: 18096kb

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
4
0
16
4
0
6
9
9
9
0
9
4
0
1
1
1
0
4
16
9
3
2
16
0
1
1
16
1
0
0
0
0
16
4
4
16
4
9
0
9
0
2
3
0
9
4
9
16
16
0
0
1
9
0
1
2
0
0
1
0
0
2
1
2
0
9
1
0
0
1
1
2
2
3
0
2
1
4
0
0
0
0
9
16
2
0
1
2
0
12
2
4
0
9
1
1
9
4
4
9
9
12
1
16
9
16
9
4
9
0
1
16
9
6
1
9
16
9
9
4
9
1
0
4
0
6
0
3
0
0
0
0
4
0
0...

result:

wrong answer 9th numbers differ - expected: '6', found: '4'