QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#863340#9677. 基础博弈练习题Tony210 626ms305788kbC++143.7kb2025-01-19 16:05:572025-01-19 16:05:57

Judging History

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

  • [2025-01-19 16:05:57]
  • 评测
  • 测评结果:10
  • 用时:626ms
  • 内存:305788kb
  • [2025-01-19 16:05:57]
  • 提交

answer

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 1e6 + 5;
int n, m, k;
int a[N], b[N], d[N], topo[N], id[N];
vector<int> pos[N], v2[N];
unordered_map<int, int> mp[N];
vector<int> e[N], e1[N], e2[N];
bool vis[N]; int state[N];
vector<int> cur[N], chk[N], toans[N];
int lt[N], rt[N];
int dfsnum, dfn[N], low[N], colnum, col[N], top, sta[N];
int T, lsttm[N], f[N], ans[N];//1 for win, 0 for lose
bool vis2[N];
void tarjan(int u){
	dfn[u] = low[u] = ++dfsnum;
	sta[++top] = u;
	for (int v : e[u])
		if (!dfn[v]){
			tarjan(v);
			low[u] = min(low[v], low[u]);
		}else if (!col[v]) low[u] = min(dfn[v], low[u]);
	if (dfn[u] == low[u]){
		colnum++;
		do{
			col[sta[top]] = colnum;
			mp[colnum][a[sta[top]]]++;
			pos[a[sta[top]]].push_back(colnum);
			v2[colnum].push_back(sta[top]);
		}while (sta[top--] != u);
	}
}
bool cmp(int u, int v){
	return id[u] > id[v];
}
void dfs(int u){
	if (state[u] == 2) return;
	cur[T].push_back(u);
	if (state[u] == 1){
		state[u] = 2;
		lsttm[u] = T;
		return;
	}
	state[u] = 2;
	toans[T].push_back(u);
	for (int v : e2[u])
		dfs(v);
}
void update(int u, int l, int r){
	bool flg = 0;
	if (r == 0) r = k + 1, flg = 1;
	int res = r;
	for (int i = l; i < r; i++){
		if (mp[u].find(b[i]) == mp[u].end()){
			res = i;
			break;
		}
		if (i != l && b[i] == b[i - 1] && mp[u][b[i]] == 1){
			res = i;
			break;
		}
	}
	int win = res == r ? (f[u] ^ ((r - l) & 1)) : (res - l) % 2;
	for (int x : v2[u])
		ans[x] = l + (win == 0);
	if (mp[u][b[l]] == 1){
		int p = -1;
		for (int x : v2[u])
			if (a[x] == b[l])
				p = x;
		int i = l;
		while (i < r && (b[i] == b[l] || mp[u].find(b[i]) == mp[u].end())) i++;
		if (i == r) ans[p] = flg ? 0 : r + (f[u] == 0);
		else{
			int win2 = res == r ? (f[u] ^ ((r - i) & 1)) : (res - i) % 2;
			ans[p] = i + (win2 == 0);
		}
	}
	f[u] = win;
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= k; i++) cin >> b[i];
	for (int i = 1; i <= m; i++){
		int u, v;
		cin >> u >> v;
		e[u].push_back(v);
	}
	for (int i = 1; i <= n; i++)
		if (!dfn[i])
			tarjan(i);
	for (int i = 1; i <= n; i++)
		for (int v : e[i])
			if (col[i] != col[v]){
				e1[col[i]].push_back(col[v]);
				e2[col[v]].push_back(col[i]);
			}
	queue<int> q;
	for (int i = 1; i <= colnum; i++){
		sort(e1[i].begin(), e1[i].end());
		e1[i].erase(unique(e1[i].begin(), e1[i].end()), e1[i].end());
		sort(e2[i].begin(), e2[i].end());
		e2[i].erase(unique(e2[i].begin(), e2[i].end()), e2[i].end());
		d[i] = e2[i].size();
		if (!d[i]) q.push(i);
	}
	int tot = 0;
	while (!q.empty()){
		int u = q.front(); q.pop();
		topo[++tot] = u;
		id[u] = tot;
		for (int v : e1[u])
			if (--d[v] == 0)
				q.push(v);
	}
	for (int i = 1; i <= k; i++)
		if (!vis[b[i]]){
			T = i;
			vis[b[i]] = 1;
			sort(pos[b[i]].begin(), pos[b[i]].end(), cmp);
			pos[b[i]].erase(unique(pos[b[i]].begin(), pos[b[i]].end()), pos[b[i]].end());
			for (int x : pos[b[i]])
				if (state[x] == 0){
					state[x] = 1;
					chk[i].push_back(x);
					cur[i].push_back(x);
					for (int v : e2[x]) dfs(v);
				}
		}
	for (int i = k; i >= 1; i--){
		sort(cur[i].begin(), cur[i].end(), cmp);
		for (int x : chk[i]) update(x, i, lsttm[x]);
		for (int x : cur[i]) vis2[x] = 1;
		for (int x : cur[i])
			if (f[x])
				for (int y : e2[x])
					if (vis2[y])
						f[y] = 1;
		for (int x : toans[i])
			for (int u : v2[x])
				ans[u] = i + (f[x] == 0);
		for (int x : cur[i]) vis2[x] = 0;
	}
	for (int i = 1; i <= n; i++) cout << (ans[i] == k + 1 ? -1 : ans[i] - 1) << ' ';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 35ms
