답안 #725254

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
725254 2023-04-17T05:14:22 Z n0sk1ll 자매 도시 (APIO20_swap) C++14
100 / 100
703 ms 57944 KB
#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;







//Note to self: Check for overflow

#include "swap.h"

namespace dsu
{
    int up[100005];

    void build(int n)
    {
        ff(i,0,n) up[i]=-1;
    }

    int Up(int x)
    {
        if (up[x]<0) return x;
        return up[x]=Up(up[x]);
    }

    void dsu(int a, int b)
    {
        a=Up(a),b=Up(b);
        if (a==b) return;
        if (up[a]<up[b]) swap(a,b);
        up[b]+=up[a],up[a]=b;
    }
}

//kad nadjemo spanning tree:
vector<vector<pii>> g(100005);
int tin[100005],tout[100005];
int up[17][100005],we[17][100005];
int dp[100005]; //min twe u subtree
int VREME=0;

int propagate[17][100005];

vector<vector<pii>> uopste(100005);

void dfs(int p, int q, int qw)
{
    tin[p]=++VREME,up[0][p]=q,we[0][p]=qw;
    for (auto [it,w] : g[p]) if (it!=q) dfs(it,p,w);
    tout[p]=VREME;
}

void build(int n)
{
    ff(i,0,n)
    {
        vector<int> temp;
        for (auto [it,w] : uopste[i]) temp.pb(w);

        if ((int)temp.size()<3) dp[i]=mod;
        else sort(all(temp)),dp[i]=temp[2];
    }

    ff(k,1,17) ff(i,0,n)
    {
        up[k][i]=up[k-1][up[k-1][i]];
        we[k][i]=max(we[k-1][i],we[k-1][up[k-1][i]]);
    }
}

bool anc(int a, int b)
{
    return tin[a]<=tin[b] && tout[a]>=tout[b];
}

int Lca(int a, int b, int &maxput)
{
    bff(k,0,17) if (!anc(up[k][a],b))
    {
        maxput=max(maxput,we[k][a]);
        a=up[k][a];
    }
    bff(k,0,17) if (!anc(up[k][b],a))
    {
        maxput=max(maxput,we[k][b]);
        b=up[k][b];
    }

    if (anc(b,a)) swap(a,b);
    if (anc(a,b))
    {
        maxput=max(maxput,we[0][b]);
        return a;
    }

    maxput=max(maxput,we[0][a]);
    maxput=max(maxput,we[0][b]);

    a=up[0][a];
    return a;
}

void init(int n, int m, vector<int> u, vector<int> v, vector<int> w)
{
    vector<pair<int,pii>> sorta;
    ff(i,0,m) uopste[u[i]].pb({v[i],w[i]}),uopste[v[i]].pb({u[i],w[i]});
    ff(i,0,m) sorta.pb({w[i],{u[i],v[i]}});
    sort(all(sorta));

    vector<pair<int,pii>> izdajnici;

    dsu::build(n);
    for (auto it : sorta)
    {
        int a=it.yy.xx,b=it.yy.yy;
        int pa=dsu::Up(a),pb=dsu::Up(b);
        if (pa!=pb) g[a].pb({b,it.xx}),g[b].pb({a,it.xx}),dsu::dsu(pa,pb);
        else izdajnici.pb(it);
    }

    ff(i,0,n) dp[i]=mod;
    dfs(0,0,0);
    build(n);

    ff(k,0,17) ff(i,0,n) propagate[k][i]=mod;

    for (auto it : izdajnici)
    {
        int a=it.yy.xx,b=it.yy.yy,w=it.xx;
        bff(k,0,17) if (!anc(up[k][a],b))
        {
            propagate[k][a]=min(propagate[k][a],w);
            a=up[k][a];
        }
        bff(k,0,17) if (!anc(up[k][b],a))
        {
            propagate[k][b]=min(propagate[k][b],w);
            b=up[k][b];
        }

        if (anc(b,a)) swap(a,b);
        if (anc(a,b))
        {
            propagate[0][b]=min(propagate[0][b],w);
        }
        else
        {
            propagate[0][b]=min(propagate[0][b],w);
            propagate[0][a]=min(propagate[0][a],w);
        }
    }

    bff(k,0,16) ff(i,0,n)
    {
        propagate[k][i]=min(propagate[k][i],propagate[k+1][i]);
        propagate[k][up[k][i]]=min(propagate[k][up[k][i]],propagate[k+1][i]);
    }

    ff(i,0,n) dp[i]=min(dp[i],propagate[0][i]);

    vector<bool> vis(n,false);
    priority_queue<pii,vector<pii>,greater<pii>> pq;
    ff(i,0,n) pq.push({dp[i],i});

    while (!pq.empty())
    {
        auto [d,p]=pq.top();
        pq.pop();

        if (vis[p]) continue;
        vis[p]=true;

        dp[p]=d;
        for (auto [it,w] : uopste[p]) pq.push({max(d,w),it});
    }
}

