QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328541#837. Giant PenguinCharlieVinnieTL 3ms12500kbC++172.4kb2024-02-15 20:54:422024-02-15 20:54:43

Judging History

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

  • [2024-02-15 20:54:43]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:12500kb
  • [2024-02-15 20:54:42]
  • 提交

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,ban[N],siz[N],cursiz,vis[N],V,ins[N],tfa[N],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(!ban[v]&&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; ins[u]=1; qwq.push_back(u);
    for(int v:ed[u]) if(v!=pa) tfa[v]=u,dfs(v,u),siz[u]+=siz[v];
    for(int v:to[u]) if(!ban[v]&&vis[v]!=V&&tfa[v]!=u&&!ins[v]) tmp.push_back(u),tmp.push_back(v);
    ins[u]=0;
}
void solve(int 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; }
    tmp={rt}; dfs(rt,0); sort(tmp.begin(),tmp.end()); tmp.erase(unique(tmp.begin(),tmp.end()),tmp.end());
    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(dis[v]==1e9) dis[v]=dis[u]+1,q.push(v);
        }
    }
    for(int u:tmp) ban[u]=1;
    for(int u:ed[rt]) if(!ban[u]) solve(u);
}
signed main(){
    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: 3ms
memory: 12500kb

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: 0ms
memory: 11920kb

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
Time Limit Exceeded

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:


result: