QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#328653 | #837. Giant Penguin | AFewSuns | WA | 22ms | 55364kb | C++14 | 4.1kb | 2024-02-15 22:46:21 | 2024-02-15 22:46:22 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define ll long long
#define bl bool
ll my_pow(ll a,ll b,ll mod){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll qpow(ll a,ll b){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
#define db double
#define pf printf
#define pc putchar
#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
#define go(u) for(ll i=head[u];i;i=e[i].nxt)
#define enter pc('\n')
#define space pc(' ')
#define fir first
#define sec second
#define MP make_pair
#define il inline
#define inf 8e18
#define random(x) rand()*rand()%(x)
#define inv(a,mod) my_pow((a),(mod-2),(mod))
il ll read(){
ll sum=0,f=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
sum=sum*10+(ch^48);
ch=getchar();
}
return sum*f;
}
il void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9) write(x/10);
pc(x%10+'0');
}
il void writeln(ll x){
write(x);
enter;
}
il void writesp(ll x){
write(x);
space;
}
}
using namespace my_std;
queue<ll> Q;
vector<ll> vec1[100010],vec2[100010],vec,id;
vector<ll> dis[100010][19],f[100010];
ll n,m,k,q,head[100010],cnt=0;
ll rt,minn=inf,nsiz,siz[100010];
ll Fa[100010],blg[100010],dep[100010];
bl ck[100010],pd[100010];
struct node{
ll nxt,to;
}e[400040];
void add(ll u,ll v){
e[++cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void dfs(ll fa,ll u){
ck[u]=1;
go(u){
ll v=e[i].to;
if(v==fa) continue;
if(!ck[v]){
vec1[u].push_back(v);
vec1[v].push_back(u);
dfs(u,v);
}
else if(u<v) vec2[u].push_back(v);
}
}
void getroot(ll fa,ll u){
siz[u]=1;
ll maxx=0;
fr(i,0,(ll)vec1[u].size()-1){
ll v=vec1[u][i];
if(v==fa||ck[v]) continue;
getroot(u,v);
maxx=max(maxx,v);
siz[u]+=siz[v];
}
maxx=max(maxx,nsiz-siz[u]);
if(maxx<minn){
minn=maxx;
rt=u;
}
}
void dfs1(ll RT,ll fa,ll u){
blg[u]=RT;
siz[u]=1;
fr(i,0,(ll)vec1[u].size()-1){
ll v=vec1[u][i];
if(v==fa||ck[v]) continue;
dfs1(RT,u,v);
siz[u]+=siz[v];
}
}
void dfs2(ll fa,ll u){
pd[u]=1;
id.push_back(u);
fr(i,0,(ll)vec2[u].size()-1){
ll v=vec2[u][i];
if(blg[v]!=blg[u]) vec.push_back(u);
}
fr(i,0,(ll)vec1[u].size()-1){
ll v=vec1[u][i];
if(v==fa||ck[v]) continue;
dfs2(u,v);
}
}
il void bfs(ll d,ll x){
fr(i,0,(ll)id.size()-1) dis[id[i]][d][x]=inf;
dis[vec[x]][d][x]=0;
Q.push(vec[x]);
while(!Q.empty()){
ll u=Q.front();
Q.pop();
go(u){
ll v=e[i].to;
if(!pd[v]||dis[v][d][x]!=inf) continue;
dis[v][d][x]=dis[u][d][x]+1;
Q.push(v);
}
}
}
void solve(ll fa,ll u){
dep[u]=dep[fa]+1;
Fa[u]=fa;
ck[u]=1;
blg[u]=u;
siz[u]=1;
fr(i,0,(ll)vec1[u].size()-1){
ll v=vec1[u][i];
if(v==fa||ck[v]) continue;
dfs1(v,u,v);
siz[u]+=siz[v];
}
vec.clear();
vec.push_back(u);
pd[u]=1;
id.clear();
id.push_back(u);
fr(i,0,(ll)vec1[u].size()-1){
ll v=vec1[u][i];
if(v==fa||ck[v]) continue;
dfs2(u,v);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
f[u].resize((ll)vec.size());
fr(i,0,(ll)f[u].size()-1) f[u][i]=inf;
fr(i,0,(ll)id.size()-1) dis[id[i]][dep[u]].resize((ll)vec.size());
fr(i,0,(ll)vec.size()-1) bfs(dep[u],i);
fr(i,0,(ll)id.size()-1) pd[id[i]]=0;
go(u){
ll v=e[i].to;
if(v==fa||ck[v]) continue;
nsiz=siz[u];
minn=inf;
getroot(u,v);
solve(u,rt);
}
}
il void mdf(ll x){
for(ll u=x;u;u=Fa[u]) fr(i,0,(ll)f[u].size()-1) f[u][i]=min(f[u][i],dis[x][dep[u]][i]);
}
il ll query(ll x){
ll res=inf;
for(ll u=x;u;u=Fa[u]) fr(i,0,(ll)f[u].size()-1) res=min(res,dis[x][dep[u]][i]+f[u][i]);
return res;
}
int main(){
n=read();
m=read();
k=read();
fr(i,1,m){
ll u=read(),v=read();
add(u,v);
add(v,u);
}
dfs(0,1);
fr(i,1,n) ck[i]=0;
getroot(0,1);
solve(0,rt);
q=read();
while(q--){
ll opt=read(),x=read();
if(opt==1) mdf(x);
else writeln(query(x));
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 55364kb
input:
5 4 0 1 2 2 3 3 4 4 5 7 1 1 1 5 2 1 2 2 2 3 2 4 2 5
output:
0 1 2 1 0
result:
ok 5 number(s): "0 1 2 1 0"
Test #2:
score: 0
Accepted
time: 3ms
memory: 55132kb
input:
5 6 2 1 2 2 3 1 3 3 4 4 5 3 5 3 1 1 2 4 2 5
output:
2 2
result:
ok 2 number(s): "2 2"
Test #3:
score: -100
Wrong Answer
time: 22ms
memory: 55256kb
input:
100 99 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52...
output:
99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 7 6 5 4 3 2 1 0 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 31 30 29 28 27 26 25 24 23 22 0 1 2 3 4 5 6 7 8 9 10 11 12 8 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 99 98 97 96 95 94 93 92 91 90 89 88...
result:
wrong answer 35th numbers differ - expected: '65', found: '7'