QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300376#7503. Too Many Edgesdefyers#WA 50ms8536kbC++172.1kb2024-01-08 09:23:202024-01-08 09:23:21

Judging History

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

  • [2024-01-08 09:23:21]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:8536kb
  • [2024-01-08 09:23:20]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using LL = long long;
using pii = pair<int, int>;

int n, m;
vector<int> v[50005];
int in[50005];
int dis[50005];
int out[50005];
vector<int> possible;
vector<int> bk[50005];

unordered_map<LL, bool> M;
bool hv(LL u, LL v) {
	LL tmp = u * (m + 1) + v;
	if (M.find(tmp) == M.end()) {
		M[tmp] = true;
		return false;
	}
	return true;
}
int ask(int u, int v) {
	cout << "? " << u << " " << v << "\n";
	cout.flush();
	int ans;
	cin >> ans;
	return ans;
}
void redo(int x) {
	int before = dis[x];
	dis[x] = 0;
	for (auto i : bk[x]) {
		dis[x] = max(dis[x], dis[i] + 1);
	}
	if (dis[x] == before) return;
	for (auto i : v[x]) redo(i);
}
void solve(int TC) {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int r1, r2;
		cin >> r1 >> r2;
		v[r1].push_back(r2);
		bk[r2].push_back(r1);
		in[r2]++;
		out[r1]++;
	}
	queue<int> q;
	for (int i = 1; i <= n; i++) {
		if (in[i] == 0) q.push(i);
		dis[i] = 0;
	}
	while (q.size()) {
		auto pr = q.front();
		q.pop();
		for (auto i : v[pr]) {
			dis[i] = max(dis[i], dis[pr] + 1);
			in[i]--;
			if (in[i] == 0) q.push(i);
		}
	}
	for (int i = 1; i <= n; i++) {
		if (out[i] == 0) possible.push_back(i);
	}
	while (true) {
		int mx = 0;
		int id;
		for (auto i : possible) {
			if (dis[i] > mx) {
				mx = dis[i];
				id = i;
			}
		}
		bool reach = true;
		while (dis[id] > 0) {
			int pt;
			for (auto i : bk[id]) {
				if (dis[i] == dis[id] - 1) {
					pt = i;
					break;
				}
			}
			if (hv(pt, id)) id = pt;
			else {
				int ans = ask(pt, id);
				if (ans == 1) id = pt;
				else {
					auto iter = find(v[pt].begin(), v[pt].end(), id);
					v[pt].erase(iter);
					auto iter2 = find(bk[id].begin(), bk[id].end(), pt);
					bk[id].erase(iter2);
					redo(id);
					reach = false;
					break;
				}
			}
		}
		if (reach) {
			cout << "! " << mx << "\n";
			cout.flush();
			break;
		}
	}
	return;
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	cout << fixed << setprecision(10);

	int t = 1;
	// cin >> t;

	for (int i = 1; i <= t; i++) {
		solve(i);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

? 4 5
? 3 4
? 2 5
? 1 2
! 2

result:

ok Correct answer

Test #2:

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

input:

230 470
212 98
107 123
116 72
158 83
135 111
78 123
221 217
214 203
28 221
229 3
111 127
128 13
125 116
180 78
175 191
182 99
194 6
12 83
209 29
169 129
192 208
145 38
33 86
104 85
145 82
38 82
193 153
109 41
199 194
89 72
161 227
36 177
13 141
173 110
212 77
155 81
87 82
104 172
148 182
170 222
68 ...

output:

? 124 151
? 73 124
? 210 73
? 99 210
? 182 99
? 148 182
? 97 148
? 92 97
? 136 92
? 31 136
? 171 31
? 184 171
? 208 184
? 146 208
? 68 146
? 203 68
? 4 203
? 74 4
? 219 74
? 88 219
? 157 88
? 43 157
? 126 43
? 228 126
? 16 228
? 213 16
? 214 213
? 133 214
? 37 133
? 216 37
? 32 216
? 122 32
? 49 122...

result:

ok Correct answer

Test #3:

score: -100
Wrong Answer
time: 50ms
memory: 8536kb

input:

32500 94033
5330 27480
6016 11178
29267 1197
5789 21547
23714 25915
15710 17107
16950 10411
13998 15431
11106 14400
927 25530
18890 3967
11978 17027
2434 683
20135 5988
10802 22709
29179 6971
24499 12498
10977 795
30366 3120
4051 21131
25859 15273
19124 10952
14475 11243
11810 1265
7311 2036
385 158...

output:

? 31700 11512
? 11240 31700
? 30299 11240
? 29669 30299
? 14903 29669
? 29261 14903
? 3190 29261
? 29839 3190
? 2312 29839
? 851 2312
? 30214 851
? 21823 30214
? 26688 21823
? 23280 26688
? 7744 23280
? 29774 7744
? 12608 29774
? 30655 12608
? 29921 30655
? 14832 29921
? 4725 14832
? 22499 4725
? 69...

result:

wrong answer The answer is incorrect