QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#254028#6705. MedianLionel_ZQY#WA 0ms3628kbC++201.7kb2023-11-17 22:42:212023-11-17 22:42:21

Judging History

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

  • [2023-11-17 22:42:21]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3628kb
  • [2023-11-17 22:42:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

struct DSU{
	vector<int> f,siz;
	DSU(){}
	DSU(int n){
		init(n);
	}
	void init(int n){
		f.resize(n);
		iota(f.begin(),f.end(),0);
		siz.assign(n,1);
	}
	int find(int x){
		while(x!=f[x]) x=f[x]=f[f[x]];
		return x;
	}
	bool same(int x,int y) { return find(x)==find(y);}
	bool merge(int x,int y){
		x=find(x);
		y=find(y);
		if(x==y){
			return false;
		}
		siz[x]+=siz[y];
		f[y]=x;
		return true;
	}
	int size(int x) { return siz[find(x)];}
};

void solve(){
	int n,m;
	cin>>n>>m;
	DSU dsu(n+1);
	bool ok=true;
	vector<int> edge[n+1],siz(n+1);
	
	function<bool(int,int)> check=[&](int u,int v){
		bool flag=false;
		for(auto x:edge[u]){
			if(x==v){
				flag=true;
				return true;
			}
			check(x,v);
		}
		return flag;
	};

	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
		if(u==v) ok=false;
		else if(!dsu.same(u,v)){
			edge[u].push_back(v);
			dsu.merge(u,v);
		}
		else{
			if(check(v,u)) ok=false;
		}
	}
	vector<bool> vis(n+1);

	function<void(int)> dfs=[&](int u){
		vis[u]=true;
		siz[u]=1;
		for(auto x:edge[u]){
			if(!vis[x]) dfs(x);
			siz[u]+=siz[x];
		}
	};

	for(int i=1;i<=n;i++){
		if(!vis[i]){
			dfs(i);
		}
	}

	if(!ok){
		for(int i=1;i<=n;i++){
			cout<<0;
		}
		cout<<'\n';
		return ;
	}

	int k=(n+1)/2;
	for(int i=1;i<=n;i++){
		int cnt=n-dsu.size(i);
		// cout<<i<<' '<<siz[i]<<' '<<cnt<<'\n';
		if(siz[i]>k) cout<<0;
		else if(siz[i]==k) cout<<1;
		else{
			if(siz[i]+cnt>=k) cout<<1;
			else cout<<0;
		}
	}
	cout<<'\n';
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	int t;
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

01000
000

result:

ok 2 lines

Test #2:

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

input:

66
13 2
9 13
7 11
11 19
9 1
8 1
5 1
2 8
4 2
2 1
5 2
6 3
3 11
3 2
4 6
6 10
9 8
3 5
1 7
5 8
3 9
4 9
6 7
3 1
2 3
11 6
9 4
1 6
5 2
1 5
4 6
8 4
15 15
10 6
15 8
7 6
11 1
5 2
3 4
11 13
4 6
10 12
10 13
1 6
15 2
5 12
13 14
5 3
15 86
14 12
8 1
14 9
8 15
5 10
1 9
11 2
6 2
7 10
10 13
14 5
4 13
5 8
4 10
13 9
6 9...

output:

1111111111111
00100000000
111
10111011111
000000001000000
000000000000000
00100
00100
1111111
0000000000010
100100000
000010010
000000000000000
001100100
000000000
0000000000000
0100110010000
000000000000000
01000000001
000000000000000
11111111111
00000000000
11111
00000000000
00010000100
00000
1111...

result:

wrong answer 2nd lines differ - expected: '01001000111', found: '00100000000'