답안 #994164

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
994164 2024-06-07T08:07:53 Z bachhoangxuan Board Game (JOI24_boardgame) C++17
100 / 100
1607 ms 205452 KB
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const int inf=1e18;
const int mod=998244353;
const int maxn=50005;
const int B=650;
const int maxs=655;
const int maxm=200005;
const int maxq=1000005;
const int maxl=25;
const int maxa=1000000;
const int root=3;
int power(int a,int n){
    int res=1;
    while(n){
        if(n&1) res=res*a%mod;
        a=a*a%mod;n>>=1;
    }
    return res;
}
const int iroot=power(3,mod-2);
const int base=101;

int X[maxn],a[maxn],b[maxn],total[maxn];
int n,m,k,c[maxn];
vector<int> edge[maxn];
int dist[maxn],D[maxn];

int Num=0,num=0,res[maxn];
vector<int> dd[maxn];

void solve(){
    cin >> n >> m >> k;
    for(int i=1;i<=m;i++){
        int u,v;cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    for(int i=1;i<=n;i++){
        char cc;cin >> cc;
        c[i]=cc-'0';Num+=c[i];
    }
    for(int i=1;i<=k;i++) cin >> X[i];

    memset(dist,-1,sizeof(dist));
    queue<int> q;
    for(int i=1;i<=n;i++) if(c[i]){
        dist[i]=0;
        q.push(i);
    }
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int v:edge[u]) if(dist[v]==-1){
            dist[v]=dist[u]+1;
            q.push(v);
        }
    }
    for(int i=1;i<=k;i++) if(dist[X[i]]) a[i]=dist[X[i]]-2;

    for(int i=1;i<=n;i++) dist[i]=inf;
    deque<pii> dq;
    for(int i=1;i<=n;i++){
        bool cc=false;
        for(int v:edge[i]) if(c[v]) cc=true;
        if(c[i] && cc){
            dist[i]=0;
            dq.push_back({0,i});
        }
    }
    while(!dq.empty()){
        auto [d,u]=dq.front();dq.pop_front();
        if(dist[u]!=d) continue;
        for(int v:edge[u]){
            int nd=d+1-c[v];
            if(dist[v]<=nd) continue;
            dist[v]=nd;
            if(nd==d) dq.push_front({nd,v});
            else dq.push_back({nd,v});
        }
    }
    for(int i=1;i<=k;i++){
        b[i]=dist[X[i]];
        if(b[i]!=inf) b[i]-=(1-c[X[i]]);
    }

    dq.clear();
    for(int i=1;i<=n;i++) dist[i]=inf;
    dist[X[1]]=0;dq.push_back({0,X[1]});
    while(!dq.empty()){
        auto [d,u]=dq.front();dq.pop_front();
        if(dist[u]!=d) continue;
        for(int v:edge[u]){
            int nd=d+c[v];
            if(dist[v]<=nd) continue;
            dist[v]=nd;
            if(nd==d) dq.push_front({nd,v});
            else dq.push_back({nd,v});
        }
    }
    for(int i=1;i<=n;i++){
        D[i]=dist[i];
        //cout << D[i] << ' ';
    }
    //cout << '\n';
    int cur=k-1;
    for(int j=2;j<=k;j++) if(b[j]-a[j]<=Num) total[max(0LL,b[j]-a[j])]++;
    cur-=total[0];total[0]=0;
    for(int j=2;j<=k;j++) total[0]+=min(a[j],b[j]);
    for(int i=1;i<=Num;i++){
        int x=total[i];
        total[i]=total[i-1]+k-1+cur;
        cur-=x;
    }
    total[0]=0;
    queue<pii> qq;
    num=min(Num,500LL);
    for(int i=1;i<=n;i++) dd[i].resize(num+1,-1);
    dd[X[1]][0]=0;
    qq.push({X[1],0});
    while(!qq.empty()){
        auto [u,t]=qq.front();qq.pop();
        for(int v:edge[u]){
            if(t+c[v]-D[v]>num || t+c[v]>Num) continue;
            if(dd[v][t+c[v]-D[v]]==-1){
                dd[v][t+c[v]-D[v]]=dd[u][t-D[u]]+1;
                qq.push({v,t+c[v]});
            }
        }
    }
    priority_queue<pii,vector<pii>,greater<pii>> pq;
    for(int i=1;i<=n;i++) dist[i]=inf;
    for(int i=1;i<=n;i++){
        res[i]=inf;
        for(int j=0;j<=num;j++){
            if(dd[i][j]==-1) continue;
            dist[i]=dd[i][j]+total[j+D[i]];
            if(!c[i]) res[i]=min(res[i],dd[i][j]+total[j+D[i]]);
            else res[i]=min(res[i],dd[i][j]+total[max(0LL,j+D[i]-1)]);
        }
    }
    if(k<=100 && Num>500){
        for(int i=1;i<=n;i++) if(dist[i]!=inf) pq.push({dist[i],i});
        cur+=k-1;
        while(!pq.empty()){
            auto [d,u]=pq.top();pq.pop();
            for(int v:edge[u]){
                int nd=d+1+cur*c[v];
                if(nd<dist[v]) pq.push({dist[v]=nd,v});
            }
        }
        for(int i=1;i<=n;i++){
            if(dist[i]==inf) continue;
            if(!c[i]) res[i]=min(res[i],dist[i]);
            else res[i]=min(res[i],dist[i]-cur);
        }
    }
    for(int i=1;i<=n;i++) cout << res[i] << '\n';
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;//cin >> test;
    while(test--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 19 ms 7996 KB Output is correct
3 Correct 26 ms 10324 KB Output is correct
4 Correct 22 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 17 ms 8112 KB Output is correct
3 Correct 27 ms 8796 KB Output is correct
4 Correct 25 ms 8796 KB Output is correct
5 Correct 28 ms 10068 KB Output is correct
6 Correct 24 ms 8760 KB Output is correct
7 Correct 25 ms 8276 KB Output is correct
8 Correct 26 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 20 ms 8120 KB Output is correct
3 Correct 29 ms 8784 KB Output is correct
4 Correct 28 ms 9044 KB Output is correct
5 Correct 30 ms 10068 KB Output is correct
6 Correct 28 ms 8796 KB Output is correct
7 Correct 28 ms 8796 KB Output is correct
8 Correct 30 ms 10068 KB Output is correct
9 Correct 15 ms 7004 KB Output is correct
10 Correct 14 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 37 ms 12636 KB Output is correct
3 Correct 3 ms 4952 KB Output is correct
4 Correct 57 ms 16692 KB Output is correct
5 Correct 18 ms 9564 KB Output is correct
6 Correct 4 ms 5212 KB Output is correct
7 Correct 4 ms 5468 KB Output is correct
8 Correct 38 ms 16756 KB Output is correct
9 Correct 44 ms 13764 KB Output is correct
10 Correct 44 ms 13796 KB Output is correct
11 Correct 45 ms 14312 KB Output is correct
12 Correct 50 ms 14684 KB Output is correct
13 Correct 21 ms 10224 KB Output is correct
14 Correct 3 ms 5464 KB Output is correct
15 Correct 5 ms 5976 KB Output is correct
16 Correct 9 ms 7260 KB Output is correct
17 Correct 22 ms 10852 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 ms 4952 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 41 ms 16732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 23892 KB Output is correct
2 Correct 1045 ms 204748 KB Output is correct
3 Correct 115 ms 35412 KB Output is correct
4 Correct 1602 ms 205176 KB Output is correct
5 Correct 1470 ms 205140 KB Output is correct
6 Correct 105 ms 27220 KB Output is correct
7 Correct 594 ms 46560 KB Output is correct
8 Correct 539 ms 95436 KB Output is correct
9 Correct 559 ms 95260 KB Output is correct
10 Correct 959 ms 155592 KB Output is correct
11 Correct 975 ms 155444 KB Output is correct
12 Correct 30 ms 9296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 23892 KB Output is correct
2 Correct 1045 ms 204748 KB Output is correct
3 Correct 115 ms 35412 KB Output is correct
4 Correct 1602 ms 205176 KB Output is correct
5 Correct 1470 ms 205140 KB Output is correct
6 Correct 105 ms 27220 KB Output is correct
7 Correct 594 ms 46560 KB Output is correct
8 Correct 539 ms 95436 KB Output is correct
9 Correct 559 ms 95260 KB Output is correct
10 Correct 959 ms 155592 KB Output is correct
11 Correct 975 ms 155444 KB Output is correct
12 Correct 30 ms 9296 KB Output is correct
13 Correct 108 ms 20048 KB Output is correct
14 Correct 1607 ms 205452 KB Output is correct
15 Correct 1554 ms 205384 KB Output is correct
16 Correct 1116 ms 204440 KB Output is correct
17 Correct 1050 ms 165380 KB Output is correct
18 Correct 1358 ms 150580 KB Output is correct
19 Correct 1302 ms 143952 KB Output is correct
20 Correct 1355 ms 203684 KB Output is correct
21 Correct 583 ms 101144 KB Output is correct
22 Correct 831 ms 122452 KB Output is correct
23 Correct 402 ms 75088 KB Output is correct
24 Correct 1345 ms 171892 KB Output is correct
25 Correct 1486 ms 201040 KB Output is correct
26 Correct 1324 ms 198224 KB Output is correct
27 Correct 20 ms 8024 KB Output is correct
28 Correct 55 ms 18772 KB Output is correct
29 Correct 101 ms 30548 KB Output is correct
30 Correct 34 ms 9300 KB Output is correct
31 Correct 102 ms 28272 KB Output is correct
32 Correct 166 ms 47688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 37 ms 12636 KB Output is correct
3 Correct 3 ms 4952 KB Output is correct
4 Correct 57 ms 16692 KB Output is correct
5 Correct 18 ms 9564 KB Output is correct
6 Correct 4 ms 5212 KB Output is correct
7 Correct 4 ms 5468 KB Output is correct
8 Correct 38 ms 16756 KB Output is correct
9 Correct 44 ms 13764 KB Output is correct
10 Correct 44 ms 13796 KB Output is correct
11 Correct 45 ms 14312 KB Output is correct
12 Correct 50 ms 14684 KB Output is correct
13 Correct 21 ms 10224 KB Output is correct
14 Correct 3 ms 5464 KB Output is correct
15 Correct 5 ms 5976 KB Output is correct
16 Correct 9 ms 7260 KB Output is correct
17 Correct 22 ms 10852 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 ms 4952 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 41 ms 16732 KB Output is correct
22 Correct 660 ms 124632 KB Output is correct
23 Correct 854 ms 124752 KB Output is correct
24 Correct 666 ms 124880 KB Output is correct
25 Correct 661 ms 124244 KB Output is correct
26 Correct 590 ms 124372 KB Output is correct
27 Correct 922 ms 124756 KB Output is correct
28 Correct 627 ms 123744 KB Output is correct
29 Correct 20 ms 8540 KB Output is correct
30 Correct 859 ms 124752 KB Output is correct
31 Correct 904 ms 124844 KB Output is correct
32 Correct 14 ms 7004 KB Output is correct
33 Correct 29 ms 8896 KB Output is correct
34 Correct 296 ms 53264 KB Output is correct
35 Correct 214 ms 41828 KB Output is correct
36 Correct 135 ms 30052 KB Output is correct
37 Correct 96 ms 24400 KB Output is correct
38 Correct 64 ms 16712 KB Output is correct
39 Correct 149 ms 42364 KB Output is correct
40 Correct 197 ms 54096 KB Output is correct
41 Correct 280 ms 77440 KB Output is correct
42 Correct 492 ms 123988 KB Output is correct
43 Correct 474 ms 123984 KB Output is correct
44 Correct 496 ms 123984 KB Output is correct
45 Correct 460 ms 123976 KB Output is correct
46 Correct 484 ms 123932 KB Output is correct
47 Correct 621 ms 123924 KB Output is correct
48 Correct 626 ms 123984 KB Output is correct
49 Correct 583 ms 124092 KB Output is correct
50 Correct 607 ms 124372 KB Output is correct
51 Correct 22 ms 8360 KB Output is correct
52 Correct 21 ms 8400 KB Output is correct
53 Correct 21 ms 8540 KB Output is correct
54 Correct 423 ms 124732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 19 ms 7996 KB Output is correct
3 Correct 26 ms 10324 KB Output is correct
4 Correct 22 ms 8796 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 17 ms 8112 KB Output is correct
7 Correct 27 ms 8796 KB Output is correct
8 Correct 25 ms 8796 KB Output is correct
9 Correct 28 ms 10068 KB Output is correct
10 Correct 24 ms 8760 KB Output is correct
11 Correct 25 ms 8276 KB Output is correct
12 Correct 26 ms 8784 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 20 ms 8120 KB Output is correct
15 Correct 29 ms 8784 KB Output is correct
16 Correct 28 ms 9044 KB Output is correct
17 Correct 30 ms 10068 KB Output is correct
18 Correct 28 ms 8796 KB Output is correct
19 Correct 28 ms 8796 KB Output is correct
20 Correct 30 ms 10068 KB Output is correct
21 Correct 15 ms 7004 KB Output is correct
22 Correct 14 ms 6748 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 37 ms 12636 KB Output is correct
25 Correct 3 ms 4952 KB Output is correct
26 Correct 57 ms 16692 KB Output is correct
27 Correct 18 ms 9564 KB Output is correct
28 Correct 4 ms 5212 KB Output is correct
29 Correct 4 ms 5468 KB Output is correct
30 Correct 38 ms 16756 KB Output is correct
31 Correct 44 ms 13764 KB Output is correct
32 Correct 44 ms 13796 KB Output is correct
33 Correct 45 ms 14312 KB Output is correct
34 Correct 50 ms 14684 KB Output is correct
35 Correct 21 ms 10224 KB Output is correct
36 Correct 3 ms 5464 KB Output is correct
37 Correct 5 ms 5976 KB Output is correct
38 Correct 9 ms 7260 KB Output is correct
39 Correct 22 ms 10852 KB Output is correct
40 Correct 3 ms 5008 KB Output is correct
41 Correct 3 ms 4952 KB Output is correct
42 Correct 3 ms 4956 KB Output is correct
43 Correct 41 ms 16732 KB Output is correct
44 Correct 143 ms 23892 KB Output is correct
45 Correct 1045 ms 204748 KB Output is correct
46 Correct 115 ms 35412 KB Output is correct
47 Correct 1602 ms 205176 KB Output is correct
48 Correct 1470 ms 205140 KB Output is correct
49 Correct 105 ms 27220 KB Output is correct
50 Correct 594 ms 46560 KB Output is correct
51 Correct 539 ms 95436 KB Output is correct
52 Correct 559 ms 95260 KB Output is correct
53 Correct 959 ms 155592 KB Output is correct
54 Correct 975 ms 155444 KB Output is correct
55 Correct 30 ms 9296 KB Output is correct
56 Correct 108 ms 20048 KB Output is correct
57 Correct 1607 ms 205452 KB Output is correct
58 Correct 1554 ms 205384 KB Output is correct
59 Correct 1116 ms 204440 KB Output is correct
60 Correct 1050 ms 165380 KB Output is correct
61 Correct 1358 ms 150580 KB Output is correct
62 Correct 1302 ms 143952 KB Output is correct
63 Correct 1355 ms 203684 KB Output is correct
64 Correct 583 ms 101144 KB Output is correct
65 Correct 831 ms 122452 KB Output is correct
66 Correct 402 ms 75088 KB Output is correct
67 Correct 1345 ms 171892 KB Output is correct
68 Correct 1486 ms 201040 KB Output is correct
69 Correct 1324 ms 198224 KB Output is correct
70 Correct 20 ms 8024 KB Output is correct
71 Correct 55 ms 18772 KB Output is correct
72 Correct 101 ms 30548 KB Output is correct
73 Correct 34 ms 9300 KB Output is correct
74 Correct 102 ms 28272 KB Output is correct
75 Correct 166 ms 47688 KB Output is correct
76 Correct 660 ms 124632 KB Output is correct
77 Correct 854 ms 124752 KB Output is correct
78 Correct 666 ms 124880 KB Output is correct
79 Correct 661 ms 124244 KB Output is correct
80 Correct 590 ms 124372 KB Output is correct
81 Correct 922 ms 124756 KB Output is correct
82 Correct 627 ms 123744 KB Output is correct
83 Correct 20 ms 8540 KB Output is correct
84 Correct 859 ms 124752 KB Output is correct
85 Correct 904 ms 124844 KB Output is correct
86 Correct 14 ms 7004 KB Output is correct
87 Correct 29 ms 8896 KB Output is correct
88 Correct 296 ms 53264 KB Output is correct
89 Correct 214 ms 41828 KB Output is correct
90 Correct 135 ms 30052 KB Output is correct
91 Correct 96 ms 24400 KB Output is correct
92 Correct 64 ms 16712 KB Output is correct
93 Correct 149 ms 42364 KB Output is correct
94 Correct 197 ms 54096 KB Output is correct
95 Correct 280 ms 77440 KB Output is correct
96 Correct 492 ms 123988 KB Output is correct
97 Correct 474 ms 123984 KB Output is correct
98 Correct 496 ms 123984 KB Output is correct
99 Correct 460 ms 123976 KB Output is correct
100 Correct 484 ms 123932 KB Output is correct
101 Correct 621 ms 123924 KB Output is correct
102 Correct 626 ms 123984 KB Output is correct
103 Correct 583 ms 124092 KB Output is correct
104 Correct 607 ms 124372 KB Output is correct
105 Correct 22 ms 8360 KB Output is correct
106 Correct 21 ms 8400 KB Output is correct
107 Correct 21 ms 8540 KB Output is correct
108 Correct 423 ms 124732 KB Output is correct
109 Correct 1142 ms 163260 KB Output is correct
110 Correct 1440 ms 204548 KB Output is correct
111 Correct 61 ms 14420 KB Output is correct
112 Correct 1062 ms 203956 KB Output is correct
113 Correct 563 ms 117072 KB Output is correct
114 Correct 1492 ms 204372 KB Output is correct
115 Correct 145 ms 25684 KB Output is correct
116 Correct 1246 ms 203688 KB Output is correct
117 Correct 1560 ms 165272 KB Output is correct
118 Correct 104 ms 13948 KB Output is correct
119 Correct 595 ms 46172 KB Output is correct
120 Correct 330 ms 42328 KB Output is correct
121 Correct 758 ms 205168 KB Output is correct
122 Correct 618 ms 105040 KB Output is correct
123 Correct 325 ms 57172 KB Output is correct
124 Correct 230 ms 46932 KB Output is correct
125 Correct 169 ms 36120 KB Output is correct
126 Correct 270 ms 67408 KB Output is correct
127 Correct 343 ms 86864 KB Output is correct
128 Correct 530 ms 126036 KB Output is correct
129 Correct 846 ms 203860 KB Output is correct
130 Correct 799 ms 203860 KB Output is correct
131 Correct 807 ms 203604 KB Output is correct
132 Correct 811 ms 203604 KB Output is correct
133 Correct 799 ms 203600 KB Output is correct
134 Correct 799 ms 203600 KB Output is correct
135 Correct 965 ms 203604 KB Output is correct
136 Correct 950 ms 203672 KB Output is correct
137 Correct 991 ms 203856 KB Output is correct
138 Correct 911 ms 203860 KB Output is correct
139 Correct 1061 ms 204108 KB Output is correct
140 Correct 36 ms 11084 KB Output is correct
141 Correct 34 ms 11084 KB Output is correct
142 Correct 38 ms 11112 KB Output is correct
143 Correct 701 ms 205136 KB Output is correct