Submission #401012

# Submission time Handle Problem Language Result Execution time Memory
401012 2021-05-09T06:51:08 Z errorgorn From Hacks to Snitches (BOI21_watchmen) C++17
70 / 100
6000 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<int,int>
#define iii pair<int,ii>
#define fi first
#define se second
#define endl '\n'

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back

#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e)?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

int n,m,k;
vector<ii> edges;

const int EXT=1000000; //extra stuff for connecting 2 watchmen
vector<int> w[250005+EXT]; //period is implicit here
vector<int> al[250005+EXT];

priority_queue<iii,vector<iii>,greater<iii> > pq;
int upt[250005+EXT]; //time to update upwards

int id[250005+EXT]; //which watchmen
int bad[250005+EXT]; //when the watchmen comes
int ped[250005+EXT]; //period
int out[250005];

vector<ii> dial[10000005];

void read(int &x){
	x=0;
	char ch=getchar_unlocked();
	while (ch&16){
		x=(x<<3)+(x<<1)+(ch&15);
		ch=getchar_unlocked();
	}
}

int fix(int i,int j){
	i%=j;
	if (i<0) i+=j;
	return i;
}

void upd(int k,int i,int j){
	if (w[i][j]>k){
		w[i][j]=k;
		//pq.push(iii(k,ii(i,j)));
		if (k>10000000) return;
		dial[k].pub(ii(i,j));
	}
}

int main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	read(n),read(m);
	
	int a,b;
	rep(x,0,m){
		read(a),read(b);
		edges.pub(ii(a,b));
	}
	
	memset(ped,-1,sizeof(ped));
	memset(id,-1,sizeof(id));
	memset(bad,-1,sizeof(bad));
	
	read(k);
	rep(x,0,k){
		read(a);
		
		vector<int> path;
		rep(y,0,a){
			read(b);
			
			path.pub(b);
			bad[b]=y;
			id[b]=x;
			ped[b]=a;
			upt[b]=1;
		}
	}
	
	int IDX=n+1;
	
	rep(x,1,n+1) if (ped[x]!=-1){
		out[x]=IDX;
		al[x].pub(out[x]);
		IDX++;
	}
	
	for (auto &it:edges){
		tie(a,b)=it;
		if (id[a]>id[b]) swap(a,b);
		//cout<<id[a]<<" "<<id[b]<<endl;
		
		if (id[a]!=-1 && id[b]!=-1 && id[a]!=id[b]){
			al[a].pub(IDX);
			al[IDX].pub(b);
			ped[IDX]=ped[b];
			upt[IDX]=ped[a];
			IDX++;
			
			al[b].pub(IDX);
			al[IDX].pub(a);
			ped[IDX]=ped[a];
			upt[IDX]=ped[b];
			IDX++;
		}
		else if (id[a]==-1 && id[b]!=-1){
			al[a].pub(b);
			al[out[b]].pub(a);
		}
		else{
			al[a].pub(b);
			al[b].pub(a);
		}
	}
	
	rep(x,0,IDX){
		if (ped[x]==-1) w[x].pub(1e9);
		else{
			rep(y,0,ped[x]) w[x].pub(1e9);
		}
	}
	
	upd(0,1,0);
	
	int weight=0;
	while (weight<10000000){
		if (dial[weight].empty()){
			weight++;
			continue;
		}
		
		int n1,n2;
		tie(n1,n2)=dial[weight].back();
		dial[weight].pob();
		
		if (w[n1][n2]!=weight) continue;
		if (ped[n1]!=-1 && weight%ped[n1]==bad[n1]) continue;
		//same position as watchmen
		
		if (n1==n){
			cout<<weight<<endl;
			return 0;
		}
		
		//cout<<weight<<" "<<n1<<" "<<n2<<" "<<ped[n1]<<" "<<bad[n1]<<endl;
		
		for (auto it:al[n1]){
			if (ped[it]==-1){ //this is a problem....
				int ww=weight;
				if (n1<=n) ww++;
				upd(ww,it,0);
			}
			else if (ped[n1]==-1 && ped[it]!=-1){ //update all
				// we have to optmize this part...
				int ww=weight+1;
				upd(ww,it,ww%ped[it]);
				
				//also need one that lands immediately after watchmen pass
				ww=weight+fix(bad[it]-weight,ped[it])+1;
				//cout<<weight<<" "<<ww<<" "<<ped[it]<<" "<<bad[it]<<endl;
				upd(ww,it,ww%ped[it]);
			}
			else if (id[n1]==id[it]){
				int ww=weight+1;
				
				if ((bad[it]+1)%ped[it]==bad[n1] && ww%ped[it]==bad[n1]) continue;
				upd(ww,it,ww%ped[it]);
			}
			else{
				int ww=weight;
				if (n1<=n) ww++;
				
				upd(ww,it,ww%ped[it]);
			}
		}
		
		if (ped[n1]!=-1){
			int ww=weight+upt[n1];
			upd(ww,n1,ww%ped[n1]);
		}
	}
	
	
	/*
	rep(x,1,IDX){
		cout<<x<<": ";
		for (auto &it:w[x]) cout<<it<<" "; cout<<endl;
	}
	//*/
	
	cout<<"impossible"<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 164 ms 310720 KB Output is correct
