답안 #587525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
587525 2022-07-02T04:39:47 Z czhang2718 Jail (JOI22_jail) C++17
100 / 100
1832 ms 341104 KB
#include "bits/stdc++.h"
using namespace std;

#define rep(i,a,b) for(int i=a; i<=b; i++)
#define f first
#define s second
#define pb push_back
#define nl '\n'
typedef vector<int> vi;
typedef pair<int, int> pii;

const int N=120001;
const int K=17;
const int BN=4800001;
int t, n, m;
bool cyc;
vi adj[N];
vi adj2[BN];
bool seen[BN], vis[BN];
int par[N][K];
int he[N];
int id_start[N][K];
int id_end[N][K];
int st[N], nd[N];
int ti;

void dfs(int x, int p){
	id_start[x][0]=st[x];
	id_end[x][0]=nd[x];
	rep(i,1,K-1){
		par[x][i]=par[par[x][i-1]][i-1];
		id_start[x][i]=++ti;
		id_end[x][i]=++ti;
		if(id_start[x][i-1]) adj2[id_start[x][i-1]].pb(id_start[x][i]);
		if(id_start[par[x][i-1]][i-1]) adj2[id_start[par[x][i-1]][i-1]].pb(id_start[x][i]);

		if(id_end[x][i-1]) adj2[id_end[x][i]].pb(id_end[x][i-1]);
		if(id_end[par[x][i-1]][i-1]) adj2[id_end[x][i]].pb(id_end[par[x][i-1]][i-1]);
	}

	for(int k:adj[x]){
		if(k==p) continue;
		he[k]=he[x]+1;
		par[k][0]=x;
		dfs(k, x);
	}
}