int getMinimumFuelCapacity(int a, int b)
{
    int maxput=0;
    int c=Lca(a,b,maxput);
    if (dp[c]==mod) return -1;
    return max(maxput,dp[c]);
}

/*

4 4
0 1 1
1 2 2
2 3 3
3 0 4
4
0 1
1 2
2 3
3 0

5 4
0 1 1
1 2 1
2 3 1
3 4 1
10
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4

5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1

3 2
0 1 5
0 2 5
1
1 2

11 10
0 1 5
1 2 4
2 3 3
2 4 10
1 5 1
5 6 2
5 7 2
0 8 10
8 9 1
8 10 1
10
1 0
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10


*/

Compilation message

swap.cpp: In function 'void dfs(int, int, int)':
swap.cpp:77:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   77 |     for (auto [it,w] : g[p]) if (it!=q) dfs(it,p,w);
      |               ^
swap.cpp: In function 'void build(int)':
swap.cpp:86:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   86 |         for (auto [it,w] : uopste[i]) temp.pb(w);
      |                   ^
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:195:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  195 |         auto [d,p]=pq.top();
      |              ^
swap.cpp:202:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  202 |         for (auto [it,w] : uopste[p]) pq.push({max(d,w),it});
      |                   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 5 ms 5332 KB Output is correct
5 Correct 4 ms 5588 KB Output is correct
6 Correct 5 ms 5552 KB Output is correct
7 Correct 4 ms 5548 KB Output is correct
8 Correct 4 ms 5548 KB Output is correct
9 Correct 140 ms 34196 KB Output is correct
10 Correct 177 ms 41028 KB Output is correct
11 Correct 157 ms 40176 KB Output is correct
12 Correct 163 ms 42100 KB Output is correct
13 Correct 153 ms 43176 KB Output is correct
14 Correct 147 ms 33764 KB Output is correct
15 Correct 375 ms 42840 KB Output is correct
16 Correct 425 ms 40816 KB Output is correct
17 Correct 454 ms 44676 KB Output is correct
18 Correct 434 ms 43960 KB Output is correct
19 Correct 122 ms 14784 KB Output is correct
20 Correct 444 ms 46160 KB Output is correct
21 Correct 408 ms 44436 KB Output is correct
22 Correct 446 ms 47384 KB Output is correct
23 Correct 428 ms 47768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 245 ms 41328 KB Output is correct
4 Correct 245 ms 42568 KB Output is correct
5 Correct 254 ms 41648 KB Output is correct
6 Correct 264 ms 42528 KB Output is correct
7 Correct 255 ms 42244 KB Output is correct
8 Correct 221 ms 40832 KB Output is correct
9 Correct 227 ms 42056 KB Output is correct
10 Correct 263 ms 40508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 5 ms 5332 KB Output is correct
5 Correct 4 ms 5588 KB Output is correct
6 Correct 5 ms 5552 KB Output is correct
7 Correct 4 ms 5548 KB Output is correct
8 Correct 4 ms 5548 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 5 ms 5588 KB Output is correct
11 Correct 4 ms 5544 KB Output is correct
12 Correct 3 ms 5588 KB Output is correct
13 Correct 4 ms 5460 KB Output is correct
14 Correct 4 ms 5552 KB Output is correct
15 Correct 4 ms 5588 KB Output is correct
16 Correct 4 ms 5544 KB Output is correct
17 Correct 4 ms 5588 KB Output is correct
18 Correct 4 ms 5588 KB Output is correct
19 Correct 5 ms 5460 KB Output is correct
20 Correct 4 ms 5544 KB Output is correct
21 Correct 4 ms 5588 KB Output is correct
22 Correct 6 ms 5592 KB Output is correct
23 Correct 5 ms 5560 KB Output is correct
24 Correct 6 ms 5816 KB Output is correct
25 Correct 5 ms 5668 KB Output is correct
26 Correct 7 ms 5716 KB Output is correct
27 Correct 4 ms 5588 KB Output is correct
28 Correct 6 ms 5716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5588 KB Output is correct
7 Correct 5 ms 5552 KB Output is correct
8 Correct 4 ms 5548 KB Output is correct
9 Correct 4 ms 5548 KB Output is correct
10 Correct 140 ms 34196 KB Output is correct
11 Correct 177 ms 41028 KB Output is correct
12 Correct 157 ms 40176 KB Output is correct
13 Correct 163 ms 42100 KB Output is correct
14 Correct 153 ms 43176 KB Output is correct
15 Correct 5 ms 5588 KB Output is correct
16 Correct 4 ms 5544 KB Output is correct
17 Correct 3 ms 5588 KB Output is correct
18 Correct 4 ms 5460 KB Output is correct
19 Correct 4 ms 5552 KB Output is correct
20 Correct 4 ms 5588 KB Output is correct
21 Correct 4 ms 5544 KB Output is correct
22 Correct 4 ms 5588 KB Output is correct
23 Correct 4 ms 5588 KB Output is correct
24 Correct 5 ms 5460 KB Output is correct
25 Correct 4 ms 5544 KB Output is correct
26 Correct 4 ms 5588 KB Output is correct
27 Correct 6 ms 5592 KB Output is correct
28 Correct 5 ms 5560 KB Output is correct
29 Correct 6 ms 5816 KB Output is correct
30 Correct 5 ms 5668 KB Output is correct
31 Correct 7 ms 5716 KB Output is correct
32 Correct 4 ms 5588 KB Output is correct
33 Correct 6 ms 5716 KB Output is correct
34 Correct 23 ms 10168 KB Output is correct
35 Correct 155 ms 42008 KB Output is correct
36 Correct 185 ms 40576 KB Output is correct
37 Correct 156 ms 39232 KB Output is correct
38 Correct 147 ms 38848 KB Output is correct
39 Correct 156 ms 38572 KB Output is correct
40 Correct 146 ms 36208 KB Output is correct
41 Correct 186 ms 40880 KB Output is correct
42 Correct 164 ms 41628 KB Output is correct
43 Correct 144 ms 42272 KB Output is correct
44 Correct 190 ms 39876 KB Output is correct
45 Correct 237 ms 41712 KB Output is correct
46 Correct 151 ms 41032 KB Output is correct
47 Correct 225 ms 40208 KB Output is correct
48 Correct 172 ms 41060 KB Output is correct
49 Correct 145 ms 26724 KB Output is correct
50 Correct 136 ms 21192 KB Output is correct
51 Correct 194 ms 36056 KB Output is correct
52 Correct 338 ms 48972 KB Output is correct
53 Correct 278 ms 50660 KB Output is correct
54 Correct 405 ms 54204 KB Output is correct
55 Correct 134 ms 44308 KB Output is correct
56 Correct 285 ms 49728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 5 ms 5332 KB Output is correct
5 Correct 4 ms 5588 KB Output is correct
6 Correct 5 ms 5552 KB Output is correct
7 Correct 4 ms 5548 KB Output is correct
8 Correct 4 ms 5548 KB Output is correct
9 Correct 140 ms 34196 KB Output is correct
10 Correct 177 ms 41028 KB Output is correct
11 Correct 157 ms 40176 KB Output is correct
12 Correct 163 ms 42100 KB Output is correct
13 Correct 153 ms 43176 KB Output is correct
14 Correct 147 ms 33764 KB Output is correct
15 Correct 375 ms 42840 KB Output is correct
16 Correct 425 ms 40816 KB Output is correct
17 Correct 454 ms 44676 KB Output is correct
18 Correct 434 ms 43960 KB Output is correct
19 Correct 245 ms 41328 KB Output is correct
20 Correct 245 ms 42568 KB Output is correct
21 Correct 254 ms 41648 KB Output is correct
22 Correct 264 ms 42528 KB Output is correct
23 Correct 255 ms 42244 KB Output is correct
24 Correct 221 ms 40832 KB Output is correct
25 Correct 227 ms 42056 KB Output is correct
26 Correct 263 ms 40508 KB Output is correct
27 Correct 5 ms 5588 KB Output is correct
28 Correct 4 ms 5544 KB Output is correct
29 Correct 3 ms 5588 KB Output is correct
30 Correct 4 ms 5460 KB Output is correct
31 Correct 4 ms 5552 KB Output is correct
32 Correct 4 ms 5588 KB Output is correct
33 Correct 4 ms 5544 KB Output is correct
34 Correct 4 ms 5588 KB Output is correct
35 Correct 4 ms 5588 KB Output is correct
36 Correct 23 ms 10168 KB Output is correct
37 Correct 155 ms 42008 KB Output is correct
38 Correct 185 ms 40576 KB Output is correct
39 Correct 156 ms 39232 KB Output is correct
40 Correct 147 ms 38848 KB Output is correct
41 Correct 156 ms 38572 KB Output is correct
42 Correct 146 ms 36208 KB Output is correct
43 Correct 186 ms 40880 KB Output is correct
44 Correct 164 ms 41628 KB Output is correct
45 Correct 144 ms 42272 KB Output is correct
46 Correct 190 ms 39876 KB Output is correct
47 Correct 29 ms 10056 KB Output is correct
48 Correct 412 ms 42484 KB Output is correct
49 Correct 447 ms 41536 KB Output is correct
50 Correct 445 ms 40944 KB Output is correct
51 Correct 461 ms 40608 KB Output is correct
52 Correct 346 ms 38628 KB Output is correct
53 Correct 235 ms 30360 KB Output is correct
54 Correct 463 ms 41984 KB Output is correct
55 Correct 472 ms 42816 KB Output is correct
56 Correct 439 ms 44280 KB Output is correct
57 Correct 350 ms 41508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5588 KB Output is correct
7 Correct 5 ms 5552 KB Output is correct
8 Correct 4 ms 5548 KB Output is correct
9 Correct 4 ms 5548 KB Output is correct
10 Correct 140 ms 34196 KB Output is correct
11 Correct 177 ms 41028 KB Output is correct
12 Correct 157 ms 40176 KB Output is correct
13 Correct 163 ms 42100 KB Output is correct
14 Correct 153 ms 43176 KB Output is correct
15 Correct 147 ms 33764 KB Output is correct
16 Correct 375 ms 42840 KB Output is correct
17 Correct 425 ms 40816 KB Output is correct
18 Correct 454 ms 44676 KB Output is correct
19 Correct 434 ms 43960 KB Output is correct
20 Correct 245 ms 41328 KB Output is correct
21 Correct 245 ms 42568 KB Output is correct
22 Correct 254 ms 41648 KB Output is correct
23 Correct 264 ms 42528 KB Output is correct
24 Correct 255 ms 42244 KB Output is correct
25 Correct 221 ms 40832 KB Output is correct
26 Correct 227 ms 42056 KB Output is correct
27 Correct 263 ms 40508 KB Output is correct
28 Correct 5 ms 5588 KB Output is correct
29 Correct 4 ms 5544 KB Output is correct
30 Correct 3 ms 5588 KB Output is correct
31 Correct 4 ms 5460 KB Output is correct
32 Correct 4 ms 5552 KB Output is correct
33 Correct 4 ms 5588 KB Output is correct
34 Correct 4 ms 5544 KB Output is correct
35 Correct 4 ms 5588 KB Output is correct
36 Correct 4 ms 5588 KB Output is correct
37 Correct 23 ms 10168 KB Output is correct
38 Correct 155 ms 42008 KB Output is correct
39 Correct 185 ms 40576 KB Output is correct
40 Correct 156 ms 39232 KB Output is correct
41 Correct 147 ms 38848 KB Output is correct
42 Correct 156 ms 38572 KB Output is correct
43 Correct 146 ms 36208 KB Output is correct
44 Correct 186 ms 40880 KB Output is correct
45 Correct 164 ms 41628 KB Output is correct
46 Correct 144 ms 42272 KB Output is correct
47 Correct 190 ms 39876 KB Output is correct
48 Correct 29 ms 10056 KB Output is correct
49 Correct 412 ms 42484 KB Output is correct
50 Correct 447 ms 41536 KB Output is correct
51 Correct 445 ms 40944 KB Output is correct
52 Correct 461 ms 40608 KB Output is correct
53 Correct 346 ms 38628 KB Output is correct
54 Correct 235 ms 30360 KB Output is correct
55 Correct 463 ms 41984 KB Output is correct
56 Correct 472 ms 42816 KB Output is correct
57 Correct 439 ms 44280 KB Output is correct
58 Correct 350 ms 41508 KB Output is correct
59 Correct 122 ms 14784 KB Output is correct
60 Correct 444 ms 46160 KB Output is correct
61 Correct 408 ms 44436 KB Output is correct
62 Correct 446 ms 47384 KB Output is correct
63 Correct 428 ms 47768 KB Output is correct
64 Correct 5 ms 5460 KB Output is correct
65 Correct 4 ms 5544 KB Output is correct
66 Correct 4 ms 5588 KB Output is correct
67 Correct 6 ms 5592 KB Output is correct
68 Correct 5 ms 5560 KB Output is correct
69 Correct 6 ms 5816 KB Output is correct
70 Correct 5 ms 5668 KB Output is correct
71 Correct 7 ms 5716 KB Output is correct
72 Correct 4 ms 5588 KB Output is correct
73 Correct 6 ms 5716 KB Output is correct
74 Correct 237 ms 41712 KB Output is correct
75 Correct 151 ms 41032 KB Output is correct
76 Correct 225 ms 40208 KB Output is correct
77 Correct 172 ms 41060 KB Output is correct
78 Correct 145 ms 26724 KB Output is correct
79 Correct 136 ms 21192 KB Output is correct
80 Correct 194 ms 36056 KB Output is correct
81 Correct 338 ms 48972 KB Output is correct
82 Correct 278 ms 50660 KB Output is correct
83 Correct 405 ms 54204 KB Output is correct
84 Correct 134 ms 44308 KB Output is correct
85 Correct 285 ms 49728 KB Output is correct
86 Correct 106 ms 19608 KB Output is correct
87 Correct 414 ms 44684 KB Output is correct
88 Correct 438 ms 44864 KB Output is correct
89 Correct 293 ms 42816 KB Output is correct
90 Correct 248 ms 29916 KB Output is correct
91 Correct 269 ms 28780 KB Output is correct
92 Correct 319 ms 39644 KB Output is correct
93 Correct 590 ms 52664 KB Output is correct
94 Correct 460 ms 54536 KB Output is correct
95 Correct 703 ms 57944 KB Output is correct
96 Correct 504 ms 46504 KB Output is correct
97 Correct 406 ms 49720 KB Output is correct