2 Correct 227 ms 320548 KB Output is correct
3 Correct 215 ms 319164 KB Output is correct
4 Correct 236 ms 318716 KB Output is correct
5 Correct 161 ms 308548 KB Output is correct
6 Correct 214 ms 319004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 310664 KB Output is correct
2 Correct 227 ms 320512 KB Output is correct
3 Correct 213 ms 319156 KB Output is correct
4 Correct 236 ms 318764 KB Output is correct
5 Correct 158 ms 308676 KB Output is correct
6 Correct 213 ms 318884 KB Output is correct
7 Correct 216 ms 319364 KB Output is correct
8 Correct 214 ms 319284 KB Output is correct
9 Correct 219 ms 319376 KB Output is correct
10 Correct 216 ms 318468 KB Output is correct
11 Correct 208 ms 317604 KB Output is correct
12 Correct 211 ms 318796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 310664 KB Output is correct
2 Correct 227 ms 320512 KB Output is correct
3 Correct 213 ms 319156 KB Output is correct
4 Correct 236 ms 318764 KB Output is correct
5 Correct 158 ms 308676 KB Output is correct
6 Correct 213 ms 318884 KB Output is correct
7 Correct 216 ms 319364 KB Output is correct
8 Correct 214 ms 319284 KB Output is correct
9 Correct 219 ms 319376 KB Output is correct
10 Correct 216 ms 318468 KB Output is correct
11 Correct 208 ms 317604 KB Output is correct
12 Correct 211 ms 318796 KB Output is correct
13 Correct 167 ms 310828 KB Output is correct
14 Correct 226 ms 320620 KB Output is correct
15 Correct 213 ms 319160 KB Output is correct
16 Correct 239 ms 318768 KB Output is correct
17 Correct 159 ms 308548 KB Output is correct
18 Correct 214 ms 318908 KB Output is correct
19 Correct 220 ms 319164 KB Output is correct
20 Correct 215 ms 319224 KB Output is correct
21 Correct 219 ms 319424 KB Output is correct
22 Correct 222 ms 318216 KB Output is correct
23 Correct 212 ms 317628 KB Output is correct
24 Correct 216 ms 318800 KB Output is correct
25 Correct 1387 ms 387168 KB Output is correct
26 Correct 1304 ms 392592 KB Output is correct
27 Correct 1185 ms 388524 KB Output is correct
28 Correct 987 ms 393064 KB Output is correct
29 Correct 1374 ms 380144 KB Output is correct
30 Correct 1351 ms 382652 KB Output is correct
31 Correct 1457 ms 390412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 310664 KB Output is correct
2 Correct 227 ms 320512 KB Output is correct
3 Correct 213 ms 319156 KB Output is correct
4 Correct 236 ms 318764 KB Output is correct
5 Correct 158 ms 308676 KB Output is correct
6 Correct 213 ms 318884 KB Output is correct
7 Correct 216 ms 319364 KB Output is correct
8 Correct 214 ms 319284 KB Output is correct
9 Correct 219 ms 319376 KB Output is correct
10 Correct 216 ms 318468 KB Output is correct
11 Correct 208 ms 317604 KB Output is correct
12 Correct 211 ms 318796 KB Output is correct
13 Correct 167 ms 310828 KB Output is correct
14 Correct 226 ms 320620 KB Output is correct
15 Correct 213 ms 319160 KB Output is correct
16 Correct 239 ms 318768 KB Output is correct
17 Correct 159 ms 308548 KB Output is correct
18 Correct 214 ms 318908 KB Output is correct
19 Correct 220 ms 319164 KB Output is correct
20 Correct 215 ms 319224 KB Output is correct
21 Correct 219 ms 319424 KB Output is correct
22 Correct 222 ms 318216 KB Output is correct
23 Correct 212 ms 317628 KB Output is correct
24 Correct 216 ms 318800 KB Output is correct
25 Correct 1387 ms 387168 KB Output is correct
26 Correct 1304 ms 392592 KB Output is correct
27 Correct 1185 ms 388524 KB Output is correct
28 Correct 987 ms 393064 KB Output is correct
29 Correct 1374 ms 380144 KB Output is correct
30 Correct 1351 ms 382652 KB Output is correct
31 Correct 1457 ms 390412 KB Output is correct
32 Correct 167 ms 310788 KB Output is correct
33 Correct 232 ms 320528 KB Output is correct
34 Correct 220 ms 319164 KB Output is correct
35 Correct 257 ms 318696 KB Output is correct
36 Correct 166 ms 308588 KB Output is correct
37 Correct 225 ms 318928 KB Output is correct
38 Correct 240 ms 319204 KB Output is correct
39 Correct 244 ms 319280 KB Output is correct
40 Correct 245 ms 319460 KB Output is correct
41 Correct 257 ms 318224 KB Output is correct
42 Correct 222 ms 317576 KB Output is correct
43 Correct 234 ms 318812 KB Output is correct
44 Correct 1438 ms 387188 KB Output is correct
45 Correct 1407 ms 392624 KB Output is correct
46 Correct 1348 ms 388528 KB Output is correct
47 Correct 1092 ms 393028 KB Output is correct
48 Correct 1440 ms 380248 KB Output is correct
49 Correct 1461 ms 382628 KB Output is correct
50 Correct 1517 ms 390452 KB Output is correct
51 Correct 1795 ms 423024 KB Output is correct
52 Correct 2103 ms 493764 KB Output is correct
53 Correct 1763 ms 461804 KB Output is correct
54 Correct 1092 ms 426892 KB Output is correct
55 Execution timed out 6029 ms 434456 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 164 ms 310720 KB Output is correct
2 Correct 227 ms 320548 KB Output is correct
3 Correct 215 ms 319164 KB Output is correct
4 Correct 236 ms 318716 KB Output is correct
5 Correct 161 ms 308548 KB Output is correct
6 Correct 214 ms 319004 KB Output is correct
7 Correct 166 ms 310664 KB Output is correct
8 Correct 227 ms 320512 KB Output is correct
9 Correct 213 ms 319156 KB Output is correct
10 Correct 236 ms 318764 KB Output is correct
11 Correct 158 ms 308676 KB Output is correct
12 Correct 213 ms 318884 KB Output is correct
13 Correct 216 ms 319364 KB Output is correct
14 Correct 214 ms 319284 KB Output is correct
15 Correct 219 ms 319376 KB Output is correct
16 Correct 216 ms 318468 KB Output is correct
17 Correct 208 ms 317604 KB Output is correct
18 Correct 211 ms 318796 KB Output is correct
19 Correct 170 ms 308452 KB Output is correct
20 Correct 168 ms 308532 KB Output is correct
21 Correct 195 ms 308528 KB Output is correct
22 Correct 184 ms 310752 KB Output is correct
23 Correct 271 ms 320512 KB Output is correct
24 Correct 257 ms 319080 KB Output is correct
25 Correct 255 ms 318756 KB Output is correct
26 Correct 175 ms 308576 KB Output is correct
27 Correct 245 ms 319040 KB Output is correct
28 Correct 239 ms 319172 KB Output is correct
29 Correct 240 ms 319264 KB Output is correct
30 Correct 240 ms 319420 KB Output is correct
31 Correct 235 ms 318140 KB Output is correct
32 Correct 225 ms 317628 KB Output is correct
33 Correct 230 ms 318792 KB Output is correct
34 Correct 1505 ms 384760 KB Output is correct
35 Correct 1518 ms 417900 KB Output is correct
36 Correct 1540 ms 420256 KB Output is correct
37 Correct 1394 ms 425520 KB Output is correct
38 Correct 1525 ms 424588 KB Output is correct
39 Correct 1401 ms 418604 KB Output is correct
40 Correct 1469 ms 426348 KB Output is correct
41 Correct 1434 ms 423248 KB Output is correct
42 Correct 1502 ms 425700 KB Output is correct
43 Correct 1561 ms 425352 KB Output is correct
44 Correct 1545 ms 427644 KB Output is correct
45 Correct 1629 ms 422096 KB Output is correct
46 Correct 1513 ms 425168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 310720 KB Output is correct
2 Correct 227 ms 320548 KB Output is correct
3 Correct 215 ms 319164 KB Output is correct
4 Correct 236 ms 318716 KB Output is correct
5 Correct 161 ms 308548 KB Output is correct
6 Correct 214 ms 319004 KB Output is correct
7 Correct 166 ms 310664 KB Output is correct
8 Correct 227 ms 320512 KB Output is correct
9 Correct 213 ms 319156 KB Output is correct
10 Correct 236 ms 318764 KB Output is correct
11 Correct 158 ms 308676 KB Output is correct
12 Correct 213 ms 318884 KB Output is correct
13 Correct 216 ms 319364 KB Output is correct
14 Correct 214 ms 319284 KB Output is correct
15 Correct 219 ms 319376 KB Output is correct
16 Correct 216 ms 318468 KB Output is correct
17 Correct 208 ms 317604 KB Output is correct
18 Correct 211 ms 318796 KB Output is correct
19 Correct 167 ms 310828 KB Output is correct
20 Correct 226 ms 320620 KB Output is correct
21 Correct 213 ms 319160 KB Output is correct
22 Correct 239 ms 318768 KB Output is correct
23 Correct 159 ms 308548 KB Output is correct
24 Correct 214 ms 318908 KB Output is correct
25 Correct 220 ms 319164 KB Output is correct
26 Correct 215 ms 319224 KB Output is correct
27 Correct 219 ms 319424 KB Output is correct
28 Correct 222 ms 318216 KB Output is correct
29 Correct 212 ms 317628 KB Output is correct
30 Correct 216 ms 318800 KB Output is correct
31 Correct 1387 ms 387168 KB Output is correct
32 Correct 1304 ms 392592 KB Output is correct
33 Correct 1185 ms 388524 KB Output is correct
34 Correct 987 ms 393064 KB Output is correct
35 Correct 1374 ms 380144 KB Output is correct
36 Correct 1351 ms 382652 KB Output is correct
37 Correct 1457 ms 390412 KB Output is correct
38 Correct 170 ms 308452 KB Output is correct
39 Correct 168 ms 308532 KB Output is correct
40 Correct 195 ms 308528 KB Output is correct
41 Correct 184 ms 310752 KB Output is correct
42 Correct 271 ms 320512 KB Output is correct
43 Correct 257 ms 319080 KB Output is correct
44 Correct 255 ms 318756 KB Output is correct
45 Correct 175 ms 308576 KB Output is correct
46 Correct 245 ms 319040 KB Output is correct
47 Correct 239 ms 319172 KB Output is correct
48 Correct 240 ms 319264 KB Output is correct
49 Correct 240 ms 319420 KB Output is correct
50 Correct 235 ms 318140 KB Output is correct
51 Correct 225 ms 317628 KB Output is correct
52 Correct 230 ms 318792 KB Output is correct
53 Correct 1505 ms 384760 KB Output is correct
54 Correct 1518 ms 417900 KB Output is correct
55 Correct 1540 ms 420256 KB Output is correct
56 Correct 1394 ms 425520 KB Output is correct
57 Correct 1525 ms 424588 KB Output is correct
58 Correct 1401 ms 418604 KB Output is correct
59 Correct 1469 ms 426348 KB Output is correct
60 Correct 1434 ms 423248 KB Output is correct
61 Correct 1502 ms 425700 KB Output is correct
62 Correct 1561 ms 425352 KB Output is correct
63 Correct 1545 ms 427644 KB Output is correct
64 Correct 1629 ms 422096 KB Output is correct
65 Correct 1513 ms 425168 KB Output is correct
66 Correct 181 ms 308516 KB Output is correct
67 Correct 182 ms 308476 KB Output is correct
68 Correct 235 ms 308584 KB Output is correct
69 Correct 190 ms 310764 KB Output is correct
70 Correct 262 ms 320544 KB Output is correct
71 Correct 242 ms 319168 KB Output is correct
72 Correct 263 ms 318656 KB Output is correct
73 Correct 184 ms 308652 KB Output is correct
74 Correct 249 ms 318900 KB Output is correct
75 Correct 252 ms 319240 KB Output is correct
76 Correct 251 ms 319288 KB Output is correct
77 Correct 251 ms 319424 KB Output is correct
78 Correct 246 ms 318144 KB Output is correct
79 Correct 243 ms 317660 KB Output is correct
80 Correct 243 ms 318784 KB Output is correct
81 Correct 1512 ms 425460 KB Output is correct
82 Correct 1437 ms 431068 KB Output is correct
83 Correct 1314 ms 426780 KB Output is correct
84 Correct 1127 ms 431376 KB Output is correct
85 Correct 1502 ms 418800 KB Output is correct
86 Correct 1532 ms 420964 KB Output is correct
87 Correct 1580 ms 428736 KB Output is correct
88 Correct 1568 ms 422856 KB Output is correct
89 Correct 1601 ms 417936 KB Output is correct
90 Correct 1594 ms 420156 KB Output is correct
91 Correct 1408 ms 425788 KB Output is correct
92 Correct 1559 ms 424792 KB Output is correct
93 Correct 1424 ms 418636 KB Output is correct
94 Correct 1487 ms 426312 KB Output is correct
95 Correct 1486 ms 423252 KB Output is correct
96 Correct 1483 ms 425668 KB Output is correct
97 Correct 1578 ms 425172 KB Output is correct
98 Correct 1613 ms 427184 KB Output is correct
99 Correct 1687 ms 421868 KB Output is correct
100 Correct 1498 ms 424884 KB Output is correct
101 Correct 1549 ms 423632 KB Output is correct
102 Correct 1411 ms 426508 KB Output is correct
103 Correct 1535 ms 425876 KB Output is correct
104 Correct 4019 ms 524288 KB Output is correct
105 Correct 1798 ms 448184 KB Output is correct
106 Correct 1649 ms 423224 KB Output is correct
107 Correct 1545 ms 428460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 310720 KB Output is correct
2 Correct 227 ms 320548 KB Output is correct
3 Correct 215 ms 319164 KB Output is correct
4 Correct 236 ms 318716 KB Output is correct
5 Correct 161 ms 308548 KB Output is correct
6 Correct 214 ms 319004 KB Output is correct
7 Correct 166 ms 310664 KB Output is correct
8 Correct 227 ms 320512 KB Output is correct
9 Correct 213 ms 319156 KB Output is correct
10 Correct 236 ms 318764 KB Output is correct
11 Correct 158 ms 308676 KB Output is correct
12 Correct 213 ms 318884 KB Output is correct
13 Correct 216 ms 319364 KB Output is correct
14 Correct 214 ms 319284 KB Output is correct
15 Correct 219 ms 319376 KB Output is correct
16 Correct 216 ms 318468 KB Output is correct
17 Correct 208 ms 317604 KB Output is correct
18 Correct 211 ms 318796 KB Output is correct
19 Correct 167 ms 310828 KB Output is correct
20 Correct 226 ms 320620 KB Output is correct
21 Correct 213 ms 319160 KB Output is correct
22 Correct 239 ms 318768 KB Output is correct
23 Correct 159 ms 308548 KB Output is correct
24 Correct 214 ms 318908 KB Output is correct
25 Correct 220 ms 319164 KB Output is correct
26 Correct 215 ms 319224 KB Output is correct
27 Correct 219 ms 319424 KB Output is correct
28 Correct 222 ms 318216 KB Output is correct
29 Correct 212 ms 317628 KB Output is correct
30 Correct 216 ms 318800 KB Output is correct
31 Correct 1387 ms 387168 KB Output is correct
32 Correct 1304 ms 392592 KB Output is correct
33 Correct 1185 ms 388524 KB Output is correct
34 Correct 987 ms 393064 KB Output is correct
35 Correct 1374 ms 380144 KB Output is correct
36 Correct 1351 ms 382652 KB Output is correct
37 Correct 1457 ms 390412 KB Output is correct
38 Correct 167 ms 310788 KB Output is correct
39 Correct 232 ms 320528 KB Output is correct
40 Correct 220 ms 319164 KB Output is correct
41 Correct 257 ms 318696 KB Output is correct
42 Correct 166 ms 308588 KB Output is correct
43 Correct 225 ms 318928 KB Output is correct
44 Correct 240 ms 319204 KB Output is correct
45 Correct 244 ms 319280 KB Output is correct
46 Correct 245 ms 319460 KB Output is correct
47 Correct 257 ms 318224 KB Output is correct
48 Correct 222 ms 317576 KB Output is correct
49 Correct 234 ms 318812 KB Output is correct
50 Correct 1438 ms 387188 KB Output is correct
51 Correct 1407 ms 392624 KB Output is correct
52 Correct 1348 ms 388528 KB Output is correct
53 Correct 1092 ms 393028 KB Output is correct
54 Correct 1440 ms 380248 KB Output is correct
55 Correct 1461 ms 382628 KB Output is correct
56 Correct 1517 ms 390452 KB Output is correct
57 Correct 1795 ms 423024 KB Output is correct
58 Correct 2103 ms 493764 KB Output is correct
59 Correct 1763 ms 461804 KB Output is correct
60 Correct 1092 ms 426892 KB Output is correct
61 Execution timed out 6029 ms 434456 KB Time limit exceeded
62 Halted 0 ms 0 KB -