memory: 268036kb

input:

83 93 13
8 9 10 7 7 7 6 3 1 10 6 2 5 7 1 3 4 2 1 10 7 4 8 9 2 2 1 9 2 5 1 7 8 6 1 9 9 10 4 1 2 9 2 3 4 2 9 10 8 1 4 1 8 4 1 4 4 7 4 8 2 9 2 5 2 2 3 3 8 5 2 9 3 10 8 8 1 6 6 1 6 7 10
7 5 10 3 2 2 7 4 8 7 6 6 5
56 36
33 41
32 62
37 7
6 53
41 13
9 36
44 77
38 62
76 16
72 5
40 13
55 60
5 78
72 45
13 44
...

output:

0 -1 -1 -1 2 0 -1 7 0 -1 3 -1 0 0 1 -1 0 -1 0 0 -1 0 -1 2 -1 -1 -1 -1 -1 -1 0 0 0 -1 0 -1 3 0 0 0 0 0 -1 -1 -1 -1 0 0 0 -1 0 0 3 0 0 0 -1 -1 3 -1 0 0 0 0 0 8 -1 -1 1 -1 -1 0 3 4 -1 3 -1 3 -1 0 0 0 -1 

result:

ok 83 numbers

Test #2:

score: 10
Accepted
time: 36ms
memory: 267816kb

input:

95 33 40
1 1 1 1 3 3 1 1 2 1 1 2 3 3 2 2 2 1 2 3 1 2 1 2 2 1 2 2 3 3 1 1 2 3 1 2 1 3 2 1 1 1 3 2 1 1 1 2 3 2 3 1 1 3 2 3 1 2 1 3 1 2 1 1 1 1 2 1 2 3 1 1 3 3 2 1 3 3 3 1 2 3 1 2 2 1 3 2 1 1 1 2 2 3 1
2 2 3 1 3 3 2 3 3 2 2 2 2 1 1 1 1 2 1 1 1 1 1 2 3 1 3 2 2 3 1 3 3 1 2 2 3 2 1 3
11 95
57 80
22 89
56 ...

output:

2 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 3 -1 -1 -1 -1 -1 -1 -1 3 -1 3 -1 -1 0 -1 3 3 -1 0 0 -1 2 -1 -1 0 -1 -1 -1 -1 -1 -1 3 -1 -1 -1 -1 -1 0 -1 -1 2 -1 -1 -1 3 3 -1 -1 -1 0 -1 -1 -1 -1 3 -1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 3 0 -1 -1 -1 -1 2 -1 0 0 

result:

ok 95 numbers

Test #3:

score: 10
Accepted
time: 30ms
memory: 267916kb

input:

94 34 89
20 7 5 8 18 12 5 15 19 1 15 7 5 6 14 19 9 19 11 11 16 20 17 5 12 8 14 2 10 19 10 1 1 6 19 18 9 14 19 16 1 6 8 10 18 8 1 17 3 9 17 9 1 16 9 15 1 15 20 10 6 14 11 9 5 18 14 20 13 18 13 18 8 1 1 8 10 5 14 5 8 16 1 14 9 7 3 20 9 20 18 17 11 18
14 2 20 16 9 12 9 4 15 4 9 20 16 18 20 7 18 19 15 5...

output:

-1 19 -1 -1 -1 -1 -1 13 -1 -1 17 -1 -1 -1 26 -1 -1 2 -1 13 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 33 2 -1 33 -1 -1 42 -1 -1 -1 -1 2 -1 -1 39 -1 -1 33 8 -1 -1 -1 -1 -1 -1 19 -1 -1 -1 4 15 1 -1 26 61 4 -1 -1 2 -1 -1 3 13 -1 13 -1 -1 -1 -1 -1 0 -1 -1 -1 8 -1 -1 61 -1 -1 2 -1 -1 

