답안 #657862

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
657862 2022-11-11T15:17:49 Z activedeltorre 자매 도시 (APIO20_swap) C++14
100 / 100
1668 ms 106780 KB
#include "swap.h"
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
//ifstream cin("a.in");
//ofstream cout("a.out");
vector<pair<int,pair<int,int> > >edges;
bool cmp(pair<int,pair<int,int> >a,pair<int,pair<int,int> >b)
{
    return a.first<b.first;
}
vector<pair<long long,long long> >adj[200005];
long long backedge[200005];
long long rasp[200005];
long long jos[200005];
long long sus[200005];
long long lvl[200005];
vector<long long>vec;
long long inf=1e14;
void dfs(int curr,int parent)
{
    int i,k;
    lvl[curr]=lvl[parent]+1;
    vec.clear();
    jos[curr]=inf;
    if(adj[curr].size()>=3)
    {
        for(i=0; i<adj[curr].size(); i++)
        {
            vec.push_back(adj[curr][i].second);
        }
        sort(vec.begin(),vec.end());
        jos[curr]=vec[2];
    }
    if(backedge[curr]!=0)
    {
        jos[curr]=min(jos[curr],backedge[curr]);
    }
    for(i=0; i<adj[curr].size(); i++)
    {
        k=adj[curr][i].first;
        if(k!=parent)
        {
            dfs(k,curr);
        }
        jos[curr]=min(jos[curr],max(jos[k],adj[curr][i].second));
    }
}
void dfs2(int curr,int par,long long val)
{
    long long i,k;
    sus[curr]=max(min(jos[par],sus[par]),val);
    rasp[curr]=min(sus[curr],jos[curr]);
    for(i=0; i<adj[curr].size(); i++)
    {
        k=adj[curr][i].first;
        if(k!=par)
        {
            dfs2(k,curr,adj[curr][i].second);
        }
    }
}
///bl e parinte
/// drum e cost de a merge la parinte
/// ext e cost minim degreee 3
long long bl[33][200005];
long long ext[33][200005];
long long drum[33][200005];
int sef[200005];
void dfs3(int curr,int par,long long val)
{
    long long i,k;
    bl[0][curr]=par;
    ext[0][curr]=min(rasp[par],rasp[curr]);
    drum[0][curr]=val;
    for(i=1; i<=30; i++)
    {
        bl[i][curr]=bl[i-1][bl[i-1][curr]];
        ext[i][curr]=min(ext[i-1][curr],ext[i-1][bl[i-1][curr]]);
        drum[i][curr]=max(drum[i-1][curr],drum[i-1][bl[i-1][curr]]);
    }
    for(i=0; i<adj[curr].size(); i++)
    {
        k=adj[curr][i].first;
        if(k!=par)
        {
            dfs3(k,curr,adj[curr][i].second);
        }
    }
}
int lca (long long a,long long b)
{
    long long i,curr;
    if(lvl[a]<lvl[b])
    {
        swap(a,b);
    }
    for(i=30; i>=0; i--)
    {
        curr=bl[i][a];
        if(lvl[curr]>=lvl[b])
        {
            a=curr;
        }
    }
    if(a==b)
    {
        return a;
    }
    else
    {
        for(i=30; i>=0; i--)
        {
            if(bl[i][a]!=bl[i][b])
            {
                a=bl[i][a];
                b=bl[i][b];
            }
        }
        return bl[0][a];
    }
}
pair<long long,long long> calc (int a,int b)
{
    long long curr,maxdrum=0,minrel=inf,i;
    if(a==b)
    {
        return make_pair(0,rasp[a]);
    }
    minrel=min(rasp[a],rasp[b]);
    maxdrum=drum[0][a];
    for(i=30; i>=0; i--)
    {
        curr=bl[i][a];
        if(lvl[curr]>=lvl[b])
        {
            maxdrum=max(maxdrum,drum[i][a]);
            minrel=min(minrel,ext[i][a]);
            a=curr;
        }
    }
    return make_pair(maxdrum,minrel);
}
int find (int u)
{
    if(sef[u]==u)
    {
        return u;
    }
    return sef[u]=find(sef[u]);
}
int merge (int a,int b)
{
    a=find(a);
    b=find(b);
    if(a!=b)
    {
        sef[a]=b;
        return 1;
    }
    return 0;
}
void init(int n, int m,vector<int> a, vector<int> b, vector<int> w)
{
    int i,x,y;
    long long val;
    for(i=1;i<=n;i++)
    {
        sef[i]=i;
    }
    for(i=1; i<=m; i++)
    {
        a[i-1]++;
        b[i-1]++;
        edges.push_back({w[i-1],{a[i-1],b[i-1]}});
    }
    sort(edges.begin(),edges.end(),cmp);
    for(i=0; i<m; i++)
    {
        val=edges[i].first;
        x=edges[i].second.first;
        y=edges[i].second.second;
        if(merge(x,y)==1)
        {
            adj[x].push_back({y,val});
            adj[y].push_back({x,val});
        }
        else
        {
            if(backedge[x]==0)
            {
                backedge[x]=val;
            }
            else
            {
                backedge[x]=min(backedge[x],val);
            }
            if(backedge[y]==0)
            {
                backedge[y]=val;
            }
            else
            {
                backedge[y]=min(backedge[y],val);
            }
        }
    }
    sus[0]=inf;
    jos[0]=inf;
    dfs(1,0);
    dfs2(1,0,0);
    dfs3(1,0,0);
}

