QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#223953 | #7503. Too Many Edges | DAleksa | TL | 0ms | 5648kb | C++14 | 1.4kb | 2023-10-22 22:29:23 | 2023-10-22 22:29:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 10;
int n, m;
set<int> g[N];
int dp[N];
bool mark[N];
vector<int> path;
int ask(int u, int v) {
cout << "? " << u << " " << v << endl;
int x;
cin >> x;
return x;
}
void dfs(int u) {
if(mark[u]) return;
for(int v : g[u]) {
dfs(v);
dp[u] = max(dp[u], dp[v] + 1);
}
}
void dfs2(int u) {
path.push_back(u);
for(int v : g[u]) {
if(dp[v] == dp[u] - 1) {
dfs2(v);
break;
}
}
}
int main() {
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].insert(v);
}
while(true) {
for(int i = 1; i <= n; i++) {
mark[i] = false;
dp[i] = 0;
}
for(int i = 1; i <= n; i++) {
if(mark[i]) continue;
dfs(i);
}
path.clear();
int mx = 1;
for(int i = 2; i <= n; i++) if(dp[i] > dp[mx]) mx = i;
dfs2(mx);
bool ok = true;
for(int i = 1; i < path.size(); i++) {
if(ask(path[i - 1], path[i]) == 0) {
g[path[i - 1]].erase(path[i]);
ok = false;
break;
}
}
if(ok) {
cout << "! " << path.size() - 1;
return 0;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5648kb
input:
5 5 1 2 1 3 2 5 3 4 4 5 1 0 1 1
output:
? 1 3 ? 3 4 ? 1 2 ? 2 5 ! 2
result:
ok Correct answer
Test #2:
score: -100
Time Limit Exceeded
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 ...