QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592333 | #837. Giant Penguin | xwh_Marvelous | WA | 24ms | 48756kb | C++14 | 2.9kb | 2024-09-26 21:57:38 | 2024-09-26 21:57:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
// #define int long long
//#define mod 1000000007
#define N 200005
#define pii pair<int,int>
#define fi first
#define se second
//#define rep(i,j,k) for(int i=j;i<=k;i++)
int n,m,k,q;
vector<int>mp[N],T[N],at[N];
vector<int>ct[N],lst;
vector<pii>que[N];
int vis[N];
void getT(int x,int fa=0){
vis[x]=1;
for(int v:mp[x]){
if(v==fa)continue;
if(vis[v]){at[x].push_back(v);continue;}
T[v].push_back(x),T[x].push_back(v);getT(v,x);
}
}
int d[12][N];
bool tg[N];
int imp[N][12],bc[N];
int bxhd[N][12];
int ans[N];
int sz[N];
int col[N];
int tot;
int rt;
void gtsz(int x,int fa){
sz[x]=1;
lst.push_back(x);
for(int v:T[x]){
if(v==fa||vis[v])continue;
gtsz(v,x);
sz[x]+=sz[v];
}
}
int gtrt(int x){
lst.clear();
gtsz(x,x);
int ret=0;
function<void(int,int)>find=[&](int u,int fa){
if(sz[x]/2>=sz[u]-1&&sz[x]-sz[u]>=sz[x]/2){ret=u;return;}
for(int v:T[u]){if(fa==v||vis[v])continue;find(v,u);if(ret)return;}
};
find(x,x);
assert(ret);
return ret;
}
void gtd(int s,int *d,int pre){
queue<int>q;
q.push(s);
for(int v:lst)d[v]=-1;
d[s]=0;
while(q.size()){
int u=q.front();q.pop();
for(int v:mp[u]){
// cout<<v<<endl;
if(col[v]<=pre||d[v]!=-1)continue;
d[v]=d[u]+1;
q.push(v);
}
}
}
void stcol(int x,int fa,int op){
// cout<<x<<endl;
col[x]=op;
for(int v:T[x]){
if(v==fa||vis[v])continue;
stcol(v,x,op);
}
}
void sol(int x,vector<pii>&qwq){
// cout<<x<<endl;
// for(auto op:qwq)cout<<op.fi<<' '<<op.se<<endl;cout<<endl;
vis[x]=1;
int pre=tot;
col[x]=pre+1;
for(int v:T[x]){stcol(v,v,++tot);}
imp[x][++bc[x]]=x;
gtd(x,d[1],pre);
for(int v:lst){
for(int u:at[v]){
// cout<<u<<' '<<v<<endl;
if(col[u]<=pre||col[u]==col[v])continue;
imp[x][++bc[x]]=v;
gtd(v,d[bc[x]],pre);
}
}
for(auto w:qwq){
int op=w.fi,v=w.se;
// cout<<op<<' '<<v<<endl;
if(op==1){
for(int i=1;i<=bc[x];i++){
bxhd[x][i]=min(bxhd[x][i],d[i][v]);
}
}else{
for(int i=1;i<=bc[x];i++){
ans[op]=min(ans[op],bxhd[x][i]+d[i][v]);
}
}
if(v!=x){
que[col[v]].push_back(w);
}
}
qwq.clear();
int g=0;
for(int v:T[x]){
if(vis[v])continue;
g++;
int rt=gtrt(v);
sol(rt,que[pre+g]);
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
mp[u].push_back(v),mp[v].push_back(u);
}
getT(1);
memset(vis,0,sizeof(vis));
memset(bxhd,0x3f,sizeof(bxhd));
memset(ans,0x3f,sizeof(ans));
cin>>q;
for(int i=1;i<=q;i++){
int op,v;
cin>>op>>v;
if(op==1&&tg[v])continue;
if(op==1)tg[v]=1;
if(op==1)que[0].push_back({op,v});
else que[0].push_back({i,v});
}
// for(auto op:que[0])cout<<op.fi<<' '<<op.se<<endl;
rt=gtrt(1);
sol(rt,que[0]);
for(int i=1;i<=q;i++){
if(ans[i]>1e9)continue;
cout<<ans[i]<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 41860kb
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: 7ms
memory: 38008kb
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: 24ms
memory: 48756kb
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 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 99 98 97 9...
result:
wrong answer 33897th numbers differ - expected: '0', found: '1'