int getMinimumFuelCapacity(int x, int y)
{
    long long lc;
    x++;
    y++;
    pair<long long,long long>a;
    pair<long long,long long>b;
    lc=lca(x,y);
    a=calc(x,lc);
    b=calc(y,lc);
    a.first=max(a.first,b.first);
    a.second=min(a.second,b.second);
    a.first=max(a.first,a.second);
    if(a.first==inf)
    {
        return -1;
    }
    else
    {
        return a.first;
    }
}
/*int main()
{
    int N, M;
    cin>>N>>M;
    vector<int> U(M), V(M), W(M);
    for (int i = 0; i < M; ++i)
    {
        cin>>U[i]>>V[i]>>W[i];
    }
    int Q;
    cin>>Q;
    vector<int> X(Q), Y(Q);
    for (int i = 0; i < Q; ++i)
    {
        cin>>X[i]>>Y[i];
    }
    init(N, M, U, V, W);
    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)
    {
        cout<<minimum_fuel_capacities[i]<<'\n';
    }
    return 0;
}*/

Compilation message

swap.cpp: In function 'void dfs(int, int)':
swap.cpp:29:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |         for(i=0; i<adj[curr].size(); i++)
      |                  ~^~~~~~~~~~~~~~~~~
swap.cpp:40:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for(i=0; i<adj[curr].size(); i++)
      |              ~^~~~~~~~~~~~~~~~~
swap.cpp: In function 'void dfs2(int, int, long long int)':
swap.cpp:55:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for(i=0; i<adj[curr].size(); i++)
      |              ~^~~~~~~~~~~~~~~~~
swap.cpp: In function 'void dfs3(int, int, long long int)':
swap.cpp:83:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for(i=0; i<adj[curr].size(); i++)
      |              ~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 3 ms 5588 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 6484 KB Output is correct
