QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#186954 | #3847. Airline | CometoTraval# | WA | 45ms | 28532kb | C++14 | 2.8kb | 2023-09-24 13:30:45 | 2023-09-24 13:30:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
template<class T>
void rd(T &x){
char c = getchar();
int f = 1;
for(;c>'9'||c<'0';c=getchar())
if (c == '-') f = -1;
for(x=0;c>='0'&&c<='9';c=getchar())
x = x * 10 + (c - '0');
x *= f;
}
int n;
vector<int>g[MAXN];
int sz[MAXN],fa[MAXN];
int dep[MAXN];
void dfs(int x, int ff){
fa[x] = ff;
dep[x] = dep[ff] + 1;
for (auto v : g[x])
if (v != ff)
dfs(v,x),sz[x] += sz[v];
sz[x] ++;
}
int q;
int getlca(int x,int y){
while(x != y){
if (dep[x] > dep[y])
x = fa[x];
else y = fa[y];
}
return x;
}
#define ll long long
vector<ll>getsize(int l,int r){
vector<ll>res;
if (l == r)
return res;
if (dep[l] < dep[r]){
// lca x
int lastx = 0;
while(r != l){
res.push_back(sz[r] - sz[lastx]);
lastx = r;
r = fa[r];
}
reverse(res.begin(),res.end());
return res;
}
int lastx = 0;
while(l != r){
res.push_back(sz[l] - sz[lastx]);
lastx = l;
l = fa[l];
}
return res;
}
int main(){
rd(n);rd(q);
for (int i = 1, x, y; i < n; i ++){
rd(x);rd(y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,0);
for (int i = 0; i < q; i ++){
int x,y;
rd(x);rd(y);
int lca = getlca(x,y);
vector<ll>sz1 = getsize(x,lca);
vector<ll>sz2 = getsize(lca,y);
if (x == lca){
int fay = y;
while(dep[fay] != dep[lca] + 1)
fay = fa[fay];
sz1.push_back(n - fa[fay]);
}
else if (y == lca){
int fax = x;
while(dep[fax] != dep[lca] + 1)
fax = fa[fax];
sz1.push_back(n - sz[fax]);
}
else {
int fax = x;
while(dep[fax] != dep[lca] + 1)
fax = fa[fax];
int fay = y;
while(dep[fay] != dep[lca] + 1)
fay = fa[fay];
sz1.push_back(n - sz[fax] - sz[fay]);
}
for (auto v : sz2)
sz1.push_back(v);
int Min = sz1.size() ;
for (int i = 1 ; i < sz1.size(); i ++)
sz1[i] += sz1[i-1];
int nd = Min/2;
long long ans =0;
for (int i = 0; i + nd <Min; i ++)
ans += (sz1.back() - sz1[i + nd]) * (i == 0 ? sz1[i] : (sz1[i] - sz1[i - 1]));
printf("%lld\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 43ms
memory: 28292kb
input:
1000 100000 552 9 456 720 540 790 628 695 848 478 66 268 798 360 773 277 116 471 874 792 912 784 502 831 359 965 925 434 677 629 271 670 76 755 92 200 814 422 922 266 617 44 480 331 18 662 153 753 669 491 368 187 99 867 476 808 774 509 98 147 724 478 447 182 923 469 881 665 674 589 770 613 436 310 8...
output:
8905 976 2924 5646 3837 5966 5530 2221 2394 2570 2238 4823 4034 2458 2582 1259 4751 2694 456 1380 3199 5788 2214 3854 3098 4086 5734 4148 3065 3428 3937 4461 2845 5935 5122 5625 2614 704 6312 3180 2480 7481 9410 4642 1660 4875 5767 2976 5717 5766 4783 4553 2483 3635 4125 4093 6074 8675 3988 4695 313...
result:
ok 100000 lines
Test #2:
score: -100
Wrong Answer
time: 45ms
memory: 28532kb
input:
9392 100000 4521 1973 362 7795 6505 6798 6079 8431 5691 9228 774 4060 2800 6246 6995 4890 5981 7196 6747 6781 8642 1970 4609 7891 6692 3764 1918 8164 8889 4386 6997 2266 8080 2062 1474 2612 5128 1618 6763 604 6611 4768 9265 2462 9323 9067 4086 7292 7661 5091 19 8937 3209 5829 4079 6058 1912 4137 443...
output:
25865 20795 47691 36294 117742 45172 57330 89573 117973 132250 35330 292130 28222 23630 79047 38816 217653 54722 121448 46997 75338 16394 24369 137591 17869 186807 34649 20072 15377 82113 24090 260999 67202 15795 51210 24248 33377 18702 163416 58397 29846 104190 78268 21985 58729 60089 77200 27965 8...
result:
wrong answer 18576th lines differ - expected: '108196', found: '1911076'