QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#543310 | #6543. Visiting Friend | 1bin | WA | 0ms | 14396kb | C++17 | 1.8kb | 2024-09-01 15:49:17 | 2024-09-01 15:49:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 2e5 + 5;
int T, n, m, t, u, v, q;
int dfsn[NMAX], lv[NMAX], low[NMAX], sz[NMAX], cnt[NMAX], par[NMAX][18];
vector<int> adj[NMAX];
void dfs(int x, int p){
dfsn[x] = low[x] = ++t;
sz[x] = 1;
for(int& nx : adj[x]){
if(nx == p) continue;
if(!dfsn[nx]){
lv[nx] = lv[x] + 1;
par[nx][0] = x;
dfs(nx, x);
sz[x] += sz[nx];
low[x] = min(low[x], low[nx]);
if(low[nx] >= dfsn[x]) cnt[x] += sz[nx];
}
else low[x] = min(low[x], dfsn[nx]);
}
return;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> T;
while(T--){
cin >> n >> m;
while(m--){
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
dfs(1, -1);
for(int j = 1; j < 18; j++)
for(int i = 1; i <= n; i++) par[i][j] = par[par[i][j - 1]][j - 1];
cin >> q;
while(q--){
cin >> u >> v;
if(lv[u] > lv[v]) swap(u, v);
int p = v;
for(int j = 17; j >= 0; j--)
if(lv[p] - lv[u] > (1<<j)) p = par[p][j];
// cout << u << ' ' << v << ' ' << p << '\n';
if(par[p][0] == u) {
int ans = sz[p] + 1 - cnt[v];
if(low[p] < dfsn[u]) ans += n - 1 - sz[p] - cnt[v];
cout << ans << '\n';
}
else cout << n - cnt[u] - cnt[v] << '\n';
}
// init
t = 0;
for(int i = 0; i <= n; i++) {
dfsn[i] = low[i] = sz[i] = cnt[i] = lv[i] = 0;
adj[i].clear();
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 13928kb
input:
1 5 5 1 2 1 3 2 4 4 5 2 5 5 1 2 1 4 2 3 2 5 3 5
output:
2 4 3 3 5
result:
ok 5 number(s): "2 4 3 3 5"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 14396kb
input:
100 10 25 7 4 1 10 7 2 3 4 5 7 9 10 10 5 8 10 6 7 9 1 4 2 2 6 8 5 6 9 5 9 7 1 2 1 4 1 9 8 8 3 1 8 4 8 2 10 3 1 3 6 100 6 4 10 8 5 4 7 8 3 10 5 9 6 9 6 8 2 10 10 9 6 9 1 8 3 6 10 9 1 4 10 8 1 6 5 1 5 10 9 1 3 5 8 7 3 2 3 9 2 9 9 4 2 10 8 4 6 2 2 1 7 4 3 6 6 5 10 6 1 4 5 1 7 10 7 1 8 5 3 8 7 5 3 9 5 4...
output:
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ...
result:
wrong answer 2001st numbers differ - expected: '9', found: '8'