QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328578#837. Giant PenguinCharlieVinnieWA 0ms10896kbC++172.6kb2024-02-15 21:27:332024-02-15 21:27:34

Judging History

你现在查看的是最新测评结果

  • [2024-02-15 21:27:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:10896kb
  • [2024-02-15 21:27:33]
  • 提交

answer

#include "bits/stdc++.h"
#ifdef DEBUG
#include "PrettyDebug.hpp"
#else
#define debug(...) [](auto...){}(__VA_ARGS__)
#define debuga(...) [](auto...){}(__VA_ARGS__)
#endif
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
using namespace std; typedef long long ll;
constexpr int N=1e5+5; using pii = pair<int,int>;
int n,m,K,Q,col[N],colcnt,L,C,siz[N],cursiz,vis[N],V,mx[N],dis[N]; vector<int> to[N],ed[N]; vector<int> tmp,qwq; vector<pii> lis[N];
void dfs0(int u){
    mx[u]=0; siz[u]=1; vis[u]=V; qwq.push_back(u);
    for(int v:to[u]) if(col[v]==C&&vis[v]!=V){
        ed[u].push_back(v); ed[v].push_back(u);
        dfs0(v); siz[u]+=siz[v]; mx[u]=max(mx[u],siz[v]);
    }
}
void dfs(int u,int pa){
    siz[u]=1; qwq.push_back(u);
    for(int v:ed[u]) if(v!=pa) dfs(v,u),siz[u]+=siz[v];
    for(int v:to[u]) if(v!=pa&&col[v]!=col[u]&&L<=col[v]&&u<v) tmp.push_back(u);
}
void gcol(int u,int pa) { col[u]=C; for(int v:ed[u]) if(v!=pa) gcol(v,u); }
void solve(int rt){
    C=col[rt]; V++; qwq.clear(); dfs0(rt); for(int u:qwq) if(mx[u]<=siz[rt]/2&&siz[rt]-siz[u]<=siz[rt]/2) { rt=u; break; }
    L=C+1; for(int u:ed[rt]) C++,gcol(u,rt);
    tmp={rt}; dfs(rt,0); sort(tmp.begin(),tmp.end()); tmp.erase(unique(tmp.begin(),tmp.end()),tmp.end());
    assert(tmp.size()<=2u*K+1);
    for(int S:tmp){
        for(int u:qwq) dis[u]=1e9;
        queue<int> q; q.push(S); dis[S]=0;
        while(q.size()){
            int u=q.front(); q.pop(); lis[u].emplace_back(S,dis[u]);
            for(int v:to[u]) if(col[v]==C&&dis[v]==1e9) dis[v]=dis[u]+1,q.push(v);
        }
    }
    auto awa=ed[rt]; for(int u:qwq) ed[u].clear();
    for(int u:awa) solve(u);
}
signed main(){
    // Fin("hh.in");
    atexit([](){cerr<<"Time = "<<clock()<<" ms"<<endl;});
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>K; For(i,1,m) { int x,y; cin>>x>>y; to[x].push_back(y); to[y].push_back(x); }
    cursiz=n; solve(1); For(i,1,n) sort(lis[i].begin(),lis[i].end()),lis[i].erase(unique(lis[i].begin(),lis[i].end()),lis[i].end());
    For(i,1,n) dis[i]=1e9;
    cin>>Q; while(Q--){
        int op,x; cin>>op>>x;
        if(op==1) { for(auto [u,d]:lis[x]) dis[u]=min(dis[u],d); }
        else { int ans=1e9; for(auto [u,d]:lis[x]) ans=min(ans,dis[u]+d);; cout<<ans<<'\n'; }
    }
    return 0;
}

// CONTINUE, NON-STOPPING, FOR CHARLIEY
// START TYPING IF YOU DON'T KNOW WHAT TO DO
// STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING

// Started Coding On: February 15 Thu, 20 : 22 : 03

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 10792kb

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: -100
Wrong Answer
time: 0ms
memory: 10896kb

input:

5 6 2
1 2
2 3
1 3
3 4
4 5
3 5
3
1 1
2 4
2 5

output:

1000000000
1000000000

result:

wrong answer 1st numbers differ - expected: '2', found: '1000000000'