Submission #928510

# Submission time Handle Problem Language Result Execution time Memory
928510 2024-02-16T13:57:50 Z User0069 Swapping Cities (APIO20_swap) C++17
30 / 100
404 ms 111132 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 24 ms 60116 KB Output is correct
2 Correct 12 ms 59996 KB Output is correct
3 Correct 12 ms 59996 KB Output is correct
4 Correct 14 ms 60196 KB Output is correct
5 Correct 13 ms 60252 KB Output is correct
6 Correct 14 ms 59996 KB Output is correct
7 Correct 14 ms 60108 KB Output is correct
8 Correct 23 ms 60504 KB Output is correct
9 Correct 119 ms 96480 KB Output is correct
10 Correct 148 ms 105848 KB Output is correct
11 Correct 148 ms 102996 KB Output is correct
12 Correct 155 ms 106224 KB Output is correct
13 Correct 141 ms 107428 KB Output is correct
14 Correct 142 ms 96104 KB Output is correct
15 Correct 313 ms 109568 KB Output is correct
16 Correct 312 ms 105708 KB Output is correct
17 Correct 283 ms 111132 KB Output is correct
18 Correct 353 ms 110308 KB Output is correct
19 Incorrect 75 ms 70484 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 60116 KB Output is correct
2 Correct 12 ms 59996 KB Output is correct
3 Correct 160 ms 103620 KB Output is correct
4 Correct 184 ms 106380 KB Output is correct
5 Correct 163 ms 106228 KB Output is correct
6 Correct 201 ms 105936 KB Output is correct
7 Correct 174 ms 106384 KB Output is correct
8 Correct 173 ms 103696 KB Output is correct
9 Correct 165 ms 106052 KB Output is correct
10 Correct 170 ms 104244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 60116 KB Output is correct
2 Correct 12 ms 59996 KB Output is correct
3 Correct 12 ms 59996 KB Output is correct
4 Correct 14 ms 60196 KB Output is correct
5 Correct 13 ms 60252 KB Output is correct
6 Correct 14 ms 59996 KB Output is correct
7 Correct 14 ms 60108 KB Output is correct
8 Correct 23 ms 60504 KB Output is correct
9 Incorrect 22 ms 59996 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 59996 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 60116 KB Output is correct
2 Correct 12 ms 59996 KB Output is correct
3 Correct 12 ms 59996 KB Output is correct
4 Correct 14 ms 60196 KB Output is correct
5 Correct 13 ms 60252 KB Output is correct
6 Correct 14 ms 59996 KB Output is correct
7 Correct 14 ms 60108 KB Output is correct
8 Correct 23 ms 60504 KB Output is correct
9 Correct 119 ms 96480 KB Output is correct
10 Correct 148 ms 105848 KB Output is correct
11 Correct 148 ms 102996 KB Output is correct
12 Correct 155 ms 106224 KB Output is correct
13 Correct 141 ms 107428 KB Output is correct
14 Correct 142 ms 96104 KB Output is correct
15 Correct 313 ms 109568 KB Output is correct
16 Correct 312 ms 105708 KB Output is correct
17 Correct 283 ms 111132 KB Output is correct
18 Correct 353 ms 110308 KB Output is correct
19 Correct 160 ms 103620 KB Output is correct
20 Correct 184 ms 106380 KB Output is correct
21 Correct 163 ms 106228 KB Output is correct
22 Correct 201 ms 105936 KB Output is correct
23 Correct 174 ms 106384 KB Output is correct
24 Correct 173 ms 103696 KB Output is correct
25 Correct 165 ms 106052 KB Output is correct
26 Correct 170 ms 104244 KB Output is correct
27 Correct 19 ms 59984 KB Output is correct
28 Correct 14 ms 60252 KB Output is correct
29 Correct 13 ms 60064 KB Output is correct
30 Correct 26 ms 59988 KB Output is correct
31 Correct 15 ms 60140 KB Output is correct
32 Correct 13 ms 59996 KB Output is correct
33 Correct 13 ms 60248 KB Output is correct
34 Correct 13 ms 60252 KB Output is correct
35 Correct 13 ms 59996 KB Output is correct
36 Correct 27 ms 65508 KB Output is correct
37 Correct 168 ms 105408 KB Output is correct
38 Correct 159 ms 103608 KB Output is correct
39 Correct 143 ms 101944 KB Output is correct
40 Correct 141 ms 101304 KB Output is correct
41 Correct 138 ms 101028 KB Output is correct
42 Correct 131 ms 98336 KB Output is correct
43 Correct 189 ms 104084 KB Output is correct
44 Correct 154 ms 104940 KB Output is correct
45 Correct 139 ms 106064 KB Output is correct
46 Correct 139 ms 101464 KB Output is correct
47 Correct 30 ms 65768 KB Output is correct
48 Correct 404 ms 108128 KB Output is correct
49 Correct 336 ms 106780 KB Output is correct
50 Correct 317 ms 106012 KB Output is correct
51 Correct 303 ms 105772 KB Output is correct
52 Correct 262 ms 102848 KB Output is correct
53 Correct 184 ms 93536 KB Output is correct
54 Correct 336 ms 108508 KB Output is correct
55 Correct 340 ms 108548 KB Output is correct
56 Correct 302 ms 110700 KB Output is correct
57 Correct 258 ms 106168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 59996 KB Output isn't correct
2 Halted 0 ms 0 KB -