답안 #751872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
751872 2023-06-01T17:14:01 Z snpmrnhlol 자매 도시 (APIO20_swap) C++17
100 / 100
405 ms 41500 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
const int M = 2e5;
const int inf = 2e9;
int id[M],deg[N];
struct nodedata
{
    int parent,active;
};
vector <nodedata> nodes;///parent,active
vector <int> e[2*N - 1];
int p2[2*N - 1][20];
int dpth[2*N - 1];
struct dsu
{
    int id,parent,active,sz;
};
dsu v[N];
int leader(int node)
{
    if(node == v[node].parent)return node;
    int l = leader(v[node].parent);
    v[node].parent = l;
    return v[node].parent;
}
void connect(int a,int b,int w)
{
    deg[a]++;
    deg[b]++;
    int active = 0;
    if(deg[a] >= 3 || deg[b] >= 3)active = 1;
    a = leader(a);
    b = leader(b);
    if(a == b)
    {
        if(nodes[v[a].id].active == inf)nodes[v[a].id].active = w;
        v[a].active = min(v[a].active,w);
    }
    else
    {
        nodes[v[a].id].parent = nodes.size();
        nodes[v[b].id].parent = nodes.size();
        if(v[a].active != inf || v[b].active != inf)active = 1;
        if(v[a].sz < v[b].sz)swap(a,b);
        v[b].parent = a;
        v[a].sz+=v[b].sz;
        v[a].id = nodes.size();
        nodes.push_back({-1,active?w:inf});
        v[a].active = active?w:inf;
    }
}
void dfs(int node,int p = -1,int d = 0)
{
    dpth[node] = d;
    p2[node][0] = p;
    for(auto i:e[node])
    {
        if(i == p)continue;
        dfs(i,node,d + 1);
    }
}
void init(int n, int m,vector<int> u,vector<int> u2,vector<int> w)
{
    int i,j;
    for(i = 0; i < m; i++)id[i] = i;
    for(i = 0; i < n; i++)
    {
        nodes.push_back({-1,inf});
        v[i] = {i,i,inf,1};
    }
    sort(id,id + m,[&](int a,int b)
    {
        return w[a] < w[b];
    });
    for(i = 0; i < m; i++)
    {
        ///connect u[id[i]] with v[id[i]]
        connect(u[id[i]],u2[id[i]],w[id[i]]);
        //cout<<u[id[i]]<<' '<<u2[id[i]]<<'\n';
    }
    int cnt = 0;
    for(auto i:nodes)
    {
        if(i.parent != -1)
        {
            e[i.parent].push_back(cnt);
        }
        cnt++;
    }
    dfs(nodes.size() - 1,nodes.size() - 1);
    for(j = 1; j < 20; j++)
    {
        for(i = 0; i < 2*n - 1; i++)
        {
            p2[i][j] = p2[p2[i][j - 1]][j - 1];
        }
    }
    /*for(i = 0;i < 2*n - 1;i++){
        for(j = 0;j < 20;j++)cout<<p2[i][j]<<' ';
        cout<<'\n';
    }
    for(i = 0;i < 2*n - 1;i++){
        cout<<p2[i][0]<<' '<<dpth[i]<<' '<<nodes[i].active<<' '<<i<<'\n';
    }*/
}

