QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#152728 | #6559. A Tree and Two Edges | BUET_POISSON# | WA | 8ms | 14800kb | C++20 | 3.3kb | 2023-08-28 18:44:16 | 2023-08-28 18:44:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define setbit0(n, i) ((n) & (~(1LL << (i))))
#define setbit1(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define lastone(n) ((n) & (-(n)))
#define endl '\n'
char gap = 32;
#define int long long
#define ii pair<int, int>
#define ll long long
#define lll __int128_t
#define pb push_back
typedef vector<int> vi;
const int N = 5e4 + 5;
vi adj[N];
vi vis(N), depth(N);
vector<vi> bparent(N, vi(20));
const int LOG = 20;
int n, q;
vector<pair<int, int>> extra;
void build_ancestor(){
for(int i=1; i < LOG; i++)
for(int j=1; j<=n; j++)
bparent[j][i]=bparent[bparent[j][i-1]][i-1];
}
int pth_ancestor(int a,int p){
for(int i=0; (1<<i)<=p; i++)
if((1<<i)&p)
a=bparent[a][i];
return a;
}
int lca(int u,int v){
if(depth[v]>depth[u])
v=pth_ancestor(v,depth[v]-depth[u]);
if(depth[u]>depth[v])
u=pth_ancestor(u,depth[u]-depth[v]);
if(u==v)
return u;
//printf("%d %d\n",u,v);
for(int i=LOG-1; i>=0; i--){
if(bparent[u][i]!=bparent[v][i]){
u=bparent[u][i];
v=bparent[v][i];
}
}
return bparent[u][0];
}
void dfs(int node, int par) {
//cout << " --> " << node << " " << par << endl;
vis[node] = 1;
bparent[node][0] = par;
depth[node] = depth[par] + 1;
for (int v: adj[node]) {
if (v == par) continue;
if (vis[v]) {
if (node < v) extra.pb({node, v});
continue;
}
dfs(v, node);
}
}
int dist(int u, int v) {
int l = lca(u, v);
return depth[u] + depth[v] - 2*depth[l];
}
bool check_overlap(int s, int t, int u, int v) {
int min_dist = dist(s, t) + dist(u, v);
int o1 = dist(s, u) + dist(v, t);
int o2 = dist(s, v) + dist(u, t);
return (min_dist < o1) and (min_dist < o2);
}
int func(int s, int u, int v, int a, int b, int t) {
return check_overlap(s, u, v, a) and check_overlap(s, u, b, t) and check_overlap(v, a, b, t);
}
void solve() {
cin >> n >> q;
for (int i=0; i<n+1; i++) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
build_ancestor();
cout << extra.size() << endl;
for (auto k: extra) cout << k.first << " " << k.second << endl;
while(q--) {
int s, t;
cin >> s >> t;
int ans = 1;
vector<pair<int, int>> uv = {{extra[0].first, extra[0].second}, {extra[0].second, extra[0].first}};
vector<pair<int, int>> ab = {{extra[1].first, extra[1].second}, {extra[1].second, extra[1].first}};
for (auto k: uv) ans += check_overlap(s, k.first, k.second, t);
for (auto k: ab) ans += check_overlap(s, k.first, k.second, t);
for (auto k1: uv) {
for (auto k2: ab) {
ans += func(s, k1.first, k1.second, k2.first, k2.second, t);
ans += func(s, k2.first, k2.second, k1.first, k1.second, t);
}
}
cout << ans << endl;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 8ms
memory: 14800kb
input:
4 6 1 2 1 3 1 4 2 3 2 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
2 1 3 1 4 3 3 3 3 3 4
result:
wrong answer 1st lines differ - expected: '3', found: '2'