QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#566054#9319. Bull FarmwangjunruiTL 2ms9796kbC++143.6kb2024-09-15 22:56:472024-09-15 22:56:47

Judging History

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

  • [2024-09-15 22:56:47]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:9796kb
  • [2024-09-15 22:56:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2005;
constexpr int M = 1e6 + 5;
int n, m, q, tot;
int a[N][N];
int b[N][N];
char str[N * 2];
int belong[N], color;
int p[N];
struct Edge
{
	int next, to;
} edge[N * 2];
int head[N], num_edge;
inline void add_edge(int from, int to)
{
	from = belong[from];
	to = belong[to];
	if (from == to)
		return;
	edge[++num_edge].next = head[from];
	edge[num_edge].to = to;
	head[from] = num_edge;
//	printf("%d %d\n", from, to);
}
int dfn[N], low[N], dfstime;
int st[N], top;
inline void tarjan(int u)
{
	st[++top] = u;
	dfn[u] = low[u] = ++dfstime;
	for (int i = head[u]; i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (!dfn[v])
		{
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if (!belong[v])
			low[u] = min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u])
	{
		belong[u] = ++color;
		while (st[top] != u)
			belong[st[top--]] = color;
		--top;
	}
}
vector<int> g[N];
bitset<N> dp[N];
int in[N];
struct node
{
	int l, r, c, id;
	inline bool operator<(const node &rhs) const
	{
		return c < rhs.c;
	}
} c[M];
bool answer[M];
inline void work()
{
	cin >> n >> m >> q;
	for (int i = 1; i <= n; ++i)
		belong[i] = p[i] = i;
	color = n;
	for (int i = 1; i <= m; ++i)
	{
		cin >> str;
		for (int j = 0; j < n; ++j)
			a[i][j + 1] = (str[j * 2] - '0') * 50 + (str[j * 2 + 1] - '0');
		for (int j = 1; j <= n; ++j)
			b[i][j] = 0;
		for (int j = 1; j <= n; ++j)
			b[i][a[i][j]]++;
	}
	for (int i = 1; i <= q; ++i)
	{
		cin >> str;
		c[i].l = (str[0] - '0') * 50 + (str[1] - '0');
		c[i].r = (str[2] - '0') * 50 + (str[3] - '0');
		c[i].c = (str[4] - '0') * 50 + (str[5] - '0');
		c[i].id = i;
	}
	sort(c + 1, c + 1 + q);
	int now = 1;
	while (now <= q && c[now].c == 0)
	{
		int l = c[now].l, r = c[now].r, id = c[now].id;
		answer[id] = (l == r);
		++now;
	}
	for (int T = 1; T <= m; ++T)
	{
		int where = 0;
		for (int i = 1; i <= n; ++i)
			if (!b[T][i])
			{
				if (where)
				{
					where = -1;
					break;
				}
				else
					where = i; 
			}
		if (where != -1)
		{
			for (int u = 1, v; u <= n; ++u)
			{
				v = a[T][u];
				if (!where)
					add_edge(u, v);
				else if (b[T][v] == 2)
					add_edge(u, where);
			}
		}
		tot = color;
		
		color = 0;
		for (int i = 1; i <= tot; ++i)
			belong[i] = 0;
			
		for (int i = 1; i <= tot; ++i)
			if (!dfn[i])
				tarjan(i);
		for (int i = 1; i <= n; ++i)
			p[i] = belong[p[i]];
		
		for (int u = 1; u <= n; ++u)
		{
			for (int i = head[u]; i; i = edge[i].next)
			{
				int v = edge[i].to;
				if (belong[u] == belong[v])
					continue;
				g[belong[v]].push_back(belong[u]);
				++in[belong[u]];
			}
		}
		
		queue<int> que;
		for (int i = 1; i <= color; ++i)
		{
			dp[i].reset();
			dp[i][i] = true;
			if (!in[i])
				que.push(i);
		}
		while (!que.empty())
		{
			int u = que.front();
			que.pop();
			for (auto v : g[u])
			{
				dp[v] |= dp[u];
				if (!--in[v])
					que.push(v);
			}
		}
		while (now <= q && c[now].c == T)
		{
			int l = c[now].l, r = c[now].r, id = c[now].id;
//			printf("%d %d %d\n", now, p[l], p[r]);
			answer[id] = dp[p[l]][p[r]];
			++now;
		}
		num_edge = dfstime = 0;
		for (int i = 1; i <= tot; ++i)
			head[i] = dfn[i] = low[i] = 0;
		
		for (int u = 1; u <= color; ++u)
		{
			for (int v : g[u])
				add_edge(u, v);
			g[u].clear();
		}
	}
	for (int i = 1; i <= q; ++i)
		cout << answer[i];
	cout << '\n';
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T = 1;
	cin >> T;
	while (T--)
		work();
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9724kb

input:

2
5 2 4
0305040201
0404040404
030300
020500
050102
020501
6 2 4
030603010601
010203060504
030202
060402
050602
060401

output:

1011
0100

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 9796kb

input:

1
3 3 6
020202
030301
030201
020102
030203
010201
010303
020303
010202

output:

010101

result:

ok single line: '010101'

Test #3:

score: -100
Time Limit Exceeded

input:

200
10 10 5000
01060:04020305080709
0103070:060204050908
09070503080401060:02
050308010204090:0607
03010502040607080:09
03080109020504060:07
06050:09040302080107
07080305010409060:02
030809010:0204060507
0:060908070201050304
060700
090:03
09080:
070405
010703
0:0100
080601
030600
070206
0:0:09
08040...

output:


result: