Submission #351019

# Submission time Handle Problem Language Result Execution time Memory
351019 2021-01-19T11:18:08 Z Kerim Swapping Cities (APIO20_swap) C++17
100 / 100
541 ms 61408 KB
#include "swap.h"
#include "bits/stdc++.h"
#define MAXN 300009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
vector<int>u,v,w,ind;
int m,n;
int ata[MAXN],bad[MAXN],deg[MAXN];
int tap(int x){
	if(ata[x]==x)return x;
	return ata[x]=tap(ata[x]);	
}
bool cmp(int x,int y){
	return (w[x]<w[y]);	
}
const int LOG=19;
int lvl[MAXN],P[MAXN][LOG];
vector<int>adj[MAXN];
PII arr[MAXN];
void add(int x,int y){
	P[y][0]=x;adj[x].pb(y);
	//cerr<<x<<"->"<<y<<endl;	
}
void merge(int x,int y,int val){
	deg[x]++;deg[y]++;
	int so=(max(deg[x],deg[y])>=3);
	//printf("HI %d %d %d\n",x,y,val);
	x=tap(x);y=tap(y);
	if(x==y)
		bad[n]=1,ata[x]=n,add(n,x);
	else{
		add(n,x);add(n,y);
		ata[y]=ata[x]=n;
		bad[n]=bad[x]|bad[y]|so;
	}
	//printf("%d %d\n",val,bad[n]);
	arr[n++]=mp(val,bad[n]);
}
void dfs(int nd){tr(it,adj[nd])lvl[*it]=lvl[nd]+1,dfs(*it);}
void init(int N, int M,vector<int>U,vector<int> V,vector<int> W) {
	u=U;v=V;w=W;m=M;n=N;
	for(int i=0;i<m;i++)ind.pb(i);
	sort(all(ind),cmp);
	for(int i=0;i<m+n;i++)ata[i]=i;
	for(int i=0;i<m;i++)
		merge(u[ind[i]],v[ind[i]],w[ind[i]]);
	for(int j=1;j<LOG;j++)
		for(int i=0;i<n;i++)
			if(P[i][j-1])
				P[i][j]=P[P[i][j-1]][j-1];
	dfs(n-1);
}
int getMinimumFuelCapacity(int x, int y){
	if(lvl[x]<lvl[y])swap(x,y);
	for(int j=LOG-1;j>=0;j--)
		if(P[x][j] and lvl[P[x][j]]>=lvl[y])
			x=P[x][j];
	for(int j=LOG-1;j>=0;j--)
		if(P[x][j] and P[x][j]!=P[y][j])
			x=P[x][j],y=P[y][j];
	x=P[x][0];
	if(arr[x].ss)return arr[x].ff;
	for(int j=LOG-1;j>=0;j--)
		if(P[x][j] and !arr[P[x][j]].ss)
			x=P[x][j];
	x=P[x][0];
	if(!x)return -1;
	return arr[x].ff;
}

Compilation message

swap.cpp: In function 'void merge(int, int, int)':
swap.cpp:52:7: warning: operation on 'n' may be undefined [-Wsequence-point]
   52 |  arr[n++]=mp(val,bad[n]);
      |      ~^~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7532 KB Output is correct
