Submission #1093000

# Submission time Handle Problem Language Result Execution time Memory
1093000 2024-09-25T16:35:30 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
230 ms 65360 KB
 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
//#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=4e5+69;
const ll offset=1e9;
const ll block_sz=317;
const ll inf=1e18;
const ll mod=1e9+7;
int n,par[maxn],m,deg[maxn],val[maxn],up[maxn][20],in[maxn],out[maxn],Time,ans[maxn];
bool ok[maxn];
vector<int> adj[maxn];
pair<int,pii> Edge[maxn];
map<pii,int> L;
int Find(int u)
{
    return (par[u]<0?u:par[u]=Find(par[u]));
}
void AddEdge(int u,int v,int w)
{
    val[n]=w;
    deg[u]++;
    deg[v]++;
    if (deg[u]>2 || deg[v]>2) ok[n]=1;
    u=Find(u);
    v=Find(v);
    par[n]+=par[u];
    par[u]=n;
    adj[n].pb(u);
    if (u!=v)
    {
        adj[n].pb(v);
        par[n]+=par[v];
        par[v]=n;
    }
    ok[n]=ok[n]|ok[u]|ok[v];
    //cerr<<n<<' '<<ok[n]<<' '<<val[n]<< " kk\n";
    n++;
}
void dfs(int u,int pre)
{
    in[u]=++Time;
    up[u][0]=pre;
    for1(i,1,19)
    {
        up[u][i]=up[up[u][i-1]][i-1];
    }
    if (ok[u]||adj[u].size()==1) ans[u]=val[u];
    else if (u!=pre) ans[u]=ans[pre];
    else ans[u]=-1;
    //cerr <<"acac "<<u<<' '<<ans[u]<<'\n';
    for(int v: adj[u])
    {
        dfs(v,u);
    }
    out[u]=Time;
}
bool is_ancestor(int u,int v)
{
    return (in[u]<=in[v] && out[v] <= out[u]);
}
int lca(int u,int v)
{
    if (is_ancestor(u,v)) return u;
    if (is_ancestor(v,u)) return v;
    for2(i,19,0)
    {
        if (!is_ancestor(up[u][i],v)) u=up[u][i];
    }
    return up[u][0];
}
void init(int N,int M,vector<int> U,vector<int> V, vector<int> W)
{
    n=N;
    m=M;
    for1(i,0,n+m) par[i]=-1;
    for1(i,0,m-1)
    {
        Edge[i+1]={W[i],{U[i],V[i]}};
        //L[{U[i],V[i]}]=W[i];
    }
    sort(Edge+1,Edge+1+m);
    for1(i,1,m)
    {
        AddEdge(Edge[i].se.fi,Edge[i].se.se,Edge[i].fi);
    }
    dfs(n-1,n-1);
}
int getMinimumFuelCapacity(int X, int Y) {
  return ans[lca(X,Y)];
}
 
