QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185515 | #2550. Lion and Zebra | kkio | WA | 3ms | 17748kb | C++20 | 4.3kb | 2023-09-22 10:40:44 | 2023-09-22 10:40:45 |
Judging History
answer
#include <bits/stdc++.h>
//#include "PrettyDebug.hpp"
using namespace std;
namespace FastIO {
struct IO {
char ibuf[(1 << 20) + 1], *iS, *iT, obuf[(1 << 20) + 1], *oS;
IO() : iS(ibuf), iT(ibuf), oS(obuf) {} ~IO() { fwrite(obuf, 1, oS - obuf, stdout); }
#if ONLINE_JUDGE
#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
#else
#define gh() getchar()
#endif
inline bool eof (const char &ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == 't' || ch == EOF; }
inline long long read() {
char ch = gh();
long long x = 0;
bool t = 0;
while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
return t ? ~(x - 1) : x;
}
inline void read (char *s) {
char ch = gh(); int l = 0;
while (eof(ch)) ch = gh();
while (!eof(ch)) s[l++] = ch, ch = gh();
}
inline void read (double &x) {
char ch = gh(); bool t = 0;
while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gh();
if (ch != '.') return t && (x = -x), void(); ch = gh();
for (double cf = 0.1; '0' <= ch && ch <= '9'; ch = gh(), cf *= 0.1) x += cf * (ch ^ 48);
t && (x = -x);
}
inline void pc (char ch) {
#ifdef ONLINE_JUDGE
if (oS == obuf + (1 << 20) + 1) fwrite(obuf, 1, oS - obuf, stdout), oS = obuf;
*oS++ = ch;
#else
putchar(ch);
#endif
}
template<typename _Tp>
inline void write (_Tp x) {
static char stk[64], *tp = stk;
if (x < 0) x = ~(x - 1), pc('-');
do *tp++ = x % 10, x /= 10;
while (x);
while (tp != stk) pc((*--tp) | 48);
}
inline void write (char *s) {
int n = strlen(s);
for (int i = 0; i < n; i++) pc(s[i]);
}
} io;
inline long long read () { return io.read(); }
template<typename Tp>
inline void read (Tp &x) { io.read(x); }
template<typename _Tp>
inline void write (_Tp x) { io.write(x); }
}
using namespace FastIO;
constexpr int maxn=1e5+10;
int n,q;vector<int> G[maxn];
int anc[maxn][19],dep[maxn];
inline void ancdfs(int u,int fa)
{
anc[u][0]=fa;dep[u]=dep[fa]+1;
for(int i=1;i<=18;i++)anc[u][i]=anc[anc[u][i-1]][i-1];
for(int v:G[u])
if(v!=fa)
ancdfs(v,u);
}
inline int getlca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
for(int i=18;i>=0;i--)if(dep[anc[x][i]]>=dep[y])x=anc[x][i];
if(x==y)return x;
for(int i=18;i>=0;i--)if(anc[x][i]^anc[y][i])x=anc[x][i],y=anc[y][i];
return anc[x][0];
}
inline int k_anc(int x,int k)
{
for(int i=18;i>=0;i--)if(k>>i&1)x=anc[x][i];
return x;
}
inline int pathto(int x,int y,int d)
{
int z=getlca(x,y);
int disxz=dep[x]-dep[z],diszy=dep[y]-dep[z];
if(disxz>=d)return k_anc(x,d);
d=disxz+diszy-d;return k_anc(y,d);
}
int upd[maxn],dwd[maxn],ftd[maxn],udd[maxn];
set<int> sdep[maxn];
void downdfs(int u,int fa)
{
ftd[u]=u,dwd[u]=0;
udd[u]=u,upd[u]=0;
for(int v:G[u])
if(v!=fa)
{
downdfs(v,u);
dwd[u]=max(dwd[u],dwd[v]+1);
if(dwd[u]==dwd[v]+1)ftd[u]=ftd[v];
sdep[u].insert(dwd[v]+1);
}
}
void updfs(int u,int fa)
{
int mxd1=-1e9,pd=0,mxd2=-1e9,gd=0;
for(int v:G[u])
if(v!=fa)
{
if(dwd[v]+1>mxd1)mxd2=mxd1,mxd1=dwd[v]+1,gd=pd,pd=v;
else if(dwd[v]+1>mxd2)mxd2=dwd[v]+1,gd=v;
}
for(int v:G[u])
if(v!=fa)
{
if(v!=pd)upd[v]=upd[u]>mxd1?(udd[v]=udd[u],upd[u]+1):(udd[v]=ftd[pd],mxd1+1);
else upd[v]=upd[u]>mxd2?(udd[v]=udd[u],upd[u]+1):(udd[v]=ftd[gd],mxd2+1);
sdep[v].insert(upd[v]);
updfs(v,u);
}
}
int main()
{
n=read(),q=read();
for(int i=1,u,v;i<n;i++)
{
u=read(),v=read();
G[u].push_back(v);G[v].push_back(u);
}
ancdfs(1,1);
downdfs(1,1);
updfs(1,1);
for(int t=1;t<=q;t++)
{
int v=read(),d=read();
int s=dwd[v]>upd[v]?ftd[v]:udd[v];
//debug(s,dwd[v],upd[v]);
int ed=(d-1)/2,c=pathto(v,s,ed),step=0;
//debug(ed,c);
if(d>2)
{
int p=pathto(v,s,ed-1);
//debug(p,dwd[p],upd[p]);
if(dep[c]<dep[p])step=max(step,dwd[p]+1);
else step=max(step,upd[p]+1);
}
if(d%2==0&&sdep[c].count(d/2))step=max(step,d/2);
//debug(step);
int ans=step+d-ed;
write(ans),io.pc('\n');
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15324kb
input:
5 2 1 2 2 3 3 4 4 5 1 4 3 1
output:
4 1
result:
ok 2 number(s): "4 1"
Test #2:
score: 0
Accepted
time: 2ms
memory: 15112kb
input:
11 2 1 2 2 3 1 4 4 5 1 6 6 7 7 8 1 9 9 10 10 11 3 2 10 4
output:
2 5
result:
ok 2 number(s): "2 5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 16156kb
input:
7 2 1 2 2 3 3 4 4 5 4 6 6 7 5 4 5 3
output:
5 3
result:
ok 2 number(s): "5 3"
Test #4:
score: 0
Accepted
time: 3ms
memory: 17748kb
input:
11 9 1 2 2 3 1 4 4 5 1 6 6 7 7 8 1 9 9 10 10 11 7 2 3 2 3 5 3 4 4 4 1 1 8 1 4 3 5 3
output:
3 2 5 4 5 1 1 4 3
result:
ok 9 numbers
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 15384kb
input:
175 7862 70 167 102 128 3 76 46 42 104 112 53 46 52 99 172 116 48 158 40 138 11 103 26 8 59 147 163 88 71 133 130 134 98 73 115 104 28 166 5 173 148 61 38 45 173 73 96 10 36 58 124 59 94 73 137 136 79 164 21 11 27 161 9 152 37 101 123 131 145 68 111 156 153 51 61 73 4 93 54 157 33 69 47 12 144 115 1...
output:
1 2 3 4 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 1 2 28 29 30 31 32 33 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 1 2 27 28 29 30 31 32 33 34 35 36 38 39 40 41...
result:
wrong answer 3rd numbers differ - expected: '9', found: '3'