Submission #434557

# Submission time Handle Problem Language Result Execution time Memory
434557 2021-06-21T12:00:08 Z dqhungdl Swapping Cities (APIO20_swap) C++17
100 / 100
301 ms 30628 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int MAX=1e5+5;
int P[MAX];
bool isLine[MAX];
ii segs[MAX];
vector<int> S[MAX];
vector<iii> edges;
vector<ii> history[MAX];

int find(int u) {
	if(P[u]==u)
		return u;
	return P[u]=find(P[u]);
}

ii getNewSeg(ii s1,ii s2,int u,int v) {
	if(s1.first==u) {
		if(s2.first==v)
			return {s1.second,s2.second};
		if(s2.second==v)
			return {s1.second,s2.first};
	} else if(s1.second==u) {
		if(s2.first==v)
			return {s1.first,s2.second};
		if(s2.second==v)
			return {s1.first,s2.first};
	}
	return {-1,-1};
}

void merge(int u,int v,int w,int rootU,int rootV) {
	if(S[u].size()<S[v].size()) {
		swap(u,v);
		swap(rootU,rootV);
	}
	P[v]=u;
	//cerr<<segs[u].first<<' '<<segs[u].second<<'\n';
	//cerr<<segs[v].first<<' '<<segs[v].second<<'\n';
	//cerr<<rootU<<' '<<rootV<<'\n';
	segs[u]=getNewSeg(segs[u],segs[v],rootU,rootV);
	//cerr<<segs[u].first<<' '<<segs[u].second<<"\n\n";
	bool newIsLine=(segs[u].first!=-1);
	if(isLine[u]!=newIsLine) {
		isLine[u]=false;
		for(int tmp:S[u])
			history[tmp].push_back({w,u});
	}
	while(S[v].size()>0) {
		int tmp=S[v].back();
		S[v].pop_back();
		S[u].push_back(tmp);
		if(!newIsLine)
			history[tmp].push_back({w,u});
	}
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	for(int i=0;i<M;i++)
		edges.push_back({W[i],{U[i],V[i]}});
	sort(edges.begin(),edges.end());
	for(int i=0;i<N;i++) {
		P[i]=i;
		isLine[i]=true;
		segs[i]={i,i};
		S[i].push_back(i);
	}
	for(iii edge:edges) {
		int u=find(edge.second.first),v=find(edge.second.second),w=edge.first;
		if(u!=v)
			merge(u,v,w,edge.second.first,edge.second.second);
		else if(isLine[u]) {
			isLine[u]=false;
			segs[u]={-1,-1};
			for(int tmp:S[u])
				history[tmp].push_back({w,u});
		}
	}
	//for(ii tmp:history[523])
	//	cerr<<tmp.first<<' '<<tmp.second<<'\n';
}