//
//int main() {
//  int N, M;
//  assert(2 == scanf("%d %d", &N, &M));
//
//  std::vector<int> U(M), V(M), W(M);
//  for (int i = 0; i < M; ++i) {
//    assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
//  }
//
//  int Q;
//  assert(1 == scanf("%d", &Q));
//
//  std::vector<int> X(Q), Y(Q);
//  for (int i = 0; i < Q; ++i) {
//    assert(2 == scanf("%d %d", &X[i], &Y[i]));
//  }
//
//  init(N, M, U, V, W);
//
//  std::vector<int> minimum_fuel_capacities(Q);
//  for (int i = 0; i < Q; ++i) {
//    minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
//  }
//
//  for (int i = 0; i < Q; ++i) {
//    printf("%d\n", minimum_fuel_capacities[i]);
//  }
//
//  return 0;
//}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 5 ms 9876 KB Output is correct
4 Correct 5 ms 9820 KB Output is correct
5 Correct 5 ms 10076 KB Output is correct
6 Correct 7 ms 9980 KB Output is correct
7 Correct 5 ms 10076 KB Output is correct
8 Correct 5 ms 10076 KB Output is correct
9 Correct 77 ms 32084 KB Output is correct
10 Correct 93 ms 37184 KB Output is correct
11 Correct 97 ms 36544 KB Output is correct
12 Correct 103 ms 38088 KB Output is correct
13 Correct 76 ms 40660 KB Output is correct
14 Correct 68 ms 32380 KB Output is correct
15 Correct 137 ms 41364 KB Output is correct
16 Correct 145 ms 40676 KB Output is correct
17 Correct 146 ms 42168 KB Output is correct
18 Correct 167 ms 44552 KB Output is correct
19 Correct 61 ms 19340 KB Output is correct
20 Correct 147 ms 42448 KB Output is correct
21 Correct 142 ms 41868 KB Output is correct
22 Correct 142 ms 43700 KB Output is correct
23 Correct 227 ms 46136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 157 ms 46072 KB Output is correct
4 Correct 189 ms 47540 KB Output is correct
5 Correct 156 ms 46860 KB Output is correct
6 Correct 177 ms 47080 KB Output is correct
7 Correct 155 ms 47048 KB Output is correct
8 Correct 150 ms 45752 KB Output is correct
9 Correct 148 ms 46764 KB Output is correct
10 Correct 159 ms 45584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 5 ms 9876 KB Output is correct
4 Correct 5 ms 9820 KB Output is correct
5 Correct 5 ms 10076 KB Output is correct
6 Correct 7 ms 9980 KB Output is correct
7 Correct 5 ms 10076 KB Output is correct
8 Correct 5 ms 10076 KB Output is correct
9 Correct 8 ms 9816 KB Output is correct
10 Correct 4 ms 10076 KB Output is correct
11 Correct 6 ms 10072 KB Output is correct
12 Correct 5 ms 10076 KB Output is correct
13 Correct 5 ms 10332 KB Output is correct
14 Correct 4 ms 10076 KB Output is correct
15 Correct 5 ms 10076 KB Output is correct
16 Correct 5 ms 10076 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 5 ms 10076 KB Output is correct
19 Correct 7 ms 10076 KB Output is correct
20 Correct 5 ms 10072 KB Output is correct
21 Correct 5 ms 10076 KB Output is correct
22 Correct 6 ms 10076 KB Output is correct
23 Correct 6 ms 10076 KB Output is correct
24 Correct 6 ms 10332 KB Output is correct
25 Correct 5 ms 10332 KB Output is correct
26 Correct 5 ms 10148 KB Output is correct
27 Correct 7 ms 10072 KB Output is correct
28 Correct 5 ms 10332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9816 KB Output is correct
2 Correct 5 ms 9816 KB Output is correct
3 Correct 5 ms 9820 KB Output is correct
4 Correct 5 ms 9876 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 5 ms 10076 KB Output is correct
7 Correct 7 ms 9980 KB Output is correct
8 Correct 5 ms 10076 KB Output is correct
9 Correct 5 ms 10076 KB Output is correct
10 Correct 77 ms 32084 KB Output is correct
11 Correct 93 ms 37184 KB Output is correct
12 Correct 97 ms 36544 KB Output is correct
13 Correct 103 ms 38088 KB Output is correct
14 Correct 76 ms 40660 KB Output is correct
15 Correct 4 ms 10076 KB Output is correct
16 Correct 6 ms 10072 KB Output is correct
17 Correct 5 ms 10076 KB Output is correct
18 Correct 5 ms 10332 KB Output is correct
19 Correct 4 ms 10076 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 5 ms 10076 KB Output is correct
22 Correct 4 ms 10076 KB Output is correct
23 Correct 5 ms 10076 KB Output is correct
24 Correct 7 ms 10076 KB Output is correct
25 Correct 5 ms 10072 KB Output is correct
26 Correct 5 ms 10076 KB Output is correct
27 Correct 6 ms 10076 KB Output is correct
28 Correct 6 ms 10076 KB Output is correct
29 Correct 6 ms 10332 KB Output is correct
30 Correct 5 ms 10332 KB Output is correct
31 Correct 5 ms 10148 KB Output is correct
32 Correct 7 ms 10072 KB Output is correct
33 Correct 5 ms 10332 KB Output is correct
34 Correct 13 ms 13660 KB Output is correct
35 Correct 84 ms 38012 KB Output is correct
36 Correct 102 ms 38004 KB Output is correct
37 Correct 83 ms 37864 KB Output is correct
38 Correct 77 ms 37456 KB Output is correct
39 Correct 78 ms 37456 KB Output is correct
40 Correct 79 ms 35156 KB Output is correct
41 Correct 92 ms 38228 KB Output is correct
42 Correct 84 ms 37968 KB Output is correct
43 Correct 94 ms 41036 KB Output is correct
44 Correct 103 ms 38228 KB Output is correct
45 Correct 109 ms 49232 KB Output is correct
46 Correct 86 ms 38020 KB Output is correct
47 Correct 79 ms 37880 KB Output is correct
48 Correct 95 ms 41552 KB Output is correct
49 Correct 86 ms 46160 KB Output is correct
50 Correct 65 ms 38224 KB Output is correct
51 Correct 93 ms 43856 KB Output is correct
52 Correct 126 ms 52564 KB Output is correct
53 Correct 168 ms 55648 KB Output is correct
54 Correct 136 ms 61264 KB Output is correct
55 Correct 70 ms 40788 KB Output is correct
56 Correct 138 ms 56244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 5 ms 9876 KB Output is correct
4 Correct 5 ms 9820 KB Output is correct
5 Correct 5 ms 10076 KB Output is correct
6 Correct 7 ms 9980 KB Output is correct
7 Correct 5 ms 10076 KB Output is correct
8 Correct 5 ms 10076 KB Output is correct
9 Correct 77 ms 32084 KB Output is correct
10 Correct 93 ms 37184 KB Output is correct
11 Correct 97 ms 36544 KB Output is correct
12 Correct 103 ms 38088 KB Output is correct
13 Correct 76 ms 40660 KB Output is correct
14 Correct 68 ms 32380 KB Output is correct
15 Correct 137 ms 41364 KB Output is correct
16 Correct 145 ms 40676 KB Output is correct
17 Correct 146 ms 42168 KB Output is correct
18 Correct 167 ms 44552 KB Output is correct
19 Correct 157 ms 46072 KB Output is correct
20 Correct 189 ms 47540 KB Output is correct
21 Correct 156 ms 46860 KB Output is correct
22 Correct 177 ms 47080 KB Output is correct
23 Correct 155 ms 47048 KB Output is correct
24 Correct 150 ms 45752 KB Output is correct
25 Correct 148 ms 46764 KB Output is correct
26 Correct 159 ms 45584 KB Output is correct
27 Correct 4 ms 10076 KB Output is correct
28 Correct 6 ms 10072 KB Output is correct
29 Correct 5 ms 10076 KB Output is correct
30 Correct 5 ms 10332 KB Output is correct
31 Correct 4 ms 10076 KB Output is correct
32 Correct 5 ms 10076 KB Output is correct
33 Correct 5 ms 10076 KB Output is correct
34 Correct 4 ms 10076 KB Output is correct
35 Correct 5 ms 10076 KB Output is correct
36 Correct 13 ms 13660 KB Output is correct
37 Correct 84 ms 38012 KB Output is correct
38 Correct 102 ms 38004 KB Output is correct
39 Correct 83 ms 37864 KB Output is correct
40 Correct 77 ms 37456 KB Output is correct
41 Correct 78 ms 37456 KB Output is correct
42 Correct 79 ms 35156 KB Output is correct
43 Correct 92 ms 38228 KB Output is correct
44 Correct 84 ms 37968 KB Output is correct
45 Correct 94 ms 41036 KB Output is correct
46 Correct 103 ms 38228 KB Output is correct
47 Correct 17 ms 13912 KB Output is correct
48 Correct 141 ms 42684 KB Output is correct
49 Correct 163 ms 42752 KB Output is correct
50 Correct 136 ms 42676 KB Output is correct
51 Correct 133 ms 42692 KB Output is correct
52 Correct 139 ms 41044 KB Output is correct
53 Correct 118 ms 34560 KB Output is correct
54 Correct 153 ms 43764 KB Output is correct
55 Correct 146 ms 42684 KB Output is correct
56 Correct 174 ms 45260 KB Output is correct
57 Correct 153 ms 43660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9816 KB Output is correct
2 Correct 5 ms 9816 KB Output is correct
3 Correct 5 ms 9820 KB Output is correct
4 Correct 5 ms 9876 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 5 ms 10076 KB Output is correct
7 Correct 7 ms 9980 KB Output is correct
8 Correct 5 ms 10076 KB Output is correct
9 Correct 5 ms 10076 KB Output is correct
10 Correct 77 ms 32084 KB Output is correct
11 Correct 93 ms 37184 KB Output is correct
12 Correct 97 ms 36544 KB Output is correct
13 Correct 103 ms 38088 KB Output is correct
14 Correct 76 ms 40660 KB Output is correct
15 Correct 68 ms 32380 KB Output is correct
16 Correct 137 ms 41364 KB Output is correct
17 Correct 145 ms 40676 KB Output is correct
18 Correct 146 ms 42168 KB Output is correct
19 Correct 167 ms 44552 KB Output is correct
20 Correct 157 ms 46072 KB Output is correct
21 Correct 189 ms 47540 KB Output is correct
22 Correct 156 ms 46860 KB Output is correct
23 Correct 177 ms 47080 KB Output is correct
24 Correct 155 ms 47048 KB Output is correct
25 Correct 150 ms 45752 KB Output is correct
26 Correct 148 ms 46764 KB Output is correct
27 Correct 159 ms 45584 KB Output is correct
28 Correct 4 ms 10076 KB Output is correct
29 Correct 6 ms 10072 KB Output is correct
30 Correct 5 ms 10076 KB Output is correct
31 Correct 5 ms 10332 KB Output is correct
32 Correct 4 ms 10076 KB Output is correct
33 Correct 5 ms 10076 KB Output is correct
34 Correct 5 ms 10076 KB Output is correct
35 Correct 4 ms 10076 KB Output is correct
36 Correct 5 ms 10076 KB Output is correct
37 Correct 13 ms 13660 KB Output is correct
38 Correct 84 ms 38012 KB Output is correct
39 Correct 102 ms 38004 KB Output is correct
40 Correct 83 ms 37864 KB Output is correct
41 Correct 77 ms 37456 KB Output is correct
42 Correct 78 ms 37456 KB Output is correct
43 Correct 79 ms 35156 KB Output is correct
44 Correct 92 ms 38228 KB Output is correct
45 Correct 84 ms 37968 KB Output is correct
46 Correct 94 ms 41036 KB Output is correct
47 Correct 103 ms 38228 KB Output is correct
48 Correct 17 ms 13912 KB Output is correct
49 Correct 141 ms 42684 KB Output is correct
50 Correct 163 ms 42752 KB Output is correct
51 Correct 136 ms 42676 KB Output is correct
52 Correct 133 ms 42692 KB Output is correct
53 Correct 139 ms 41044 KB Output is correct
54 Correct 118 ms 34560 KB Output is correct
55 Correct 153 ms 43764 KB Output is correct
56 Correct 146 ms 42684 KB Output is correct
57 Correct 174 ms 45260 KB Output is correct
58 Correct 153 ms 43660 KB Output is correct
59 Correct 61 ms 19340 KB Output is correct
60 Correct 147 ms 42448 KB Output is correct
61 Correct 142 ms 41868 KB Output is correct
62 Correct 142 ms 43700 KB Output is correct
63 Correct 227 ms 46136 KB Output is correct
64 Correct 7 ms 10076 KB Output is correct
65 Correct 5 ms 10072 KB Output is correct
66 Correct 5 ms 10076 KB Output is correct
67 Correct 6 ms 10076 KB Output is correct
68 Correct 6 ms 10076 KB Output is correct
69 Correct 6 ms 10332 KB Output is correct
70 Correct 5 ms 10332 KB Output is correct
71 Correct 5 ms 10148 KB Output is correct
72 Correct 7 ms 10072 KB Output is correct
73 Correct 5 ms 10332 KB Output is correct
74 Correct 109 ms 49232 KB Output is correct
75 Correct 86 ms 38020 KB Output is correct
76 Correct 79 ms 37880 KB Output is correct
77 Correct 95 ms 41552 KB Output is correct
78 Correct 86 ms 46160 KB Output is correct
79 Correct 65 ms 38224 KB Output is correct
80 Correct 93 ms 43856 KB Output is correct
81 Correct 126 ms 52564 KB Output is correct
82 Correct 168 ms 55648 KB Output is correct
83 Correct 136 ms 61264 KB Output is correct
84 Correct 70 ms 40788 KB Output is correct
85 Correct 138 ms 56244 KB Output is correct
86 Correct 57 ms 24584 KB Output is correct
87 Correct 159 ms 42680 KB Output is correct
88 Correct 153 ms 42936 KB Output is correct
89 Correct 176 ms 44912 KB Output is correct
90 Correct 159 ms 52136 KB Output is correct
91 Correct 179 ms 49272 KB Output is correct
92 Correct 187 ms 47760 KB Output is correct
93 Correct 190 ms 56584 KB Output is correct
94 Correct 230 ms 60116 KB Output is correct
95 Correct 203 ms 65360 KB Output is correct
96 Correct 171 ms 45396 KB Output is correct
97 Correct 205 ms 52752 KB Output is correct