int getMinimumFuelCapacity(int x, int y)
{
    if(nodes[nodes.size() - 1].active == inf)return -1;
    if(dpth[x] < dpth[y])swap(x,y);
    for(int i = 19; i >= 0; i--)
    {
        if(dpth[x] - (1<<i) >= dpth[y])
        {
            x = p2[x][i];
        }
    }
    for(int i = 19; i >= 0; i--)
    {
        if(p2[x][i] != p2[y][i])
        {
            x = p2[x][i];
            y = p2[y][i];
        }
    }
    if(x != y)x = p2[x][0];
    for(int i = 19; i >= 0; i--)
    {
        if(nodes[p2[x][i]].active == inf)
        {
            x = p2[x][i];
        }
    }
    if(nodes[x].active == inf)
    {
        x = p2[x][0];
    }
    /*while(dpth[x] > dpth[y])x = p2[x][0];
    while(x != y)x = p2[x][0],y = p2[y][0];
    while(nodes[x].active == inf)x = p2[x][0];*/
    return nodes[x].active;
}
/**
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
**/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5276 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 87 ms 25240 KB Output is correct
10 Correct 112 ms 29740 KB Output is correct
11 Correct 105 ms 29396 KB Output is correct
12 Correct 116 ms 30856 KB Output is correct
13 Correct 119 ms 32224 KB Output is correct
14 Correct 102 ms 25276 KB Output is correct
15 Correct 154 ms 31772 KB Output is correct
16 Correct 166 ms 31108 KB Output is correct
17 Correct 164 ms 32504 KB Output is correct
18 Correct 178 ms 33988 KB Output is correct
19 Correct 101 ms 12468 KB Output is correct
20 Correct 234 ms 32952 KB Output is correct
21 Correct 235 ms 32256 KB Output is correct
22 Correct 288 ms 33936 KB Output is correct
23 Correct 335 ms 35316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 375 ms 35072 KB Output is correct
4 Correct 341 ms 36360 KB Output is correct
5 Correct 364 ms 35712 KB Output is correct
6 Correct 345 ms 36256 KB Output is correct
7 Correct 397 ms 35976 KB Output is correct
8 Correct 405 ms 34904 KB Output is correct
9 Correct 344 ms 35752 KB Output is correct
10 Correct 376 ms 34636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5276 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 4 ms 5280 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 5 ms 5204 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5244 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 3 ms 5204 KB Output is correct
19 Correct 4 ms 5204 KB Output is correct
20 Correct 3 ms 5204 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 4 ms 5032 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 5 ms 5312 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
27 Correct 3 ms 5204 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5276 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 87 ms 25240 KB Output is correct
11 Correct 112 ms 29740 KB Output is correct
12 Correct 105 ms 29396 KB Output is correct
13 Correct 116 ms 30856 KB Output is correct
14 Correct 119 ms 32224 KB Output is correct
15 Correct 4 ms 5280 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 5 ms 5204 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5244 KB Output is correct
20 Correct 4 ms 5204 KB Output is correct
21 Correct 4 ms 5204 KB Output is correct
22 Correct 4 ms 5204 KB Output is correct
23 Correct 3 ms 5204 KB Output is correct
24 Correct 4 ms 5204 KB Output is correct
25 Correct 3 ms 5204 KB Output is correct
26 Correct 3 ms 5204 KB Output is correct
27 Correct 4 ms 5032 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 5 ms 5312 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 3 ms 5204 KB Output is correct
33 Correct 4 ms 5204 KB Output is correct
34 Correct 13 ms 8532 KB Output is correct
35 Correct 115 ms 30844 KB Output is correct
36 Correct 116 ms 30808 KB Output is correct
37 Correct 117 ms 30748 KB Output is correct
38 Correct 131 ms 30452 KB Output is correct
39 Correct 109 ms 30328 KB Output is correct
40 Correct 105 ms 28252 KB Output is correct
41 Correct 113 ms 30756 KB Output is correct
42 Correct 109 ms 30828 KB Output is correct
43 Correct 105 ms 32668 KB Output is correct
44 Correct 120 ms 30748 KB Output is correct
45 Correct 119 ms 27800 KB Output is correct
46 Correct 122 ms 30780 KB Output is correct
47 Correct 125 ms 30652 KB Output is correct
48 Correct 141 ms 31124 KB Output is correct
49 Correct 80 ms 9548 KB Output is correct
50 Correct 53 ms 10016 KB Output is correct
51 Correct 101 ms 23800 KB Output is correct
52 Correct 157 ms 32528 KB Output is correct
53 Correct 159 ms 33320 KB Output is correct
54 Correct 191 ms 33468 KB Output is correct
55 Correct 116 ms 32448 KB Output is correct
56 Correct 160 ms 33644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5276 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 87 ms 25240 KB Output is correct
10 Correct 112 ms 29740 KB Output is correct
11 Correct 105 ms 29396 KB Output is correct
12 Correct 116 ms 30856 KB Output is correct
13 Correct 119 ms 32224 KB Output is correct
14 Correct 102 ms 25276 KB Output is correct
15 Correct 154 ms 31772 KB Output is correct
16 Correct 166 ms 31108 KB Output is correct
17 Correct 164 ms 32504 KB Output is correct
18 Correct 178 ms 33988 KB Output is correct
19 Correct 375 ms 35072 KB Output is correct
20 Correct 341 ms 36360 KB Output is correct
21 Correct 364 ms 35712 KB Output is correct
22 Correct 345 ms 36256 KB Output is correct
23 Correct 397 ms 35976 KB Output is correct
24 Correct 405 ms 34904 KB Output is correct
25 Correct 344 ms 35752 KB Output is correct
26 Correct 376 ms 34636 KB Output is correct
27 Correct 4 ms 5280 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 5 ms 5204 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
31 Correct 4 ms 5244 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 4 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 3 ms 5204 KB Output is correct
36 Correct 13 ms 8532 KB Output is correct
37 Correct 115 ms 30844 KB Output is correct
38 Correct 116 ms 30808 KB Output is correct
39 Correct 117 ms 30748 KB Output is correct
40 Correct 131 ms 30452 KB Output is correct
41 Correct 109 ms 30328 KB Output is correct
42 Correct 105 ms 28252 KB Output is correct
43 Correct 113 ms 30756 KB Output is correct
44 Correct 109 ms 30828 KB Output is correct
45 Correct 105 ms 32668 KB Output is correct
46 Correct 120 ms 30748 KB Output is correct
47 Correct 23 ms 8948 KB Output is correct
48 Correct 273 ms 37500 KB Output is correct
49 Correct 290 ms 37432 KB Output is correct
50 Correct 291 ms 37400 KB Output is correct
51 Correct 238 ms 37300 KB Output is correct
52 Correct 255 ms 35880 KB Output is correct
53 Correct 227 ms 29296 KB Output is correct
54 Correct 285 ms 38360 KB Output is correct
55 Correct 279 ms 37496 KB Output is correct
56 Correct 387 ms 38872 KB Output is correct
57 Correct 288 ms 38420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5276 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 87 ms 25240 KB Output is correct
11 Correct 112 ms 29740 KB Output is correct
12 Correct 105 ms 29396 KB Output is correct
13 Correct 116 ms 30856 KB Output is correct
14 Correct 119 ms 32224 KB Output is correct
15 Correct 102 ms 25276 KB Output is correct
16 Correct 154 ms 31772 KB Output is correct
17 Correct 166 ms 31108 KB Output is correct
18 Correct 164 ms 32504 KB Output is correct
19 Correct 178 ms 33988 KB Output is correct
20 Correct 375 ms 35072 KB Output is correct
21 Correct 341 ms 36360 KB Output is correct
22 Correct 364 ms 35712 KB Output is correct
23 Correct 345 ms 36256 KB Output is correct
24 Correct 397 ms 35976 KB Output is correct
25 Correct 405 ms 34904 KB Output is correct
26 Correct 344 ms 35752 KB Output is correct
27 Correct 376 ms 34636 KB Output is correct
28 Correct 4 ms 5280 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 5 ms 5204 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 4 ms 5244 KB Output is correct
33 Correct 4 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 3 ms 5204 KB Output is correct
37 Correct 13 ms 8532 KB Output is correct
38 Correct 115 ms 30844 KB Output is correct
39 Correct 116 ms 30808 KB Output is correct
40 Correct 117 ms 30748 KB Output is correct
41 Correct 131 ms 30452 KB Output is correct
42 Correct 109 ms 30328 KB Output is correct
43 Correct 105 ms 28252 KB Output is correct
44 Correct 113 ms 30756 KB Output is correct
45 Correct 109 ms 30828 KB Output is correct
46 Correct 105 ms 32668 KB Output is correct
47 Correct 120 ms 30748 KB Output is correct
48 Correct 23 ms 8948 KB Output is correct
49 Correct 273 ms 37500 KB Output is correct
50 Correct 290 ms 37432 KB Output is correct
51 Correct 291 ms 37400 KB Output is correct
52 Correct 238 ms 37300 KB Output is correct
53 Correct 255 ms 35880 KB Output is correct
54 Correct 227 ms 29296 KB Output is correct
55 Correct 285 ms 38360 KB Output is correct
56 Correct 279 ms 37496 KB Output is correct
57 Correct 387 ms 38872 KB Output is correct
58 Correct 288 ms 38420 KB Output is correct
59 Correct 101 ms 12468 KB Output is correct
60 Correct 234 ms 32952 KB Output is correct
61 Correct 235 ms 32256 KB Output is correct
62 Correct 288 ms 33936 KB Output is correct
63 Correct 335 ms 35316 KB Output is correct
64 Correct 4 ms 5204 KB Output is correct
65 Correct 3 ms 5204 KB Output is correct
66 Correct 3 ms 5204 KB Output is correct
67 Correct 4 ms 5032 KB Output is correct
68 Correct 4 ms 5204 KB Output is correct
69 Correct 3 ms 5204 KB Output is correct
70 Correct 5 ms 5312 KB Output is correct
71 Correct 4 ms 5204 KB Output is correct
72 Correct 3 ms 5204 KB Output is correct
73 Correct 4 ms 5204 KB Output is correct
74 Correct 119 ms 27800 KB Output is correct
75 Correct 122 ms 30780 KB Output is correct
76 Correct 125 ms 30652 KB Output is correct
77 Correct 141 ms 31124 KB Output is correct
78 Correct 80 ms 9548 KB Output is correct
79 Correct 53 ms 10016 KB Output is correct
80 Correct 101 ms 23800 KB Output is correct
81 Correct 157 ms 32528 KB Output is correct
82 Correct 159 ms 33320 KB Output is correct
83 Correct 191 ms 33468 KB Output is correct
84 Correct 116 ms 32448 KB Output is correct
85 Correct 160 ms 33644 KB Output is correct
86 Correct 76 ms 15872 KB Output is correct
87 Correct 268 ms 37428 KB Output is correct
88 Correct 283 ms 37544 KB Output is correct
89 Correct 328 ms 36308 KB Output is correct
90 Correct 178 ms 16892 KB Output is correct
91 Correct 187 ms 19064 KB Output is correct
92 Correct 286 ms 31524 KB Output is correct
93 Correct 296 ms 40552 KB Output is correct
94 Correct 309 ms 41500 KB Output is correct
95 Correct 302 ms 41420 KB Output is correct
96 Correct 376 ms 39148 KB Output is correct
97 Correct 306 ms 40468 KB Output is correct