int getMinimumFuelCapacity(int X, int Y) {
	vector<ii> hisX=history[X],hisY=history[Y];
	int rs=-1;
	while(hisX.size()>0&&hisY.size()>0&&hisX.back().second==hisY.back().second) {
		rs=max(hisX.back().first,hisY.back().first);
		hisX.pop_back(),hisY.pop_back();
	}
	return rs;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5040 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 82 ms 14600 KB Output is correct
10 Correct 100 ms 16316 KB Output is correct
11 Correct 97 ms 16048 KB Output is correct
12 Correct 83 ms 16744 KB Output is correct
13 Correct 66 ms 14100 KB Output is correct
14 Correct 70 ms 14564 KB Output is correct
15 Correct 149 ms 18448 KB Output is correct
16 Correct 143 ms 17900 KB Output is correct
17 Correct 152 ms 18664 KB Output is correct
18 Correct 133 ms 15776 KB Output is correct
19 Correct 81 ms 10476 KB Output is correct
20 Correct 185 ms 21296 KB Output is correct
21 Correct 182 ms 21052 KB Output is correct
22 Correct 217 ms 21884 KB Output is correct
23 Correct 175 ms 19892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 186 ms 18788 KB Output is correct
4 Correct 177 ms 19200 KB Output is correct
5 Correct 176 ms 19152 KB Output is correct
6 Correct 199 ms 19100 KB Output is correct
7 Correct 188 ms 19380 KB Output is correct
8 Correct 177 ms 18788 KB Output is correct
9 Correct 234 ms 19128 KB Output is correct
10 Correct 171 ms 18672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5040 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 5068 KB Output is correct
11 Correct 4 ms 5088 KB Output is correct
12 Correct 4 ms 5068 KB Output is correct
13 Correct 4 ms 5068 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 6 ms 5088 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 5144 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5008 KB Output is correct
24 Correct 4 ms 5196 KB Output is correct
25 Correct 4 ms 5196 KB Output is correct
26 Correct 4 ms 5196 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5040 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 82 ms 14600 KB Output is correct
11 Correct 100 ms 16316 KB Output is correct
12 Correct 97 ms 16048 KB Output is correct
13 Correct 83 ms 16744 KB Output is correct
14 Correct 66 ms 14100 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 4 ms 5088 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 5068 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 6 ms 5088 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5144 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5008 KB Output is correct
29 Correct 4 ms 5196 KB Output is correct
30 Correct 4 ms 5196 KB Output is correct
31 Correct 4 ms 5196 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5196 KB Output is correct
34 Correct 17 ms 7272 KB Output is correct
35 Correct 116 ms 20536 KB Output is correct
36 Correct 105 ms 20484 KB Output is correct
37 Correct 119 ms 21216 KB Output is correct
38 Correct 114 ms 21112 KB Output is correct
39 Correct 160 ms 22164 KB Output is correct
40 Correct 126 ms 21008 KB Output is correct
41 Correct 98 ms 21176 KB Output is correct
42 Correct 96 ms 20532 KB Output is correct
43 Correct 101 ms 18660 KB Output is correct
44 Correct 154 ms 23128 KB Output is correct
45 Correct 123 ms 20012 KB Output is correct
46 Correct 103 ms 20492 KB Output is correct
47 Correct 107 ms 19600 KB Output is correct
48 Correct 121 ms 19316 KB Output is correct
49 Correct 82 ms 14648 KB Output is correct
50 Correct 59 ms 12152 KB Output is correct
51 Correct 96 ms 17744 KB Output is correct
52 Correct 141 ms 24112 KB Output is correct
53 Correct 195 ms 25788 KB Output is correct
54 Correct 190 ms 26416 KB Output is correct
55 Correct 94 ms 18624 KB Output is correct
56 Correct 158 ms 23412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5040 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 82 ms 14600 KB Output is correct
10 Correct 100 ms 16316 KB Output is correct
11 Correct 97 ms 16048 KB Output is correct
12 Correct 83 ms 16744 KB Output is correct
13 Correct 66 ms 14100 KB Output is correct
14 Correct 70 ms 14564 KB Output is correct
15 Correct 149 ms 18448 KB Output is correct
16 Correct 143 ms 17900 KB Output is correct
17 Correct 152 ms 18664 KB Output is correct
18 Correct 133 ms 15776 KB Output is correct
19 Correct 186 ms 18788 KB Output is correct
20 Correct 177 ms 19200 KB Output is correct
21 Correct 176 ms 19152 KB Output is correct
22 Correct 199 ms 19100 KB Output is correct
23 Correct 188 ms 19380 KB Output is correct
24 Correct 177 ms 18788 KB Output is correct
25 Correct 234 ms 19128 KB Output is correct
26 Correct 171 ms 18672 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5088 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 6 ms 5088 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5144 KB Output is correct
36 Correct 17 ms 7272 KB Output is correct
37 Correct 116 ms 20536 KB Output is correct
38 Correct 105 ms 20484 KB Output is correct
39 Correct 119 ms 21216 KB Output is correct
40 Correct 114 ms 21112 KB Output is correct
41 Correct 160 ms 22164 KB Output is correct
42 Correct 126 ms 21008 KB Output is correct
43 Correct 98 ms 21176 KB Output is correct
44 Correct 96 ms 20532 KB Output is correct
45 Correct 101 ms 18660 KB Output is correct
46 Correct 154 ms 23128 KB Output is correct
47 Correct 28 ms 7724 KB Output is correct
48 Correct 197 ms 25320 KB Output is correct
49 Correct 196 ms 25604 KB Output is correct
50 Correct 301 ms 26244 KB Output is correct
51 Correct 230 ms 26480 KB Output is correct
52 Correct 267 ms 26104 KB Output is correct
53 Correct 174 ms 22828 KB Output is correct
54 Correct 208 ms 26244 KB Output is correct
55 Correct 193 ms 25520 KB Output is correct
56 Correct 190 ms 23516 KB Output is correct
57 Correct 294 ms 28340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5040 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 82 ms 14600 KB Output is correct
11 Correct 100 ms 16316 KB Output is correct
12 Correct 97 ms 16048 KB Output is correct
13 Correct 83 ms 16744 KB Output is correct
14 Correct 66 ms 14100 KB Output is correct
15 Correct 70 ms 14564 KB Output is correct
16 Correct 149 ms 18448 KB Output is correct
17 Correct 143 ms 17900 KB Output is correct
18 Correct 152 ms 18664 KB Output is correct
19 Correct 133 ms 15776 KB Output is correct
20 Correct 186 ms 18788 KB Output is correct
21 Correct 177 ms 19200 KB Output is correct
22 Correct 176 ms 19152 KB Output is correct
23 Correct 199 ms 19100 KB Output is correct
24 Correct 188 ms 19380 KB Output is correct
25 Correct 177 ms 18788 KB Output is correct
26 Correct 234 ms 19128 KB Output is correct
27 Correct 171 ms 18672 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5088 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 6 ms 5088 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5144 KB Output is correct
37 Correct 17 ms 7272 KB Output is correct
38 Correct 116 ms 20536 KB Output is correct
39 Correct 105 ms 20484 KB Output is correct
40 Correct 119 ms 21216 KB Output is correct
41 Correct 114 ms 21112 KB Output is correct
42 Correct 160 ms 22164 KB Output is correct
43 Correct 126 ms 21008 KB Output is correct
44 Correct 98 ms 21176 KB Output is correct
45 Correct 96 ms 20532 KB Output is correct
46 Correct 101 ms 18660 KB Output is correct
47 Correct 154 ms 23128 KB Output is correct
48 Correct 28 ms 7724 KB Output is correct
49 Correct 197 ms 25320 KB Output is correct
50 Correct 196 ms 25604 KB Output is correct
51 Correct 301 ms 26244 KB Output is correct
52 Correct 230 ms 26480 KB Output is correct
53 Correct 267 ms 26104 KB Output is correct
54 Correct 174 ms 22828 KB Output is correct
55 Correct 208 ms 26244 KB Output is correct
56 Correct 193 ms 25520 KB Output is correct
57 Correct 190 ms 23516 KB Output is correct
58 Correct 294 ms 28340 KB Output is correct
59 Correct 81 ms 10476 KB Output is correct
60 Correct 185 ms 21296 KB Output is correct
61 Correct 182 ms 21052 KB Output is correct
62 Correct 217 ms 21884 KB Output is correct
63 Correct 175 ms 19892 KB Output is correct
64 Correct 4 ms 5068 KB Output is correct
65 Correct 4 ms 5068 KB Output is correct
66 Correct 4 ms 5068 KB Output is correct
67 Correct 4 ms 5068 KB Output is correct
68 Correct 4 ms 5008 KB Output is correct
69 Correct 4 ms 5196 KB Output is correct
70 Correct 4 ms 5196 KB Output is correct
71 Correct 4 ms 5196 KB Output is correct
72 Correct 4 ms 5068 KB Output is correct
73 Correct 4 ms 5196 KB Output is correct
74 Correct 123 ms 20012 KB Output is correct
75 Correct 103 ms 20492 KB Output is correct
76 Correct 107 ms 19600 KB Output is correct
77 Correct 121 ms 19316 KB Output is correct
78 Correct 82 ms 14648 KB Output is correct
79 Correct 59 ms 12152 KB Output is correct
80 Correct 96 ms 17744 KB Output is correct
81 Correct 141 ms 24112 KB Output is correct
82 Correct 195 ms 25788 KB Output is correct
83 Correct 190 ms 26416 KB Output is correct
84 Correct 94 ms 18624 KB Output is correct
85 Correct 158 ms 23412 KB Output is correct
86 Correct 62 ms 11520 KB Output is correct
87 Correct 229 ms 25144 KB Output is correct
88 Correct 198 ms 25632 KB Output is correct
89 Correct 226 ms 23400 KB Output is correct
90 Correct 192 ms 18452 KB Output is correct
91 Correct 151 ms 18528 KB Output is correct
92 Correct 184 ms 22552 KB Output is correct
93 Correct 237 ms 28496 KB Output is correct
94 Correct 291 ms 30188 KB Output is correct
95 Correct 248 ms 30628 KB Output is correct
96 Correct 254 ms 23660 KB Output is correct
97 Correct 236 ms 26492 KB Output is correct