QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#408194#6705. Mediancur_nxtWA 3ms3692kbC++201.9kb2024-05-09 19:59:392024-05-09 19:59:40

Judging History

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

  • [2024-05-09 19:59:40]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3692kb
  • [2024-05-09 19:59:39]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 210;
int p[N];

int find(int x)
{
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int T;
	cin >> T;
	
	while(T --)
	{
		int n,m;
		cin >> n >> m;
		for(int i = 1;i <= n;i ++) p[i] = i;
		
		bool flag = true;
		vector<vector<int>> g(n + 1), g2(n + 1);
		vector<int> in(n + 1), pre(n + 1,-1), suf(n + 1,-1), out(n + 1);
		for(int i = 1;i <= m;i ++)
		{
			int u,v;
			cin >> u >> v;
			g[u].push_back(v);
			g2[v].push_back(u);
			in[v] ++;
			out[u] ++;
			p[find(v)] = find(u);
		}
		
		vector<int> in2 = in;
		queue<int> p;
		for(int i = 1;i <= n;i ++)
		{
			if(!in2[i]) p.push(i);
		}
		
		while(p.size())
		{
			int t = p.front();
			p.pop();
			for(auto x : g[t]) 
			{
				in2[x] --;
				if(!in2[x]) p.push(x);
			}
		}
		
		for(int i = 1;i <= n;i ++)
			if(in2[i]) flag = false;
		
		std::function<int(int)> dfs = [&](int cur)
		{
			if(pre[cur] != -1) return pre[cur];
			
			int res = 0;
			for(auto x : g[cur])
			{
				res += dfs(x);
			}
			return pre[cur] = res + 1;
		};
		
		std::function<int(int)> dfs2 = [&](int cur)
		{
			if(suf[cur] != -1) return suf[cur];
			
			int res = 0;
			for(auto x : g2[cur])
			{
				res += dfs2(x);
			}
			return suf[cur] = res + 1;
		};
		
		if(!flag) 
		{
			for(int i = 0;i < n;i ++) cout << 0;
			cout << '\n';
		}
		else 
		{
			std::string ans(n, '0');
			for(int i = 1; i <= n; i++)
				if(in[i] == 0)
					dfs(i);
			for(int i = 1; i <= n; i++)
				if(out[i] == 0)
					dfs2(i);
//			for(int i = 1; i <= n; i++)
//				std::cout << pre[i] << ' ' << suf[i] << endl;
			for(int i = 1; i <= n; i++)
				if(pre[i] - 1 <= n / 2 && suf[i] - 1 <= n / 2)
					ans[i - 1] = '1';
			std::cout << ans << endl;
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3ms
memory: 3692kb

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
00000000111
111
11111111111
111111111111111
000000000000000
00000
01000
1111111
0000000000000
111000101
111111111
000000001010000
010111111
000000000
0000000000000
1111111111111
000000000000000
00000101001
000000000000000
11111111111
00000000000
11111
00000000000
11101110111
00000
1111...

result:

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