6 Correct 5 ms 6484 KB Output is correct
7 Correct 5 ms 6612 KB Output is correct
8 Correct 4 ms 6612 KB Output is correct
9 Correct 226 ms 77088 KB Output is correct
10 Correct 278 ms 94480 KB Output is correct
11 Correct 284 ms 92496 KB Output is correct
12 Correct 301 ms 98008 KB Output is correct
13 Correct 266 ms 99528 KB Output is correct
14 Correct 320 ms 76320 KB Output is correct
15 Correct 1385 ms 96800 KB Output is correct
16 Correct 1405 ms 92588 KB Output is correct
17 Correct 1104 ms 102164 KB Output is correct
18 Correct 1269 ms 99872 KB Output is correct
19 Correct 307 ms 25936 KB Output is correct
20 Correct 1385 ms 101352 KB Output is correct
21 Correct 1428 ms 96972 KB Output is correct
22 Correct 1368 ms 104864 KB Output is correct
23 Correct 1348 ms 104616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 1314 ms 89872 KB Output is correct
4 Correct 1323 ms 94248 KB Output is correct
5 Correct 1347 ms 91420 KB Output is correct
6 Correct 1275 ms 93860 KB Output is correct
7 Correct 1387 ms 92680 KB Output is correct
8 Correct 1307 ms 89216 KB Output is correct
9 Correct 1294 ms 92296 KB Output is correct
10 Correct 1316 ms 88380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 3 ms 5588 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 6484 KB Output is correct
6 Correct 5 ms 6484 KB Output is correct
7 Correct 5 ms 6612 KB Output is correct
8 Correct 4 ms 6612 KB Output is correct
9 Correct 3 ms 5588 KB Output is correct
10 Correct 4 ms 6484 KB Output is correct
11 Correct 4 ms 6484 KB Output is correct
12 Correct 4 ms 6484 KB Output is correct
13 Correct 4 ms 6356 KB Output is correct
14 Correct 4 ms 6356 KB Output is correct
15 Correct 4 ms 6484 KB Output is correct
16 Correct 4 ms 6484 KB Output is correct
17 Correct 5 ms 6484 KB Output is correct
18 Correct 4 ms 6484 KB Output is correct
19 Correct 4 ms 6228 KB Output is correct
20 Correct 4 ms 6484 KB Output is correct
21 Correct 4 ms 6420 KB Output is correct
22 Correct 4 ms 5972 KB Output is correct
23 Correct 4 ms 6356 KB Output is correct
24 Correct 5 ms 6612 KB Output is correct
25 Correct 5 ms 6612 KB Output is correct
26 Correct 5 ms 6612 KB Output is correct
27 Correct 4 ms 6616 KB Output is correct
28 Correct 4 ms 6484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 3 ms 5588 KB Output is correct
4 Correct 3 ms 5588 KB Output is correct
5 Correct 4 ms 5972 KB Output is correct
6 Correct 4 ms 6484 KB Output is correct
7 Correct 5 ms 6484 KB Output is correct
8 Correct 5 ms 6612 KB Output is correct
9 Correct 4 ms 6612 KB Output is correct
10 Correct 226 ms 77088 KB Output is correct
11 Correct 278 ms 94480 KB Output is correct
12 Correct 284 ms 92496 KB Output is correct
13 Correct 301 ms 98008 KB Output is correct
14 Correct 266 ms 99528 KB Output is correct
15 Correct 4 ms 6484 KB Output is correct
16 Correct 4 ms 6484 KB Output is correct
17 Correct 4 ms 6484 KB Output is correct
18 Correct 4 ms 6356 KB Output is correct
19 Correct 4 ms 6356 KB Output is correct
20 Correct 4 ms 6484 KB Output is correct
21 Correct 4 ms 6484 KB Output is correct
22 Correct 5 ms 6484 KB Output is correct
23 Correct 4 ms 6484 KB Output is correct
24 Correct 4 ms 6228 KB Output is correct
25 Correct 4 ms 6484 KB Output is correct
26 Correct 4 ms 6420 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 4 ms 6356 KB Output is correct
29 Correct 5 ms 6612 KB Output is correct
30 Correct 5 ms 6612 KB Output is correct
31 Correct 5 ms 6612 KB Output is correct
32 Correct 4 ms 6616 KB Output is correct
33 Correct 4 ms 6484 KB Output is correct
34 Correct 26 ms 17172 KB Output is correct
35 Correct 293 ms 97124 KB Output is correct
36 Correct 291 ms 94000 KB Output is correct
37 Correct 277 ms 91624 KB Output is correct
38 Correct 260 ms 89924 KB Output is correct
39 Correct 262 ms 89020 KB Output is correct
40 Correct 219 ms 82124 KB Output is correct
41 Correct 291 ms 94900 KB Output is correct
42 Correct 299 ms 96320 KB Output is correct
43 Correct 272 ms 97284 KB Output is correct
44 Correct 253 ms 90764 KB Output is correct
45 Correct 219 ms 78396 KB Output is correct
46 Correct 284 ms 94640 KB Output is correct
47 Correct 251 ms 92864 KB Output is correct
48 Correct 244 ms 91628 KB Output is correct
49 Correct 67 ms 14968 KB Output is correct
50 Correct 57 ms 18280 KB Output is correct
51 Correct 183 ms 65232 KB Output is correct
52 Correct 311 ms 99112 KB Output is correct
53 Correct 285 ms 98460 KB Output is correct
54 Correct 340 ms 103096 KB Output is correct
55 Correct 266 ms 100952 KB Output is correct
56 Correct 287 ms 97596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 3 ms 5588 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 4 ms 6484 KB Output is correct
6 Correct 5 ms 6484 KB Output is correct
7 Correct 5 ms 6612 KB Output is correct
8 Correct 4 ms 6612 KB Output is correct
9 Correct 226 ms 77088 KB Output is correct
10 Correct 278 ms 94480 KB Output is correct
11 Correct 284 ms 92496 KB Output is correct
12 Correct 301 ms 98008 KB Output is correct
13 Correct 266 ms 99528 KB Output is correct
14 Correct 320 ms 76320 KB Output is correct
15 Correct 1385 ms 96800 KB Output is correct
16 Correct 1405 ms 92588 KB Output is correct
17 Correct 1104 ms 102164 KB Output is correct
18 Correct 1269 ms 99872 KB Output is correct
19 Correct 1314 ms 89872 KB Output is correct
20 Correct 1323 ms 94248 KB Output is correct
21 Correct 1347 ms 91420 KB Output is correct
22 Correct 1275 ms 93860 KB Output is correct
23 Correct 1387 ms 92680 KB Output is correct
24 Correct 1307 ms 89216 KB Output is correct
25 Correct 1294 ms 92296 KB Output is correct
26 Correct 1316 ms 88380 KB Output is correct
27 Correct 4 ms 6484 KB Output is correct
28 Correct 4 ms 6484 KB Output is correct
29 Correct 4 ms 6484 KB Output is correct
30 Correct 4 ms 6356 KB Output is correct
31 Correct 4 ms 6356 KB Output is correct
32 Correct 4 ms 6484 KB Output is correct
33 Correct 4 ms 6484 KB Output is correct
34 Correct 5 ms 6484 KB Output is correct
35 Correct 4 ms 6484 KB Output is correct
36 Correct 26 ms 17172 KB Output is correct
37 Correct 293 ms 97124 KB Output is correct
38 Correct 291 ms 94000 KB Output is correct
39 Correct 277 ms 91624 KB Output is correct
40 Correct 260 ms 89924 KB Output is correct
41 Correct 262 ms 89020 KB Output is correct
42 Correct 219 ms 82124 KB Output is correct
43 Correct 291 ms 94900 KB Output is correct
44 Correct 299 ms 96320 KB Output is correct
45 Correct 272 ms 97284 KB Output is correct
46 Correct 253 ms 90764 KB Output is correct
47 Correct 59 ms 16524 KB Output is correct
48 Correct 1482 ms 97376 KB Output is correct
49 Correct 1613 ms 95572 KB Output is correct
50 Correct 1668 ms 94080 KB Output is correct
51 Correct 1639 ms 93304 KB Output is correct
52 Correct 1571 ms 87620 KB Output is correct
53 Correct 1265 ms 65404 KB Output is correct
54 Correct 1587 ms 97084 KB Output is correct
55 Correct 1408 ms 98080 KB Output is correct
56 Correct 1228 ms 100516 KB Output is correct
57 Correct 1522 ms 93968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 3 ms 5588 KB Output is correct
4 Correct 3 ms 5588 KB Output is correct
5 Correct 4 ms 5972 KB Output is correct
6 Correct 4 ms 6484 KB Output is correct
7 Correct 5 ms 6484 KB Output is correct
8 Correct 5 ms 6612 KB Output is correct
9 Correct 4 ms 6612 KB Output is correct
10 Correct 226 ms 77088 KB Output is correct
11 Correct 278 ms 94480 KB Output is correct
12 Correct 284 ms 92496 KB Output is correct
13 Correct 301 ms 98008 KB Output is correct
14 Correct 266 ms 99528 KB Output is correct
15 Correct 320 ms 76320 KB Output is correct
16 Correct 1385 ms 96800 KB Output is correct
17 Correct 1405 ms 92588 KB Output is correct
18 Correct 1104 ms 102164 KB Output is correct
19 Correct 1269 ms 99872 KB Output is correct
20 Correct 1314 ms 89872 KB Output is correct
21 Correct 1323 ms 94248 KB Output is correct
22 Correct 1347 ms 91420 KB Output is correct
23 Correct 1275 ms 93860 KB Output is correct
24 Correct 1387 ms 92680 KB Output is correct
25 Correct 1307 ms 89216 KB Output is correct
26 Correct 1294 ms 92296 KB Output is correct
27 Correct 1316 ms 88380 KB Output is correct
28 Correct 4 ms 6484 KB Output is correct
29 Correct 4 ms 6484 KB Output is correct
30 Correct 4 ms 6484 KB Output is correct
31 Correct 4 ms 6356 KB Output is correct
32 Correct 4 ms 6356 KB Output is correct
33 Correct 4 ms 6484 KB Output is correct
34 Correct 4 ms 6484 KB Output is correct
35 Correct 5 ms 6484 KB Output is correct
36 Correct 4 ms 6484 KB Output is correct
37 Correct 26 ms 17172 KB Output is correct
38 Correct 293 ms 97124 KB Output is correct
39 Correct 291 ms 94000 KB Output is correct
40 Correct 277 ms 91624 KB Output is correct
41 Correct 260 ms 89924 KB Output is correct
42 Correct 262 ms 89020 KB Output is correct
43 Correct 219 ms 82124 KB Output is correct
44 Correct 291 ms 94900 KB Output is correct
45 Correct 299 ms 96320 KB Output is correct
46 Correct 272 ms 97284 KB Output is correct
47 Correct 253 ms 90764 KB Output is correct
48 Correct 59 ms 16524 KB Output is correct
49 Correct 1482 ms 97376 KB Output is correct
50 Correct 1613 ms 95572 KB Output is correct
51 Correct 1668 ms 94080 KB Output is correct
52 Correct 1639 ms 93304 KB Output is correct
53 Correct 1571 ms 87620 KB Output is correct
54 Correct 1265 ms 65404 KB Output is correct
55 Correct 1587 ms 97084 KB Output is correct
56 Correct 1408 ms 98080 KB Output is correct
57 Correct 1228 ms 100516 KB Output is correct
58 Correct 1522 ms 93968 KB Output is correct
59 Correct 307 ms 25936 KB Output is correct
60 Correct 1385 ms 101352 KB Output is correct
61 Correct 1428 ms 96972 KB Output is correct
62 Correct 1368 ms 104864 KB Output is correct
63 Correct 1348 ms 104616 KB Output is correct
64 Correct 4 ms 6228 KB Output is correct
65 Correct 4 ms 6484 KB Output is correct
66 Correct 4 ms 6420 KB Output is correct
67 Correct 4 ms 5972 KB Output is correct
68 Correct 4 ms 6356 KB Output is correct
69 Correct 5 ms 6612 KB Output is correct
70 Correct 5 ms 6612 KB Output is correct
71 Correct 5 ms 6612 KB Output is correct
72 Correct 4 ms 6616 KB Output is correct
73 Correct 4 ms 6484 KB Output is correct
74 Correct 219 ms 78396 KB Output is correct
75 Correct 284 ms 94640 KB Output is correct
76 Correct 251 ms 92864 KB Output is correct
77 Correct 244 ms 91628 KB Output is correct
78 Correct 67 ms 14968 KB Output is correct
79 Correct 57 ms 18280 KB Output is correct
80 Correct 183 ms 65232 KB Output is correct
81 Correct 311 ms 99112 KB Output is correct
82 Correct 285 ms 98460 KB Output is correct
83 Correct 340 ms 103096 KB Output is correct
84 Correct 266 ms 100952 KB Output is correct
85 Correct 287 ms 97596 KB Output is correct
86 Correct 311 ms 35040 KB Output is correct
87 Correct 1668 ms 99332 KB Output is correct
88 Correct 1618 ms 99564 KB Output is correct
89 Correct 1395 ms 91232 KB Output is correct
90 Correct 283 ms 19852 KB Output is correct
91 Correct 381 ms 27444 KB Output is correct
92 Correct 1285 ms 70372 KB Output is correct
93 Correct 1645 ms 104276 KB Output is correct
94 Correct 1535 ms 102924 KB Output is correct
95 Correct 1625 ms 106780 KB Output is correct
96 Correct 1466 ms 102752 KB Output is correct
97 Correct 1518 ms 100456 KB Output is correct