int lca(int u, int v){
	if(he[u]<he[v]) swap(u,v);
	int d=he[u]-he[v];
	for(int i=0; i<K; i++){
		if(d&(1<<i)) u=par[u][i];
	}
	if(u==v) return u;
	for(int i=K-1; i>=0; i--){
		if(par[u][i] && par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
	}
	return par[u][0];
}

int dist(int u, int v){ return he[u]+he[v]-2*he[lca(u,v)]; }

void dfs2(int x){
	vis[x]=1;
	seen[x]=1;
	for(int k:adj2[x]){
		if(seen[k]){
			cyc=1; return;
		}
		if(vis[k]) continue;
		dfs2(k);
	}
	seen[x]=0;
}

void add_start(int a, int up, int to){
	int d=he[a]-he[up]+1;
	for(int i=K-1; i>=0; i--){
		if(d&(1<<i)){
			if(id_start[a][i]) adj2[id_start[a][i]].push_back(to);
			a=par[a][i];
		}
	}
}

void add_end(int a, int up, int to){
	// cout << "add end " << a << " - " << up << " TO " << to << nl;
	int d=he[a]-he[up]+1;
	for(int i=K-1; i>=0; i--){
		if(d&(1<<i)){
			if(id_end[a][i]) adj2[to].push_back(id_end[a][i]);
			a=par[a][i];
		}
	}
}

int binlift(int x, int d){
	int old=x;
	rep(i,0,K-1){
		if(d&(1<<i)) x=par[x][i];
	}
	return x;
}

int main(){
  cin.tie(0)->sync_with_stdio(0);

  cin >> t;
  while(t--){
  	cin >> n;
  	rep(i,1,n) adj[i].clear();
  	rep(i,1,n-1){
  		int u,v; cin >> u >> v;
  		adj[u].pb(v); adj[v].pb(u);
  	}
  	
  	cin >> m;
  	vector<pii> qu;
  	rep(i,1,m){
  		int s, e; cin >> s >> e;
  		qu.pb({s, e});
  		st[s]=i;
  		nd[e]=i;
  	}
  	ti=m;
  	dfs(1, 0);

  	// add edges
  	rep(i,1,m){
  		int a=qu[i-1].f, b=qu[i-1].s;
  		int lc=lca(a,b);
  		if(a==lc) add_start(b, binlift(b, he[b]-he[a]-1), i);
  		else add_start(par[a][0],lc, i), add_start(b,lc, i);
  		
  		if(b==lc) add_end(a, binlift(a, he[a]-he[b]-1), i);
  		else add_end(par[b][0],lc, i), add_end(a,lc, i);
  	}

  	cyc=0;
  	rep(i,1,ti){
  		if(cyc) break;
  		if(vis[i]) continue;
  		dfs2(i);
  	}

  	cout << (cyc?"No":"Yes") << nl;

  	rep(i,1,ti){
  		vis[i]=seen[i]=0;
  		adj2[i].clear();
  	}
  	rep(i,1,m){
  		int s=qu[i-1].f, e=qu[i-1].s;
  		st[s]=0;
  		nd[e]=0;
  	}
  }
}
// 

Compilation message

jail.cpp: In function 'int binlift(int, int)':
jail.cpp:99:6: warning: unused variable 'old' [-Wunused-variable]
   99 |  int old=x;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 115904 KB Output is correct
2 Correct 60 ms 115968 KB Output is correct
3 Correct 54 ms 115848 KB Output is correct
4 Correct 85 ms 116144 KB Output is correct
5 Correct 114 ms 116172 KB Output is correct
6 Correct 56 ms 116268 KB Output is correct
7 Correct 58 ms 116200 KB Output is correct
8 Correct 57 ms 116240 KB Output is correct
9 Correct 324 ms 124108 KB Output is correct
10 Correct 1066 ms 274524 KB Output is correct
11 Correct 74 ms 116108 KB Output is correct
12 Correct 154 ms 117068 KB Output is correct
13 Correct 990 ms 283720 KB Output is correct
14 Correct 916 ms 283740 KB Output is correct
15 Correct 1380 ms 304752 KB Output is correct
16 Correct 1832 ms 338040 KB Output is correct
17 Correct 1256 ms 285380 KB Output is correct
18 Correct 1048 ms 291372 KB Output is correct
19 Correct 1206 ms 281800 KB Output is correct
20 Correct 840 ms 281652 KB Output is correct
21 Correct 1316 ms 299828 KB Output is correct
22 Correct 824 ms 281228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 115788 KB Output is correct
2 Correct 53 ms 115868 KB Output is correct
3 Correct 62 ms 116240 KB Output is correct
4 Correct 65 ms 116452 KB Output is correct
5 Correct 59 ms 116204 KB Output is correct
6 Correct 61 ms 116164 KB Output is correct
7 Correct 58 ms 116264 KB Output is correct
8 Correct 60 ms 116188 KB Output is correct
9 Correct 56 ms 116172 KB Output is correct
10 Correct 57 ms 116148 KB Output is correct
11 Correct 59 ms 116256 KB Output is correct
12 Correct 57 ms 116168 KB Output is correct
13 Correct 56 ms 116232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 115788 KB Output is correct
2 Correct 53 ms 115868 KB Output is correct
3 Correct 62 ms 116240 KB Output is correct
4 Correct 65 ms 116452 KB Output is correct
5 Correct 59 ms 116204 KB Output is correct
6 Correct 61 ms 116164 KB Output is correct
7 Correct 58 ms 116264 KB Output is correct
8 Correct 60 ms 116188 KB Output is correct
9 Correct 56 ms 116172 KB Output is correct
10 Correct 57 ms 116148 KB Output is correct
11 Correct 59 ms 116256 KB Output is correct
12 Correct 57 ms 116168 KB Output is correct
13 Correct 56 ms 116232 KB Output is correct
14 Correct 62 ms 115832 KB Output is correct
15 Correct 55 ms 115796 KB Output is correct
16 Correct 64 ms 116264 KB Output is correct
17 Correct 63 ms 116284 KB Output is correct
18 Correct 77 ms 116280 KB Output is correct
19 Correct 56 ms 115916 KB Output is correct
20 Correct 59 ms 116188 KB Output is correct
21 Correct 57 ms 116244 KB Output is correct
22 Correct 57 ms 116200 KB Output is correct
23 Correct 54 ms 115788 KB Output is correct
24 Correct 58 ms 115976 KB Output is correct
25 Correct 60 ms 116132 KB Output is correct
26 Correct 65 ms 116172 KB Output is correct
27 Correct 62 ms 116232 KB Output is correct
28 Correct 53 ms 115916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 115788 KB Output is correct
2 Correct 53 ms 115868 KB Output is correct
3 Correct 62 ms 116240 KB Output is correct
4 Correct 65 ms 116452 KB Output is correct
5 Correct 59 ms 116204 KB Output is correct
6 Correct 61 ms 116164 KB Output is correct
7 Correct 58 ms 116264 KB Output is correct
8 Correct 60 ms 116188 KB Output is correct
9 Correct 56 ms 116172 KB Output is correct
10 Correct 57 ms 116148 KB Output is correct
11 Correct 59 ms 116256 KB Output is correct
12 Correct 57 ms 116168 KB Output is correct
13 Correct 56 ms 116232 KB Output is correct
14 Correct 62 ms 115832 KB Output is correct
15 Correct 55 ms 115796 KB Output is correct
16 Correct 64 ms 116264 KB Output is correct
17 Correct 63 ms 116284 KB Output is correct
18 Correct 77 ms 116280 KB Output is correct
19 Correct 56 ms 115916 KB Output is correct
20 Correct 59 ms 116188 KB Output is correct
21 Correct 57 ms 116244 KB Output is correct
22 Correct 57 ms 116200 KB Output is correct
23 Correct 54 ms 115788 KB Output is correct
24 Correct 58 ms 115976 KB Output is correct
25 Correct 60 ms 116132 KB Output is correct
26 Correct 65 ms 116172 KB Output is correct
27 Correct 62 ms 116232 KB Output is correct
28 Correct 53 ms 115916 KB Output is correct
29 Correct 67 ms 116204 KB Output is correct
30 Correct 59 ms 116360 KB Output is correct
31 Correct 69 ms 116300 KB Output is correct
32 Correct 61 ms 116216 KB Output is correct
33 Correct 64 ms 116224 KB Output is correct
34 Correct 66 ms 116172 KB Output is correct
35 Correct 63 ms 116264 KB Output is correct
36 Correct 60 ms 116344 KB Output is correct
37 Correct 60 ms 116132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 115788 KB Output is correct
2 Correct 53 ms 115868 KB Output is correct
3 Correct 62 ms 116240 KB Output is correct
4 Correct 65 ms 116452 KB Output is correct
5 Correct 59 ms 116204 KB Output is correct
6 Correct 61 ms 116164 KB Output is correct
7 Correct 58 ms 116264 KB Output is correct
8 Correct 60 ms 116188 KB Output is correct
9 Correct 56 ms 116172 KB Output is correct
10 Correct 57 ms 116148 KB Output is correct
11 Correct 59 ms 116256 KB Output is correct
12 Correct 57 ms 116168 KB Output is correct
13 Correct 56 ms 116232 KB Output is correct
14 Correct 62 ms 115832 KB Output is correct
15 Correct 55 ms 115796 KB Output is correct
16 Correct 64 ms 116264 KB Output is correct
17 Correct 63 ms 116284 KB Output is correct
18 Correct 77 ms 116280 KB Output is correct
19 Correct 56 ms 115916 KB Output is correct
20 Correct 59 ms 116188 KB Output is correct
21 Correct 57 ms 116244 KB Output is correct
22 Correct 57 ms 116200 KB Output is correct
23 Correct 54 ms 115788 KB Output is correct
24 Correct 58 ms 115976 KB Output is correct
25 Correct 60 ms 116132 KB Output is correct
26 Correct 65 ms 116172 KB Output is correct
27 Correct 62 ms 116232 KB Output is correct
28 Correct 53 ms 115916 KB Output is correct
29 Correct 67 ms 116204 KB Output is correct
30 Correct 59 ms 116360 KB Output is correct
31 Correct 69 ms 116300 KB Output is correct
32 Correct 61 ms 116216 KB Output is correct
33 Correct 64 ms 116224 KB Output is correct
34 Correct 66 ms 116172 KB Output is correct
35 Correct 63 ms 116264 KB Output is correct
36 Correct 60 ms 116344 KB Output is correct
37 Correct 60 ms 116132 KB Output is correct
38 Correct 386 ms 124200 KB Output is correct
39 Correct 1117 ms 273984 KB Output is correct
40 Correct 242 ms 125644 KB Output is correct
41 Correct 241 ms 125016 KB Output is correct
42 Correct 196 ms 125100 KB Output is correct
43 Correct 262 ms 125196 KB Output is correct
44 Correct 81 ms 117580 KB Output is correct
45 Correct 428 ms 267976 KB Output is correct
46 Correct 492 ms 267936 KB Output is correct
47 Correct 885 ms 270512 KB Output is correct
48 Correct 856 ms 270560 KB Output is correct
49 Correct 414 ms 270776 KB Output is correct
50 Correct 552 ms 270740 KB Output is correct
51 Correct 634 ms 271552 KB Output is correct
52 Correct 566 ms 271652 KB Output is correct
53 Correct 97 ms 126868 KB Output is correct
54 Correct 504 ms 268060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 115880 KB Output is correct
2 Correct 54 ms 115808 KB Output is correct
3 Correct 53 ms 115788 KB Output is correct
4 Correct 54 ms 115808 KB Output is correct
5 Correct 69 ms 116044 KB Output is correct
6 Correct 59 ms 116232 KB Output is correct
7 Correct 64 ms 116116 KB Output is correct
8 Correct 56 ms 115864 KB Output is correct
9 Correct 53 ms 115836 KB Output is correct
10 Correct 55 ms 115996 KB Output is correct
11 Correct 52 ms 115840 KB Output is correct
12 Correct 58 ms 116184 KB Output is correct
13 Correct 110 ms 116208 KB Output is correct
14 Correct 137 ms 116196 KB Output is correct
15 Correct 107 ms 116280 KB Output is correct
16 Correct 388 ms 268412 KB Output is correct
17 Correct 608 ms 277496 KB Output is correct
18 Correct 847 ms 295136 KB Output is correct
19 Correct 528 ms 268756 KB Output is correct
20 Correct 416 ms 268936 KB Output is correct
21 Correct 504 ms 268912 KB Output is correct
22 Correct 604 ms 274540 KB Output is correct
23 Correct 570 ms 273504 KB Output is correct
24 Correct 586 ms 273264 KB Output is correct
25 Correct 565 ms 273180 KB Output is correct
26 Correct 598 ms 273132 KB Output is correct
27 Correct 658 ms 277860 KB Output is correct
28 Correct 640 ms 286260 KB Output is correct
29 Correct 572 ms 280300 KB Output is correct
30 Correct 575 ms 276748 KB Output is correct
31 Correct 531 ms 277820 KB Output is correct
32 Correct 537 ms 273136 KB Output is correct
33 Correct 527 ms 275960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 115904 KB Output is correct
2 Correct 60 ms 115968 KB Output is correct
3 Correct 54 ms 115848 KB Output is correct
4 Correct 85 ms 116144 KB Output is correct
5 Correct 114 ms 116172 KB Output is correct
6 Correct 56 ms 116268 KB Output is correct
7 Correct 58 ms 116200 KB Output is correct
8 Correct 57 ms 116240 KB Output is correct
9 Correct 324 ms 124108 KB Output is correct
10 Correct 1066 ms 274524 KB Output is correct
11 Correct 74 ms 116108 KB Output is correct
12 Correct 154 ms 117068 KB Output is correct
13 Correct 990 ms 283720 KB Output is correct
14 Correct 916 ms 283740 KB Output is correct
15 Correct 1380 ms 304752 KB Output is correct
16 Correct 1832 ms 338040 KB Output is correct
17 Correct 1256 ms 285380 KB Output is correct
18 Correct 1048 ms 291372 KB Output is correct
19 Correct 1206 ms 281800 KB Output is correct
20 Correct 840 ms 281652 KB Output is correct
21 Correct 1316 ms 299828 KB Output is correct
22 Correct 824 ms 281228 KB Output is correct
23 Correct 55 ms 115788 KB Output is correct
24 Correct 53 ms 115868 KB Output is correct
25 Correct 62 ms 116240 KB Output is correct
26 Correct 65 ms 116452 KB Output is correct
27 Correct 59 ms 116204 KB Output is correct
28 Correct 61 ms 116164 KB Output is correct
29 Correct 58 ms 116264 KB Output is correct
30 Correct 60 ms 116188 KB Output is correct
31 Correct 56 ms 116172 KB Output is correct
32 Correct 57 ms 116148 KB Output is correct
33 Correct 59 ms 116256 KB Output is correct
34 Correct 57 ms 116168 KB Output is correct
35 Correct 56 ms 116232 KB Output is correct
36 Correct 62 ms 115832 KB Output is correct
37 Correct 55 ms 115796 KB Output is correct
38 Correct 64 ms 116264 KB Output is correct
39 Correct 63 ms 116284 KB Output is correct
40 Correct 77 ms 116280 KB Output is correct
41 Correct 56 ms 115916 KB Output is correct
42 Correct 59 ms 116188 KB Output is correct
43 Correct 57 ms 116244 KB Output is correct
44 Correct 57 ms 116200 KB Output is correct
45 Correct 54 ms 115788 KB Output is correct
46 Correct 58 ms 115976 KB Output is correct
47 Correct 60 ms 116132 KB Output is correct
48 Correct 65 ms 116172 KB Output is correct
49 Correct 62 ms 116232 KB Output is correct
50 Correct 53 ms 115916 KB Output is correct
51 Correct 67 ms 116204 KB Output is correct
52 Correct 59 ms 116360 KB Output is correct
53 Correct 69 ms 116300 KB Output is correct
54 Correct 61 ms 116216 KB Output is correct
55 Correct 64 ms 116224 KB Output is correct
56 Correct 66 ms 116172 KB Output is correct
57 Correct 63 ms 116264 KB Output is correct
58 Correct 60 ms 116344 KB Output is correct
59 Correct 60 ms 116132 KB Output is correct
60 Correct 386 ms 124200 KB Output is correct
61 Correct 1117 ms 273984 KB Output is correct
62 Correct 242 ms 125644 KB Output is correct
63 Correct 241 ms 125016 KB Output is correct
64 Correct 196 ms 125100 KB Output is correct
65 Correct 262 ms 125196 KB Output is correct
66 Correct 81 ms 117580 KB Output is correct
67 Correct 428 ms 267976 KB Output is correct
68 Correct 492 ms 267936 KB Output is correct
69 Correct 885 ms 270512 KB Output is correct
70 Correct 856 ms 270560 KB Output is correct
71 Correct 414 ms 270776 KB Output is correct
72 Correct 552 ms 270740 KB Output is correct
73 Correct 634 ms 271552 KB Output is correct
74 Correct 566 ms 271652 KB Output is correct
75 Correct 97 ms 126868 KB Output is correct
76 Correct 504 ms 268060 KB Output is correct
77 Correct 54 ms 115880 KB Output is correct
78 Correct 54 ms 115808 KB Output is correct
79 Correct 53 ms 115788 KB Output is correct
80 Correct 54 ms 115808 KB Output is correct
81 Correct 69 ms 116044 KB Output is correct
82 Correct 59 ms 116232 KB Output is correct
83 Correct 64 ms 116116 KB Output is correct
84 Correct 56 ms 115864 KB Output is correct
85 Correct 53 ms 115836 KB Output is correct
86 Correct 55 ms 115996 KB Output is correct
87 Correct 52 ms 115840 KB Output is correct
88 Correct 58 ms 116184 KB Output is correct
89 Correct 110 ms 116208 KB Output is correct
90 Correct 137 ms 116196 KB Output is correct
91 Correct 107 ms 116280 KB Output is correct
92 Correct 388 ms 268412 KB Output is correct
93 Correct 608 ms 277496 KB Output is correct
94 Correct 847 ms 295136 KB Output is correct
95 Correct 528 ms 268756 KB Output is correct
96 Correct 416 ms 268936 KB Output is correct
97 Correct 504 ms 268912 KB Output is correct
98 Correct 604 ms 274540 KB Output is correct
99 Correct 570 ms 273504 KB Output is correct
100 Correct 586 ms 273264 KB Output is correct
101 Correct 565 ms 273180 KB Output is correct
102 Correct 598 ms 273132 KB Output is correct
103 Correct 658 ms 277860 KB Output is correct
104 Correct 640 ms 286260 KB Output is correct
105 Correct 572 ms 280300 KB Output is correct
106 Correct 575 ms 276748 KB Output is correct
107 Correct 531 ms 277820 KB Output is correct
108 Correct 537 ms 273136 KB Output is correct
109 Correct 527 ms 275960 KB Output is correct
110 Correct 131 ms 116708 KB Output is correct
111 Correct 107 ms 116476 KB Output is correct
112 Correct 1307 ms 310500 KB Output is correct
113 Correct 1058 ms 274544 KB Output is correct
114 Correct 1004 ms 300352 KB Output is correct
115 Correct 291 ms 269964 KB Output is correct
116 Correct 642 ms 274260 KB Output is correct
117 Correct 905 ms 301348 KB Output is correct
118 Correct 482 ms 269008 KB Output is correct
119 Correct 413 ms 269000 KB Output is correct
120 Correct 91 ms 129248 KB Output is correct
121 Correct 719 ms 277924 KB Output is correct
122 Correct 623 ms 277604 KB Output is correct
123 Correct 1152 ms 276764 KB Output is correct
124 Correct 976 ms 276484 KB Output is correct
125 Correct 906 ms 277200 KB Output is correct
126 Correct 1695 ms 341104 KB Output is correct
127 Correct 1225 ms 308728 KB Output is correct
128 Correct 1067 ms 296584 KB Output is correct
129 Correct 1236 ms 294952 KB Output is correct
130 Correct 1183 ms 303348 KB Output is correct