5 Correct 6 ms 7660 KB Output is correct
6 Correct 7 ms 7660 KB Output is correct
7 Correct 6 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 118 ms 27108 KB Output is correct
10 Correct 155 ms 31516 KB Output is correct
11 Correct 141 ms 31076 KB Output is correct
12 Correct 154 ms 32484 KB Output is correct
13 Correct 146 ms 34788 KB Output is correct
14 Correct 137 ms 27188 KB Output is correct
15 Correct 338 ms 33776 KB Output is correct
16 Correct 331 ms 32864 KB Output is correct
17 Correct 368 ms 34380 KB Output is correct
18 Correct 533 ms 36812 KB Output is correct
19 Correct 137 ms 17024 KB Output is correct
20 Correct 398 ms 38904 KB Output is correct
21 Correct 369 ms 38292 KB Output is correct
22 Correct 327 ms 40140 KB Output is correct
23 Correct 509 ms 42444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 445 ms 38632 KB Output is correct
4 Correct 492 ms 40012 KB Output is correct
5 Correct 459 ms 39192 KB Output is correct
6 Correct 428 ms 39936 KB Output is correct
7 Correct 475 ms 39652 KB Output is correct
8 Correct 444 ms 38432 KB Output is correct
9 Correct 413 ms 39396 KB Output is correct
10 Correct 443 ms 38272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 6 ms 7532 KB Output is correct
6 Correct 6 ms 7660 KB Output is correct
7 Correct 7 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 6 ms 7660 KB Output is correct
10 Correct 6 ms 7660 KB Output is correct
11 Correct 7 ms 7660 KB Output is correct
12 Correct 6 ms 7660 KB Output is correct
13 Correct 6 ms 7660 KB Output is correct
14 Correct 6 ms 7660 KB Output is correct
15 Correct 6 ms 7660 KB Output is correct
16 Correct 6 ms 7660 KB Output is correct
17 Correct 7 ms 7660 KB Output is correct
18 Correct 7 ms 7660 KB Output is correct
19 Correct 6 ms 7660 KB Output is correct
20 Correct 7 ms 7660 KB Output is correct
21 Correct 6 ms 7660 KB Output is correct
22 Correct 7 ms 7916 KB Output is correct
23 Correct 6 ms 7660 KB Output is correct
24 Correct 7 ms 7788 KB Output is correct
25 Correct 7 ms 7788 KB Output is correct
26 Correct 7 ms 7916 KB Output is correct
27 Correct 6 ms 7660 KB Output is correct
28 Correct 6 ms 7916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 6 ms 7532 KB Output is correct
6 Correct 6 ms 7660 KB Output is correct
7 Correct 7 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 6 ms 7660 KB Output is correct
10 Correct 118 ms 27108 KB Output is correct
11 Correct 155 ms 31516 KB Output is correct
12 Correct 141 ms 31076 KB Output is correct
13 Correct 154 ms 32484 KB Output is correct
14 Correct 146 ms 34788 KB Output is correct
15 Correct 6 ms 7660 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 6 ms 7660 KB Output is correct
18 Correct 6 ms 7660 KB Output is correct
19 Correct 6 ms 7660 KB Output is correct
20 Correct 6 ms 7660 KB Output is correct
21 Correct 6 ms 7660 KB Output is correct
22 Correct 7 ms 7660 KB Output is correct
23 Correct 7 ms 7660 KB Output is correct
24 Correct 19 ms 10860 KB Output is correct
25 Correct 157 ms 32484 KB Output is correct
26 Correct 156 ms 32484 KB Output is correct
27 Correct 153 ms 32484 KB Output is correct
28 Correct 146 ms 32228 KB Output is correct
29 Correct 176 ms 31972 KB Output is correct
30 Correct 145 ms 29924 KB Output is correct
31 Correct 164 ms 32612 KB Output is correct
32 Correct 171 ms 32660 KB Output is correct
33 Correct 147 ms 35556 KB Output is correct
34 Correct 174 ms 32484 KB Output is correct
35 Correct 6 ms 7660 KB Output is correct
36 Correct 7 ms 7660 KB Output is correct
37 Correct 6 ms 7660 KB Output is correct
38 Correct 7 ms 7916 KB Output is correct
39 Correct 6 ms 7660 KB Output is correct
40 Correct 7 ms 7788 KB Output is correct
41 Correct 7 ms 7788 KB Output is correct
42 Correct 7 ms 7916 KB Output is correct
43 Correct 6 ms 7660 KB Output is correct
44 Correct 6 ms 7916 KB Output is correct
45 Correct 213 ms 42848 KB Output is correct
46 Correct 154 ms 32484 KB Output is correct
47 Correct 161 ms 32612 KB Output is correct
48 Correct 188 ms 35812 KB Output is correct
49 Correct 157 ms 40772 KB Output is correct
50 Correct 129 ms 33252 KB Output is correct
51 Correct 177 ms 37988 KB Output is correct
52 Correct 247 ms 45664 KB Output is correct
53 Correct 283 ms 48224 KB Output is correct
54 Correct 294 ms 53600 KB Output is correct
55 Correct 164 ms 35296 KB Output is correct
56 Correct 265 ms 48992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7532 KB Output is correct
5 Correct 6 ms 7660 KB Output is correct
6 Correct 7 ms 7660 KB Output is correct
7 Correct 6 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 118 ms 27108 KB Output is correct
10 Correct 155 ms 31516 KB Output is correct
11 Correct 141 ms 31076 KB Output is correct
12 Correct 154 ms 32484 KB Output is correct
13 Correct 146 ms 34788 KB Output is correct
14 Correct 137 ms 27188 KB Output is correct
15 Correct 338 ms 33776 KB Output is correct
16 Correct 331 ms 32864 KB Output is correct
17 Correct 368 ms 34380 KB Output is correct
18 Correct 533 ms 36812 KB Output is correct
19 Correct 445 ms 38632 KB Output is correct
20 Correct 492 ms 40012 KB Output is correct
21 Correct 459 ms 39192 KB Output is correct
22 Correct 428 ms 39936 KB Output is correct
23 Correct 475 ms 39652 KB Output is correct
24 Correct 444 ms 38432 KB Output is correct
25 Correct 413 ms 39396 KB Output is correct
26 Correct 443 ms 38272 KB Output is correct
27 Correct 6 ms 7660 KB Output is correct
28 Correct 7 ms 7660 KB Output is correct
29 Correct 6 ms 7660 KB Output is correct
30 Correct 6 ms 7660 KB Output is correct
31 Correct 6 ms 7660 KB Output is correct
32 Correct 6 ms 7660 KB Output is correct
33 Correct 6 ms 7660 KB Output is correct
34 Correct 7 ms 7660 KB Output is correct
35 Correct 7 ms 7660 KB Output is correct
36 Correct 19 ms 10860 KB Output is correct
37 Correct 157 ms 32484 KB Output is correct
38 Correct 156 ms 32484 KB Output is correct
39 Correct 153 ms 32484 KB Output is correct
40 Correct 146 ms 32228 KB Output is correct
41 Correct 176 ms 31972 KB Output is correct
42 Correct 145 ms 29924 KB Output is correct
43 Correct 164 ms 32612 KB Output is correct
44 Correct 171 ms 32660 KB Output is correct
45 Correct 147 ms 35556 KB Output is correct
46 Correct 174 ms 32484 KB Output is correct
47 Correct 34 ms 11372 KB Output is correct
48 Correct 325 ms 39244 KB Output is correct
49 Correct 328 ms 39244 KB Output is correct
50 Correct 319 ms 39200 KB Output is correct
51 Correct 319 ms 39128 KB Output is correct
52 Correct 296 ms 37524 KB Output is correct
53 Correct 289 ms 31320 KB Output is correct
54 Correct 324 ms 40140 KB Output is correct
55 Correct 345 ms 39264 KB Output is correct
56 Correct 488 ms 41676 KB Output is correct
57 Correct 329 ms 40184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 6 ms 7532 KB Output is correct
6 Correct 6 ms 7660 KB Output is correct
7 Correct 7 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 6 ms 7660 KB Output is correct
10 Correct 118 ms 27108 KB Output is correct
11 Correct 155 ms 31516 KB Output is correct
12 Correct 141 ms 31076 KB Output is correct
13 Correct 154 ms 32484 KB Output is correct
14 Correct 146 ms 34788 KB Output is correct
15 Correct 137 ms 27188 KB Output is correct
16 Correct 338 ms 33776 KB Output is correct
17 Correct 331 ms 32864 KB Output is correct
18 Correct 368 ms 34380 KB Output is correct
19 Correct 533 ms 36812 KB Output is correct
20 Correct 445 ms 38632 KB Output is correct
21 Correct 492 ms 40012 KB Output is correct
22 Correct 459 ms 39192 KB Output is correct
23 Correct 428 ms 39936 KB Output is correct
24 Correct 475 ms 39652 KB Output is correct
25 Correct 444 ms 38432 KB Output is correct
26 Correct 413 ms 39396 KB Output is correct
27 Correct 443 ms 38272 KB Output is correct
28 Correct 6 ms 7660 KB Output is correct
29 Correct 7 ms 7660 KB Output is correct
30 Correct 6 ms 7660 KB Output is correct
31 Correct 6 ms 7660 KB Output is correct
32 Correct 6 ms 7660 KB Output is correct
33 Correct 6 ms 7660 KB Output is correct
34 Correct 6 ms 7660 KB Output is correct
35 Correct 7 ms 7660 KB Output is correct
36 Correct 7 ms 7660 KB Output is correct
37 Correct 19 ms 10860 KB Output is correct
38 Correct 157 ms 32484 KB Output is correct
39 Correct 156 ms 32484 KB Output is correct
40 Correct 153 ms 32484 KB Output is correct
41 Correct 146 ms 32228 KB Output is correct
42 Correct 176 ms 31972 KB Output is correct
43 Correct 145 ms 29924 KB Output is correct
44 Correct 164 ms 32612 KB Output is correct
45 Correct 171 ms 32660 KB Output is correct
46 Correct 147 ms 35556 KB Output is correct
47 Correct 174 ms 32484 KB Output is correct
48 Correct 34 ms 11372 KB Output is correct
49 Correct 325 ms 39244 KB Output is correct
50 Correct 328 ms 39244 KB Output is correct
51 Correct 319 ms 39200 KB Output is correct
52 Correct 319 ms 39128 KB Output is correct
53 Correct 296 ms 37524 KB Output is correct
54 Correct 289 ms 31320 KB Output is correct
55 Correct 324 ms 40140 KB Output is correct
56 Correct 345 ms 39264 KB Output is correct
57 Correct 488 ms 41676 KB Output is correct
58 Correct 329 ms 40184 KB Output is correct
59 Correct 137 ms 17024 KB Output is correct
60 Correct 398 ms 38904 KB Output is correct
61 Correct 369 ms 38292 KB Output is correct
62 Correct 327 ms 40140 KB Output is correct
63 Correct 509 ms 42444 KB Output is correct
64 Correct 6 ms 7660 KB Output is correct
65 Correct 7 ms 7660 KB Output is correct
66 Correct 6 ms 7660 KB Output is correct
67 Correct 7 ms 7916 KB Output is correct
68 Correct 6 ms 7660 KB Output is correct
69 Correct 7 ms 7788 KB Output is correct
70 Correct 7 ms 7788 KB Output is correct
71 Correct 7 ms 7916 KB Output is correct
72 Correct 6 ms 7660 KB Output is correct
73 Correct 6 ms 7916 KB Output is correct
74 Correct 213 ms 42848 KB Output is correct
75 Correct 154 ms 32484 KB Output is correct
76 Correct 161 ms 32612 KB Output is correct
77 Correct 188 ms 35812 KB Output is correct
78 Correct 157 ms 40772 KB Output is correct
79 Correct 129 ms 33252 KB Output is correct
80 Correct 177 ms 37988 KB Output is correct
81 Correct 247 ms 45664 KB Output is correct
82 Correct 283 ms 48224 KB Output is correct
83 Correct 294 ms 53600 KB Output is correct
84 Correct 164 ms 35296 KB Output is correct
85 Correct 265 ms 48992 KB Output is correct
86 Correct 107 ms 21916 KB Output is correct
87 Correct 326 ms 39244 KB Output is correct
88 Correct 339 ms 39276 KB Output is correct
89 Correct 450 ms 41392 KB Output is correct
90 Correct 303 ms 49596 KB Output is correct
91 Correct 335 ms 46832 KB Output is correct
92 Correct 456 ms 44484 KB Output is correct
93 Correct 405 ms 53148 KB Output is correct
94 Correct 541 ms 56516 KB Output is correct
95 Correct 525 ms 61408 KB Output is correct
96 Correct 511 ms 42060 KB Output is correct
97 Correct 514 ms 49136 KB Output is correct