QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#149889#4934. Forbidden CardMegaSamWA 1ms9924kbC++142.6kb2023-08-25 14:29:312023-08-25 14:29:34

Judging History

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

  • [2023-08-25 14:29:34]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:9924kb
  • [2023-08-25 14:29:31]
  • 提交

answer

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e5+7,M=1e6+7;
int n,m,cnt,pos;
int a[N],b[N],ans[N],f[M],dep[M],fa[M];
vector<int> ve;
bool vis[M],is[M];
int find(int k){ return k==f[k]?k:f[k]=find(f[k]); }
void work2(){
	for(int i=1;i<=m;i++) vis[i]=0;
	is[0]=1;
	bool flag=0;
	int cc=0;
	for(int i=1;i<=n;i++){
		cc=i;
		if(vis[a[i]]&& vis[b[i]]) {flag=1;break;}
		else if(vis[a[i]]&&(!vis[b[i]])) {
			int tmp=b[i];
			while(!is[tmp]){
				ans[i]++;
				is[tmp]=1;
				tmp=fa[tmp];
			}
		}else if((!vis[a[i]])&&vis[b[i]]){
			int tmp=a[i];
			while(!is[tmp]){
				ans[i]++;
				is[tmp]=1;
				tmp=fa[tmp];
			}
		}else{
			int ra=find(a[i]),rb=find(b[i]);
			if(ra==rb){
				int tmp=dep[a[i]]>dep[b[i]]?b[i]:a[i];
				while(!is[tmp]){
					ans[i]++;
					is[tmp]=1;
					tmp=fa[tmp];
				}
			}
		}
		if(!vis[a[i]]) vis[a[i]]=1;
		else if(vis[a[i]] &&!vis[b[i]]) vis[b[i]]=1;
		if(find(a[i])!=find(b[i])){
			f[b[i]]=a[i];
			fa[b[i]]=a[i];
			dep[b[i]]=dep[a[i]]+1;
		}
	}
//	int sum=0;
//	for(int i=1;i<=n;i++) sum+=ans[i];
//	printf("%d\n%d",sum,cc);
	if(!flag){
		for(int i=1;i<=n;i++){
			if(vis[a[i]]&& vis[b[i]]) {break;}
			else if(vis[a[i]]&&(!vis[b[i]])) {
				int tmp=b[i];
				while(!is[tmp]){
					ans[i]++;
					is[tmp]=1;
					tmp=fa[tmp];
				}
			}else if((!vis[a[i]])&&vis[b[i]]){
				int tmp=a[i];
				while(!is[tmp]){
					ans[i]++;
					is[tmp]=1;
					tmp=fa[tmp];
				}
			}else{
				int ra=find(a[i]),rb=find(b[i]);
				if(ra==rb){
					int tmp=dep[a[i]]>dep[b[i]]?b[i]:a[i];
					while(!is[tmp]){
						ans[i]++;
						is[tmp]=1;
						tmp=fa[tmp];
					}
				}
			}
			if(!vis[a[i]]) vis[a[i]]=1;
			else if(!vis[b[i]]) vis[b[i]]=1;
			if(find(a[i])!=find(b[i])){
				f[b[i]]=a[i];
				fa[b[i]]=a[i];
				dep[b[i]]=dep[a[i]]+1;
			}
		}
	}
	int sum=0;
	for(int i=1;i<=n;i++)sum+=ans[i];
	ans[pos]=m-sum;
	for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
//	printf("%d\n",sum);
}
int main(){
//	freopen("marketplace.in","r",stdin);
//	freopen("marketplace.out","w",stdout);
	scanf("%d%d",&n,&m); cnt=m;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
	}
	for(int i=1;i<=m;i++) vis[i]=0,f[i]=i;
	for(int i=1;i<=n;i++){
		if(vis[a[i]] && vis[b[i]]){ pos=i;break;}
		else if(!vis[a[i]]) vis[a[i]]=1;
		else if(!vis[b[i]]) vis[b[i]]=1;
	}	
	if(!pos){
		for(int i=1;i<=n;i++){
			if(vis[a[i]] && vis[b[i]]){ pos=i;break;}
			else if(!vis[a[i]]) vis[a[i]]=1;
			else if(!vis[b[i]]) vis[b[i]]=1;
		}
	}
//	printf("%d %d\n",pos,cnt);
	if(!pos) pos=1;
//	ans[pos]=cnt;
//	if(n<=2000) work1();
//	else 
	work2();
}

詳細信息

Test #1:

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

input:

3 6
1 2
2 4
4 2

output:

3
0
3

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 7704kb

input:

4 10
1 5
2 6
3 7
4 8

output:

2
2
2
2

result:

wrong answer 1st lines differ - expected: '4', found: '2'