QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#223985#7503. Too Many EdgesDAleksaTL 2ms6072kbC++141.4kb2023-10-22 22:44:182023-10-22 22:44:19

Judging History

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

  • [2023-10-22 22:44:19]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:6072kb
  • [2023-10-22 22:44:18]
  • 提交

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]) {
        if(mark[v]) continue;
        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;
}

詳細信息

Test #1:

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

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 ...

output:


result: