Submission #928406

# Submission time Handle Problem Language Result Execution time Memory
928406 2024-02-16T10:35:05 Z User0069 Swapping Cities (APIO20_swap) C++17
100 / 100
414 ms 108848 KB
#include<bits/stdc++.h>
//#include"swap.h"
#define taskname "vcl"
#define el '\n'
#define fi first
#define sc second
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
//#define int ll
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
#define Faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=2e6+1;
const int INF=1e9+7;
const int mod=1e9+7;
int n,m,d[maxn],cnt[maxn],par[maxn],h[maxn],up[maxn][20],f[maxn][20],g[maxn][20],laz[maxn][20];
struct edge
{
    int u,v,w,used;
}e[maxn];
vector<int> adj[maxn];
bool operator <(edge a,edge b)
{
    return a.w<b.w;
}
int fp(int x)
{
    if(par[x]<0) return x;
    return par[x]=fp(par[x]);
}
void uni(int a,int b)
{
    a=fp(a);
    b=fp(b);
    if(a==b) return;
    if(par[a]>par[b]) swap(a,b);
    par[a]+=par[b];
    par[b]=a;
}
void dfs(int x,int par)
{
    h[x]=h[par]+1;
    up[x][0]=par;
    for(int i:adj[x])
    {
        int y=e[i].u+e[i].v-x;
        if(y==par) continue;
        f[y][0]=e[i].w;
        dfs(y,x);
    }
}
struct ele
{
    int des,dis;
};
bool operator <(ele a,ele b)
{
    return a.dis>b.dis;
}
void dijkstra()
{
    priority_queue<ele> pq;
    for(int i=1;i<=n;i++)
    {
        pq.push({i,d[i]});
        cnt[i]=0;
    }
    while(!pq.empty())
    {
        int des=pq.top().des;
        int dis=pq.top().dis;
        pq.pop();
        if(cnt[des]) continue;
        cnt[des]=1;
        d[des]=dis;
        for(int i:adj[des])
        {
            int des1=e[i].u+e[i].v-des;
            pq.push({des1,max(dis,e[i].w)});
        }
    }
    for(int i=1;i<=n;i++)
    {
        g[i][0]=d[i];
    }
}
void update(int x,int y,int w)
{
    if(h[x]!=h[y])
    {
        if(h[x]<h[y]) swap(x,y);
        int dif=h[x]-h[y];
        for(int i=0;i<20;i++)
        {
            if(dif&(1<<i))
            {
                laz[x][i]=min(laz[x][i],w);
                x=up[x][i];
            }
        }
    }
    if(x!=y)
    {
        int lg=__lg(h[x]);
        for(int i=lg;i>=0;i--)
        {
            if(up[x][i]!=up[y][i])
            {
                laz[x][i]=min(laz[x][i],w);
                laz[y][i]=min(laz[y][i],w);
                x=up[x][i];
                y=up[y][i];
            }
        }
        laz[x][0]=min(laz[x][0],w);
        laz[y][0]=min(laz[y][0],w);
        x=up[x][0];
    }
    laz[x][0]=min(laz[x][0],w);
}
void init(int _n,int _m,vector<int> _u,vector<int> _v,vector<int> _w)
{
    n=_n;
    m=_m;
    for(int i=0;i<m;i++)
    {
        e[i+1].u=++_u[i];
        e[i+1].v=++_v[i];
        e[i+1].w=_w[i];
        e[i+1].used=0;
    }
    sort(e+1,e+m+1);
    for(int i=1;i<=n;i++)
    {
        d[i]=INF;
        par[i]=-1;
    }
    for(int i=1;i<=m;i++)
    {
        if(++cnt[e[i].u]==3) d[e[i].u]=e[i].w;
        if(++cnt[e[i].v]==3) d[e[i].v]=e[i].w;
        if(fp(e[i].u)!=fp(e[i].v))
        {
            uni(e[i].u,e[i].v);
            adj[e[i].u].push_back(i);
            adj[e[i].v].push_back(i);
            e[i].used=1;
        }
    }
    dfs(1,0);
    dijkstra();
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<20;j++)
        {
            laz[i][j]=INF;
        }
    }
    for(int j=1;j<20;j++)
    {
        for(int i=1;i<=n;i++)
        {
            up[i][j]=up[up[i][j-1]][j-1];
        }
    }
    for(int i=1;i<=m;i++)
    {
        if(e[i].used==0)
        {
           update(e[i].u,e[i].v,e[i].w);
        }
    }
    for(int j=1;j<20;j++)
    {
        for(int i=1;i<=n;i++)
        {
            f[i][j]=max(f[i][j-1],f[up[i][j-1]][j-1]);
            g[i][j]=min(g[i][j-1],g[up[i][j-1]][j-1]);
        }
    }
    for(int j=19;j;j--)
    {
        for(int i=1;i<=n;i++)
        {
            laz[i][j-1]=min(laz[i][j-1],laz[i][j]);
            laz[up[i][j-1]][j-1]=min(laz[up[i][j-1]][j-1],laz[i][j]);
        }
    }
}
int getMinimumFuelCapacity(int x,int y)
{
  ++x;
  ++y;
  int w1=0,w2=INF;
    if(h[x]!=h[y])
    {
        if(h[x]<h[y]) swap(x,y);
        int dif=h[x]-h[y];
        for(int i=0;i<20;i++)
        {
            if(dif&(1<<i))
            {
                w1=max(w1,f[x][i]);
                w2=min(w2,g[x][i]);
                x=up[x][i];
            }
        }
    }
    if(x!=y)
    {
        int lg=__lg(h[x]);
        for(int i=lg;i>=0;i--)
        {
            if(up[x][i]!=up[y][i])
            {
                w1=max(w1,f[x][i]);
                w2=min(w2,g[x][i]);
                w1=max(w1,f[y][i]);
                w2=min(w2,g[y][i]);
                x=up[x][i];
                y=up[y][i];
            }
        }
        w1=max(w1,f[x][0]);
        w1=max(w1,f[y][0]);
        w2=min(w2,g[x][0]);
        w2=min(w2,g[y][0]);
        x=up[x][0];
    }
    w2=min(w2,g[x][0]);
    int ans=max(w1,min(w2,laz[x][0]));
    if(ans==INF) return -1;
    else return ans;
}
//signed main()
//{
//    if (fopen(taskname".INP","r"))
//    {
//        freopen(taskname".INP","r",stdin);
//        freopen(taskname"1.OUT","w",stdout);
//    }
//    Faster
//    int n,m,q;
//    vector<int> v1,v2,v3;
//    cin>>n>>m>>q;
//    for(int i=1; i<=m; i++)
//    {
//        int u,v,w;
//        cin>>u>>v>>w;
//        v1.push_back(u);
//        v2.push_back(v);
//        v3.push_back(w);
//    }
//    init(n,m,v1,v2,v3);
//    while(q--)
//    {
//        int u,v;
//        cin>>u>>v;
//        cout<<getMinimumFuelCapacity(++u,++v)<<"\n";
//    }
//}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 59996 KB Output is correct
2 Correct 14 ms 59992 KB Output is correct
3 Correct 13 ms 59996 KB Output is correct
4 Correct 12 ms 59992 KB Output is correct
5 Correct 13 ms 60176 KB Output is correct
6 Correct 14 ms 59996 KB Output is correct
7 Correct 13 ms 60252 KB Output is correct
8 Correct 13 ms 60052 KB Output is correct
9 Correct 117 ms 95480 KB Output is correct
10 Correct 143 ms 104368 KB Output is correct
11 Correct 142 ms 101852 KB Output is correct
12 Correct 159 ms 104888 KB Output is correct
13 Correct 142 ms 106188 KB Output is correct
14 Correct 119 ms 94948 KB Output is correct
15 Correct 323 ms 107352 KB Output is correct
16 Correct 290 ms 103608 KB Output is correct
17 Correct 294 ms 108832 KB Output is correct
18 Correct 279 ms 107860 KB Output is correct
19 Correct 78 ms 70352 KB Output is correct
20 Correct 315 ms 107564 KB Output is correct
21 Correct 303 ms 105116 KB Output is correct
22 Correct 346 ms 108368 KB Output is correct
23 Correct 291 ms 108848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 59996 KB Output is correct
2 Correct 14 ms 59992 KB Output is correct
3 Correct 161 ms 100124 KB Output is correct
4 Correct 164 ms 102288 KB Output is correct
5 Correct 168 ms 102576 KB Output is correct
6 Correct 162 ms 102288 KB Output is correct
7 Correct 179 ms 102336 KB Output is correct
8 Correct 157 ms 100148 KB Output is correct
9 Correct 164 ms 102352 KB Output is correct
10 Correct 155 ms 100176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 59996 KB Output is correct
2 Correct 14 ms 59992 KB Output is correct
3 Correct 13 ms 59996 KB Output is correct
4 Correct 12 ms 59992 KB Output is correct
5 Correct 13 ms 60176 KB Output is correct
6 Correct 14 ms 59996 KB Output is correct
7 Correct 13 ms 60252 KB Output is correct
8 Correct 13 ms 60052 KB Output is correct
9 Correct 13 ms 59996 KB Output is correct
10 Correct 14 ms 60224 KB Output is correct
11 Correct 13 ms 60240 KB Output is correct
12 Correct 14 ms 60124 KB Output is correct
13 Correct 13 ms 60204 KB Output is correct
14 Correct 14 ms 59996 KB Output is correct
15 Correct 13 ms 60224 KB Output is correct
16 Correct 14 ms 59996 KB Output is correct
17 Correct 13 ms 60252 KB Output is correct
18 Correct 13 ms 60228 KB Output is correct
19 Correct 13 ms 60196 KB Output is correct
20 Correct 13 ms 59996 KB Output is correct
21 Correct 13 ms 59992 KB Output is correct
22 Correct 13 ms 59992 KB Output is correct
23 Correct 13 ms 59996 KB Output is correct
24 Correct 13 ms 59996 KB Output is correct
25 Correct 13 ms 59996 KB Output is correct
26 Correct 15 ms 60252 KB Output is correct
27 Correct 13 ms 60252 KB Output is correct
28 Correct 14 ms 59996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 59996 KB Output is correct
2 Correct 19 ms 59996 KB Output is correct
3 Correct 14 ms 59992 KB Output is correct
4 Correct 13 ms 59996 KB Output is correct
5 Correct 12 ms 59992 KB Output is correct
6 Correct 13 ms 60176 KB Output is correct
7 Correct 14 ms 59996 KB Output is correct
8 Correct 13 ms 60252 KB Output is correct
9 Correct 13 ms 60052 KB Output is correct
10 Correct 117 ms 95480 KB Output is correct
11 Correct 143 ms 104368 KB Output is correct
12 Correct 142 ms 101852 KB Output is correct
13 Correct 159 ms 104888 KB Output is correct
14 Correct 142 ms 106188 KB Output is correct
15 Correct 14 ms 60224 KB Output is correct
16 Correct 13 ms 60240 KB Output is correct
17 Correct 14 ms 60124 KB Output is correct
18 Correct 13 ms 60204 KB Output is correct
19 Correct 14 ms 59996 KB Output is correct
20 Correct 13 ms 60224 KB Output is correct
21 Correct 14 ms 59996 KB Output is correct
22 Correct 13 ms 60252 KB Output is correct
23 Correct 13 ms 60228 KB Output is correct
24 Correct 13 ms 60196 KB Output is correct
25 Correct 13 ms 59996 KB Output is correct
26 Correct 13 ms 59992 KB Output is correct
27 Correct 13 ms 59992 KB Output is correct
28 Correct 13 ms 59996 KB Output is correct
29 Correct 13 ms 59996 KB Output is correct
30 Correct 13 ms 59996 KB Output is correct
31 Correct 15 ms 60252 KB Output is correct
32 Correct 13 ms 60252 KB Output is correct
33 Correct 14 ms 59996 KB Output is correct
34 Correct 25 ms 64996 KB Output is correct
35 Correct 174 ms 104048 KB Output is correct
36 Correct 148 ms 102096 KB Output is correct
37 Correct 140 ms 100536 KB Output is correct
38 Correct 136 ms 100024 KB Output is correct
39 Correct 142 ms 100012 KB Output is correct
40 Correct 119 ms 97216 KB Output is correct
41 Correct 155 ms 103100 KB Output is correct
42 Correct 159 ms 103104 KB Output is correct
43 Correct 143 ms 104124 KB Output is correct
44 Correct 139 ms 99420 KB Output is correct
45 Correct 136 ms 93444 KB Output is correct
46 Correct 145 ms 100608 KB Output is correct
47 Correct 139 ms 99228 KB Output is correct
48 Correct 149 ms 99472 KB Output is correct
49 Correct 68 ms 67920 KB Output is correct
50 Correct 62 ms 67372 KB Output is correct
51 Correct 119 ms 88356 KB Output is correct
52 Correct 212 ms 104192 KB Output is correct
53 Correct 181 ms 103328 KB Output is correct
54 Correct 226 ms 106428 KB Output is correct
55 Correct 156 ms 105432 KB Output is correct
56 Correct 184 ms 103148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 59996 KB Output is correct
2 Correct 14 ms 59992 KB Output is correct
3 Correct 13 ms 59996 KB Output is correct
4 Correct 12 ms 59992 KB Output is correct
5 Correct 13 ms 60176 KB Output is correct
6 Correct 14 ms 59996 KB Output is correct
7 Correct 13 ms 60252 KB Output is correct
8 Correct 13 ms 60052 KB Output is correct
9 Correct 117 ms 95480 KB Output is correct
10 Correct 143 ms 104368 KB Output is correct
11 Correct 142 ms 101852 KB Output is correct
12 Correct 159 ms 104888 KB Output is correct
13 Correct 142 ms 106188 KB Output is correct
14 Correct 119 ms 94948 KB Output is correct
15 Correct 323 ms 107352 KB Output is correct
16 Correct 290 ms 103608 KB Output is correct
17 Correct 294 ms 108832 KB Output is correct
18 Correct 279 ms 107860 KB Output is correct
19 Correct 161 ms 100124 KB Output is correct
20 Correct 164 ms 102288 KB Output is correct
21 Correct 168 ms 102576 KB Output is correct
22 Correct 162 ms 102288 KB Output is correct
23 Correct 179 ms 102336 KB Output is correct
24 Correct 157 ms 100148 KB Output is correct
25 Correct 164 ms 102352 KB Output is correct
26 Correct 155 ms 100176 KB Output is correct
27 Correct 14 ms 60224 KB Output is correct
28 Correct 13 ms 60240 KB Output is correct
29 Correct 14 ms 60124 KB Output is correct
30 Correct 13 ms 60204 KB Output is correct
31 Correct 14 ms 59996 KB Output is correct
32 Correct 13 ms 60224 KB Output is correct
33 Correct 14 ms 59996 KB Output is correct
34 Correct 13 ms 60252 KB Output is correct
35 Correct 13 ms 60228 KB Output is correct
36 Correct 25 ms 64996 KB Output is correct
37 Correct 174 ms 104048 KB Output is correct
38 Correct 148 ms 102096 KB Output is correct
39 Correct 140 ms 100536 KB Output is correct
40 Correct 136 ms 100024 KB Output is correct
41 Correct 142 ms 100012 KB Output is correct
42 Correct 119 ms 97216 KB Output is correct
43 Correct 155 ms 103100 KB Output is correct
44 Correct 159 ms 103104 KB Output is correct
45 Correct 143 ms 104124 KB Output is correct
46 Correct 139 ms 99420 KB Output is correct
47 Correct 30 ms 65516 KB Output is correct
48 Correct 337 ms 104480 KB Output is correct
49 Correct 316 ms 103224 KB Output is correct
50 Correct 312 ms 102604 KB Output is correct
51 Correct 282 ms 102096 KB Output is correct
52 Correct 254 ms 99444 KB Output is correct
53 Correct 168 ms 90344 KB Output is correct
54 Correct 341 ms 104476 KB Output is correct
55 Correct 330 ms 105076 KB Output is correct
56 Correct 283 ms 107044 KB Output is correct
57 Correct 270 ms 102388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 59996 KB Output is correct
2 Correct 19 ms 59996 KB Output is correct
3 Correct 14 ms 59992 KB Output is correct
4 Correct 13 ms 59996 KB Output is correct
5 Correct 12 ms 59992 KB Output is correct
6 Correct 13 ms 60176 KB Output is correct
7 Correct 14 ms 59996 KB Output is correct
8 Correct 13 ms 60252 KB Output is correct
9 Correct 13 ms 60052 KB Output is correct
10 Correct 117 ms 95480 KB Output is correct
11 Correct 143 ms 104368 KB Output is correct
12 Correct 142 ms 101852 KB Output is correct
13 Correct 159 ms 104888 KB Output is correct
14 Correct 142 ms 106188 KB Output is correct
15 Correct 119 ms 94948 KB Output is correct
16 Correct 323 ms 107352 KB Output is correct
17 Correct 290 ms 103608 KB Output is correct
18 Correct 294 ms 108832 KB Output is correct
19 Correct 279 ms 107860 KB Output is correct
20 Correct 161 ms 100124 KB Output is correct
21 Correct 164 ms 102288 KB Output is correct
22 Correct 168 ms 102576 KB Output is correct
23 Correct 162 ms 102288 KB Output is correct
24 Correct 179 ms 102336 KB Output is correct
25 Correct 157 ms 100148 KB Output is correct
26 Correct 164 ms 102352 KB Output is correct
27 Correct 155 ms 100176 KB Output is correct
28 Correct 14 ms 60224 KB Output is correct
29 Correct 13 ms 60240 KB Output is correct
30 Correct 14 ms 60124 KB Output is correct
31 Correct 13 ms 60204 KB Output is correct
32 Correct 14 ms 59996 KB Output is correct
33 Correct 13 ms 60224 KB Output is correct
34 Correct 14 ms 59996 KB Output is correct
35 Correct 13 ms 60252 KB Output is correct
36 Correct 13 ms 60228 KB Output is correct
37 Correct 25 ms 64996 KB Output is correct
38 Correct 174 ms 104048 KB Output is correct
39 Correct 148 ms 102096 KB Output is correct
40 Correct 140 ms 100536 KB Output is correct
41 Correct 136 ms 100024 KB Output is correct
42 Correct 142 ms 100012 KB Output is correct
43 Correct 119 ms 97216 KB Output is correct
44 Correct 155 ms 103100 KB Output is correct
45 Correct 159 ms 103104 KB Output is correct
46 Correct 143 ms 104124 KB Output is correct
47 Correct 139 ms 99420 KB Output is correct
48 Correct 30 ms 65516 KB Output is correct
49 Correct 337 ms 104480 KB Output is correct
50 Correct 316 ms 103224 KB Output is correct
51 Correct 312 ms 102604 KB Output is correct
52 Correct 282 ms 102096 KB Output is correct
53 Correct 254 ms 99444 KB Output is correct
54 Correct 168 ms 90344 KB Output is correct
55 Correct 341 ms 104476 KB Output is correct
56 Correct 330 ms 105076 KB Output is correct
57 Correct 283 ms 107044 KB Output is correct
58 Correct 270 ms 102388 KB Output is correct
59 Correct 78 ms 70352 KB Output is correct
60 Correct 315 ms 107564 KB Output is correct
61 Correct 303 ms 105116 KB Output is correct
62 Correct 346 ms 108368 KB Output is correct
63 Correct 291 ms 108848 KB Output is correct
64 Correct 13 ms 60196 KB Output is correct
65 Correct 13 ms 59996 KB Output is correct
66 Correct 13 ms 59992 KB Output is correct
67 Correct 13 ms 59992 KB Output is correct
68 Correct 13 ms 59996 KB Output is correct
69 Correct 13 ms 59996 KB Output is correct
70 Correct 13 ms 59996 KB Output is correct
71 Correct 15 ms 60252 KB Output is correct
72 Correct 13 ms 60252 KB Output is correct
73 Correct 14 ms 59996 KB Output is correct
74 Correct 136 ms 93444 KB Output is correct
75 Correct 145 ms 100608 KB Output is correct
76 Correct 139 ms 99228 KB Output is correct
77 Correct 149 ms 99472 KB Output is correct
78 Correct 68 ms 67920 KB Output is correct
79 Correct 62 ms 67372 KB Output is correct
80 Correct 119 ms 88356 KB Output is correct
81 Correct 212 ms 104192 KB Output is correct
82 Correct 181 ms 103328 KB Output is correct
83 Correct 226 ms 106428 KB Output is correct
84 Correct 156 ms 105432 KB Output is correct
85 Correct 184 ms 103148 KB Output is correct
86 Correct 69 ms 75364 KB Output is correct
87 Correct 326 ms 102952 KB Output is correct
88 Correct 304 ms 103220 KB Output is correct
89 Correct 229 ms 99416 KB Output is correct
90 Correct 131 ms 72404 KB Output is correct
91 Correct 143 ms 72720 KB Output is correct
92 Correct 216 ms 90844 KB Output is correct
93 Correct 360 ms 107112 KB Output is correct
94 Correct 269 ms 105344 KB Output is correct
95 Correct 414 ms 108220 KB Output is correct
96 Correct 303 ms 105500 KB Output is correct
97 Correct 276 ms 102920 KB Output is correct