result:

ok 94 numbers

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #4:

score: 0
Wrong Answer
time: 50ms
memory: 268464kb

input:

2498 1795 2498
63 29 161 58 131 5 165 91 155 175 6 60 113 130 5 114 127 138 143 161 1 53 6 168 21 7 120 88 141 2 126 117 128 156 140 3 138 66 102 77 23 58 1 53 167 64 84 9 65 4 39 162 155 140 137 139 159 140 150 149 69 85 22 102 2 35 87 89 171 162 18 93 151 22 96 98 98 101 51 108 10 98 59 87 65 94 7...

output:

-1 -1 1599 -1 -1 0 1599 -1 -1 -1 0 -1 -1 -1 0 -1 1200 -1 -1 -1 0 -1 -1 1599 -1 0 -1 -1 -1 -1 1199 -1 -1 -1 1299 0 1299 -1 -1 -1 200 499 0 499 -1 -1 -1 -1 -1 -1 -1 -1 -1 1299 1299 -1 -1 -1 -1 1400 599 -1 -1 -1 -1 300 -1 -1 -1 -1 -1 900 -1 -1 900 -1 899 -1 -1 999 0 -1 -1 -1 -1 -1 -1 899 -1 -1 -1 1699 ...

result:

wrong answer 137th numbers differ - expected: '200', found: '199'

Subtask #3:

score: 0
Time Limit Exceeded

Test #6:

score: 30
Accepted
time: 626ms
memory: 305788kb

input:

100000 355071 10000
5 7 4 7 4 1 10 5 9 4 9 4 3 10 5 4 9 1 7 10 1 6 10 3 10 9 8 4 6 3 10 8 6 8 3 5 10 9 7 7 1 3 8 8 6 2 8 4 2 9 1 10 3 6 3 8 9 10 5 7 3 2 1 5 7 4 3 4 6 4 2 7 2 5 5 6 4 6 7 4 4 6 4 2 3 9 9 9 10 8 1 6 7 2 9 8 2 3 1 6 9 4 10 3 10 1 2 3 3 4 1 1 1 5 8 6 8 3 1 6 2 9 5 9 4 7 2 10 7 5 2 2 7 4...

output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 100000 numbers

Test #7:

score: 30
Accepted
time: 135ms
memory: 298456kb

input:

100000 300561 10000
6 3 6 10 10 9 7 3 6 4 5 4 1 2 3 2 10 6 3 7 8 7 10 5 9 10 2 3 9 5 6 10 9 1 4 9 1 10 7 2 9 10 5 5 9 3 5 5 5 9 9 5 1 7 5 10 8 6 8 4 5 9 2 10 1 6 4 10 10 9 2 1 10 1 9 5 3 2 9 3 4 8 10 7 5 2 4 5 3 6 9 7 5 10 2 7 4 7 10 8 1 7 7 1 7 7 6 6 7 1 5 4 6 2 1 8 6 10 6 10 1 5 8 4 6 2 10 6 10 4 ...

output:

0 4 0 0 3 0 0 4 0 0 0 0 0 0 0 0 0 0 1 0 0 1 7 0 1 2 0 0 0 0 4 0 0 1 0 0 0 2 0 0 0 3 0 0 1 0 0 0 3 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 1 0 9 0 0 0 2 0 3 1 0 2 1 0 0 0 2 0 0 0 0 0 0 3 0 0 0 0 0 2 2 0 0 1 0 0 0 0 0 1 0 0 1 0 2 0 6 0 0 1 5 0 0 3 0 0 1 2 0 2 3 0 0 2 0 1 1 9 0 2 5 2 0 2 1 3 1 6 4 0 0 1 0 2 1 0 ...

result:

ok 100000 numbers

Test #8:

score: 0
Time Limit Exceeded

input:

500000 1770902 50000
4 7 2 3 6 10 8 2 2 6 2 3 3 7 3 1 5 2 1 10 2 6 3 4 2 8 10 6 6 10 9 3 3 2 9 10 4 5 3 9 7 10 4 3 6 6 4 9 4 4 4 1 9 5 6 10 3 7 5 8 10 1 6 5 1 7 9 10 2 4 6 9 6 2 2 8 4 7 9 1 9 4 6 4 6 3 9 2 2 1 1 1 8 3 10 10 2 2 5 15 20 18 17 15 12 11 11 11 11 12 13 19 18 20 15 11 11 20 10 14 13 14 1...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #4:

0%