Submission #680782

# Submission time Handle Problem Language Result Execution time Memory
680782 2023-01-11T19:05:48 Z arnold518 Jail (JOI22_jail) C++17
100 / 100
1707 ms 224816 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;

int Q, N, NN, M;
vector<int> adj[MAXN+10];
pii A[MAXN+10];
int idx[MAXN+10];

vector<int> adj2[MAXN*10+10];
vector<int> radj2[MAXN*10+10];

int dep[MAXN+10], par[MAXN+10], sz[MAXN+10];
void dfs(int now, int bef, int d)
{
	dep[now]=d;
	par[now]=bef;
	sz[now]=1;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now, d+1);
		sz[now]+=sz[nxt];
	}
}

void addEdge(int u, int v)
{
	//printf("%d %d\n", u, v);
	adj2[u].push_back(v);
	radj2[v].push_back(u);
}

void init(int node, int tl, int tr)
{
	if(tl==tr)
	{
		idx[tl]=node;
		return;
	}
	int mid=tl+tr>>1;
	init(node*2, tl, mid);
	init(node*2+1, mid+1, tr);

	addEdge(M+node*2, M+node);
	addEdge(M+node*2+1, M+node);
	addEdge(M+N*4+node, M+N*4+node*2);
	addEdge(M+N*4+node, M+N*4+node*2+1);
}

void get(int node, int tl, int tr, int l, int r, vector<int> &V)
{
	if(r<tl || tr<l) return;
	if(l<=tl && tr<=r)
	{
		V.push_back(node);
		return;
	}
	int mid=tl+tr>>1;
	get(node*2, tl, mid, l, r, V);
	get(node*2+1, mid+1, tr, l, r, V);
}

int dfn[MAXN+10], head[MAXN+10], dcnt;
void hld(int now, int bef)
{
	dfn[now]=++dcnt;

	int heavy=0;
	for(int nxt : adj[now]) if(nxt!=bef && sz[heavy]<sz[nxt]) heavy=nxt;
	if(!heavy) return;

	head[heavy]=head[now];
	hld(heavy, now);

	for(int nxt : adj[now]) if(nxt!=bef && nxt!=heavy)
	{
		head[nxt]=nxt;
		hld(nxt, now);
	}
}

int vis[MAXN*10+10];
vector<int> S;
void dfs2(int now)
{
	vis[now]=1;
	for(int nxt : adj2[now]) if(!vis[nxt]) dfs2(nxt);
	S.push_back(now);
}

int scc[MAXN*10+10], scnt;
void dfs3(int now)
{
	scc[now]=scnt;
	for(int nxt : radj2[now]) if(!scc[nxt]) dfs3(nxt);
}

bool solve()
{
	scanf("%d", &N);
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	scanf("%d", &M);
	for(int i=1; i<=M; i++)
	{
		scanf("%d%d", &A[i].first, &A[i].second);
	}

	NN=N*8+M;

	init(1, 1, N);
	dfs(1, 0, 1);

	head[1]=1;
	hld(1, 0);

	for(int i=1; i<=M; i++)
	{
		addEdge(i, idx[dfn[A[i].first]]+M);
		addEdge(idx[dfn[A[i].second]]+M+N*4, i);
	}

	for(int i=1; i<=M; i++)
	{
		int u=A[i].first, v=A[i].second;
		while(head[u]!=head[v])
		{
			if(dep[head[u]]>dep[head[v]]) swap(u, v);

			vector<int> V;
			get(1, 1, N, dfn[head[v]], dfn[v], V);
			for(auto it : V)
			{
				addEdge(it+M, i);
				addEdge(i, it+M+N*4);
			}
			v=par[head[v]];
		}
		if(dep[u]>dep[v]) swap(u, v);
		vector<int> V;
		get(1, 1, N, dfn[u], dfn[v], V);
		for(auto it : V)
		{
			addEdge(it+M, i);
			addEdge(i, it+M+N*4);
		}
	}

	for(int i=1; i<=NN; i++) if(!vis[i]) dfs2(i);
	reverse(S.begin(), S.end());
	for(auto it : S) if(!scc[it]) scnt++, dfs3(it);

	set<int> SS;
	for(int i=1; i<=M; i++) SS.insert(scc[i]);
	return SS.size()==M;
}

void clear()
{
	for(int i=1; i<=N; i++)
	{
		adj[i].clear();
	}
	for(int i=1; i<=NN; i++)
	{
		adj2[i].clear();
		radj2[i].clear();
		vis[i]=0;
		scc[i]=0;
	}
	S.clear();
	dcnt=0;
	scnt=0;
}

int main()
{
	scanf("%d", &Q);
	while(Q--)
	{
		if(solve()) printf("Yes\n");
		else printf("No\n");
		clear();
	}
}

Compilation message

jail.cpp: In function 'void init(int, int, int)':
jail.cpp:46:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |  int mid=tl+tr>>1;
      |          ~~^~~
jail.cpp: In function 'void get(int, int, int, int, int, std::vector<int>&)':
jail.cpp:64:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |  int mid=tl+tr>>1;
      |          ~~^~~
jail.cpp: In function 'bool solve()':
jail.cpp:166:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  166 |  return SS.size()==M;
      |         ~~~~~~~~~^~~
jail.cpp:106:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
jail.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
jail.cpp:114:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |  scanf("%d", &M);
      |  ~~~~~^~~~~~~~~~
jail.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   scanf("%d%d", &A[i].first, &A[i].second);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp: In function 'int main()':
jail.cpp:189:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  189 |  scanf("%d", &Q);
      |  ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 45 ms 98900 KB Output is correct
2 Correct 46 ms 98952 KB Output is correct
3 Correct 49 ms 98968 KB Output is correct
4 Correct 72 ms 99024 KB Output is correct
5 Correct 92 ms 99016 KB Output is correct
6 Correct 46 ms 99004 KB Output is correct
7 Correct 47 ms 99060 KB Output is correct
8 Correct 51 ms 99136 KB Output is correct
9 Correct 113 ms 101968 KB Output is correct
10 Correct 204 ms 149204 KB Output is correct
11 Correct 56 ms 98984 KB Output is correct
12 Correct 114 ms 99116 KB Output is correct
13 Correct 325 ms 164452 KB Output is correct
14 Correct 362 ms 162460 KB Output is correct
15 Correct 704 ms 172896 KB Output is correct
16 Correct 1143 ms 213760 KB Output is correct
17 Correct 461 ms 180908 KB Output is correct
18 Correct 376 ms 171712 KB Output is correct
19 Correct 445 ms 177096 KB Output is correct
20 Correct 469 ms 177148 KB Output is correct
21 Correct 493 ms 177804 KB Output is correct
22 Correct 265 ms 157596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 98892 KB Output is correct
2 Correct 52 ms 98884 KB Output is correct
3 Correct 51 ms 98992 KB Output is correct
4 Correct 48 ms 99016 KB Output is correct
5 Correct 55 ms 99016 KB Output is correct
6 Correct 47 ms 99024 KB Output is correct
7 Correct 54 ms 99020 KB Output is correct
8 Correct 48 ms 99052 KB Output is correct
9 Correct 54 ms 99004 KB Output is correct
10 Correct 49 ms 99048 KB Output is correct
11 Correct 57 ms 99000 KB Output is correct
12 Correct 54 ms 98968 KB Output is correct
13 Correct 46 ms 99092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 98892 KB Output is correct
2 Correct 52 ms 98884 KB Output is correct
3 Correct 51 ms 98992 KB Output is correct
4 Correct 48 ms 99016 KB Output is correct
5 Correct 55 ms 99016 KB Output is correct
6 Correct 47 ms 99024 KB Output is correct
7 Correct 54 ms 99020 KB Output is correct
8 Correct 48 ms 99052 KB Output is correct
9 Correct 54 ms 99004 KB Output is correct
10 Correct 49 ms 99048 KB Output is correct
11 Correct 57 ms 99000 KB Output is correct
12 Correct 54 ms 98968 KB Output is correct
13 Correct 46 ms 99092 KB Output is correct
14 Correct 50 ms 98884 KB Output is correct
15 Correct 46 ms 98968 KB Output is correct
16 Correct 54 ms 99120 KB Output is correct
17 Correct 50 ms 99072 KB Output is correct
18 Correct 51 ms 99056 KB Output is correct
19 Correct 46 ms 98900 KB Output is correct
20 Correct 49 ms 99112 KB Output is correct
21 Correct 56 ms 99080 KB Output is correct
22 Correct 48 ms 99004 KB Output is correct
23 Correct 47 ms 98956 KB Output is correct
24 Correct 47 ms 99000 KB Output is correct
25 Correct 48 ms 99064 KB Output is correct
26 Correct 52 ms 99088 KB Output is correct
27 Correct 49 ms 99100 KB Output is correct
28 Correct 52 ms 98988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 98892 KB Output is correct
2 Correct 52 ms 98884 KB Output is correct
3 Correct 51 ms 98992 KB Output is correct
4 Correct 48 ms 99016 KB Output is correct
5 Correct 55 ms 99016 KB Output is correct
6 Correct 47 ms 99024 KB Output is correct
7 Correct 54 ms 99020 KB Output is correct
8 Correct 48 ms 99052 KB Output is correct
9 Correct 54 ms 99004 KB Output is correct
10 Correct 49 ms 99048 KB Output is correct
11 Correct 57 ms 99000 KB Output is correct
12 Correct 54 ms 98968 KB Output is correct
13 Correct 46 ms 99092 KB Output is correct
14 Correct 50 ms 98884 KB Output is correct
15 Correct 46 ms 98968 KB Output is correct
16 Correct 54 ms 99120 KB Output is correct
17 Correct 50 ms 99072 KB Output is correct
18 Correct 51 ms 99056 KB Output is correct
19 Correct 46 ms 98900 KB Output is correct
20 Correct 49 ms 99112 KB Output is correct
21 Correct 56 ms 99080 KB Output is correct
22 Correct 48 ms 99004 KB Output is correct
23 Correct 47 ms 98956 KB Output is correct
24 Correct 47 ms 99000 KB Output is correct
25 Correct 48 ms 99064 KB Output is correct
26 Correct 52 ms 99088 KB Output is correct
27 Correct 49 ms 99100 KB Output is correct
28 Correct 52 ms 98988 KB Output is correct
29 Correct 50 ms 99120 KB Output is correct
30 Correct 50 ms 99088 KB Output is correct
31 Correct 50 ms 99168 KB Output is correct
32 Correct 52 ms 99020 KB Output is correct
33 Correct 49 ms 99028 KB Output is correct
34 Correct 50 ms 99088 KB Output is correct
35 Correct 51 ms 99188 KB Output is correct
36 Correct 50 ms 99076 KB Output is correct
37 Correct 48 ms 99032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 98892 KB Output is correct
2 Correct 52 ms 98884 KB Output is correct
3 Correct 51 ms 98992 KB Output is correct
4 Correct 48 ms 99016 KB Output is correct
5 Correct 55 ms 99016 KB Output is correct
6 Correct 47 ms 99024 KB Output is correct
7 Correct 54 ms 99020 KB Output is correct
8 Correct 48 ms 99052 KB Output is correct
9 Correct 54 ms 99004 KB Output is correct
10 Correct 49 ms 99048 KB Output is correct
11 Correct 57 ms 99000 KB Output is correct
12 Correct 54 ms 98968 KB Output is correct
13 Correct 46 ms 99092 KB Output is correct
14 Correct 50 ms 98884 KB Output is correct
15 Correct 46 ms 98968 KB Output is correct
16 Correct 54 ms 99120 KB Output is correct
17 Correct 50 ms 99072 KB Output is correct
18 Correct 51 ms 99056 KB Output is correct
19 Correct 46 ms 98900 KB Output is correct
20 Correct 49 ms 99112 KB Output is correct
21 Correct 56 ms 99080 KB Output is correct
22 Correct 48 ms 99004 KB Output is correct
23 Correct 47 ms 98956 KB Output is correct
24 Correct 47 ms 99000 KB Output is correct
25 Correct 48 ms 99064 KB Output is correct
26 Correct 52 ms 99088 KB Output is correct
27 Correct 49 ms 99100 KB Output is correct
28 Correct 52 ms 98988 KB Output is correct
29 Correct 50 ms 99120 KB Output is correct
30 Correct 50 ms 99088 KB Output is correct
31 Correct 50 ms 99168 KB Output is correct
32 Correct 52 ms 99020 KB Output is correct
33 Correct 49 ms 99028 KB Output is correct
34 Correct 50 ms 99088 KB Output is correct
35 Correct 51 ms 99188 KB Output is correct
36 Correct 50 ms 99076 KB Output is correct
37 Correct 48 ms 99032 KB Output is correct
38 Correct 112 ms 102412 KB Output is correct
39 Correct 211 ms 149644 KB Output is correct
40 Correct 134 ms 102428 KB Output is correct
41 Correct 150 ms 102480 KB Output is correct
42 Correct 136 ms 102552 KB Output is correct
43 Correct 114 ms 101748 KB Output is correct
44 Correct 72 ms 100000 KB Output is correct
45 Correct 228 ms 140404 KB Output is correct
46 Correct 224 ms 140292 KB Output is correct
47 Correct 209 ms 144868 KB Output is correct
48 Correct 221 ms 144840 KB Output is correct
49 Correct 223 ms 140512 KB Output is correct
50 Correct 252 ms 140484 KB Output is correct
51 Correct 196 ms 141632 KB Output is correct
52 Correct 225 ms 141636 KB Output is correct
53 Correct 74 ms 102680 KB Output is correct
54 Correct 240 ms 140176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 98892 KB Output is correct
2 Correct 54 ms 98916 KB Output is correct
3 Correct 61 ms 98968 KB Output is correct
4 Correct 48 ms 98940 KB Output is correct
5 Correct 57 ms 98976 KB Output is correct
6 Correct 48 ms 99032 KB Output is correct
7 Correct 46 ms 99056 KB Output is correct
8 Correct 51 ms 99020 KB Output is correct
9 Correct 46 ms 98900 KB Output is correct
10 Correct 55 ms 99020 KB Output is correct
11 Correct 53 ms 98928 KB Output is correct
12 Correct 55 ms 99072 KB Output is correct
13 Correct 97 ms 99584 KB Output is correct
14 Correct 121 ms 99592 KB Output is correct
15 Correct 128 ms 99512 KB Output is correct
16 Correct 333 ms 144540 KB Output is correct
17 Correct 800 ms 167904 KB Output is correct
18 Correct 1285 ms 201516 KB Output is correct
19 Correct 452 ms 149556 KB Output is correct
20 Correct 444 ms 149444 KB Output is correct
21 Correct 499 ms 149556 KB Output is correct
22 Correct 717 ms 165580 KB Output is correct
23 Correct 586 ms 164900 KB Output is correct
24 Correct 574 ms 167536 KB Output is correct
25 Correct 609 ms 167448 KB Output is correct
26 Correct 593 ms 167972 KB Output is correct
27 Correct 669 ms 167156 KB Output is correct
28 Correct 690 ms 170020 KB Output is correct
29 Correct 745 ms 164140 KB Output is correct
30 Correct 531 ms 160492 KB Output is correct
31 Correct 511 ms 159100 KB Output is correct
32 Correct 501 ms 158612 KB Output is correct
33 Correct 479 ms 159284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 98900 KB Output is correct
2 Correct 46 ms 98952 KB Output is correct
3 Correct 49 ms 98968 KB Output is correct
4 Correct 72 ms 99024 KB Output is correct
5 Correct 92 ms 99016 KB Output is correct
6 Correct 46 ms 99004 KB Output is correct
7 Correct 47 ms 99060 KB Output is correct
8 Correct 51 ms 99136 KB Output is correct
9 Correct 113 ms 101968 KB Output is correct
10 Correct 204 ms 149204 KB Output is correct
11 Correct 56 ms 98984 KB Output is correct
12 Correct 114 ms 99116 KB Output is correct
13 Correct 325 ms 164452 KB Output is correct
14 Correct 362 ms 162460 KB Output is correct
15 Correct 704 ms 172896 KB Output is correct
16 Correct 1143 ms 213760 KB Output is correct
17 Correct 461 ms 180908 KB Output is correct
18 Correct 376 ms 171712 KB Output is correct
19 Correct 445 ms 177096 KB Output is correct
20 Correct 469 ms 177148 KB Output is correct
21 Correct 493 ms 177804 KB Output is correct
22 Correct 265 ms 157596 KB Output is correct
23 Correct 52 ms 98892 KB Output is correct
24 Correct 52 ms 98884 KB Output is correct
25 Correct 51 ms 98992 KB Output is correct
26 Correct 48 ms 99016 KB Output is correct
27 Correct 55 ms 99016 KB Output is correct
28 Correct 47 ms 99024 KB Output is correct
29 Correct 54 ms 99020 KB Output is correct
30 Correct 48 ms 99052 KB Output is correct
31 Correct 54 ms 99004 KB Output is correct
32 Correct 49 ms 99048 KB Output is correct
33 Correct 57 ms 99000 KB Output is correct
34 Correct 54 ms 98968 KB Output is correct
35 Correct 46 ms 99092 KB Output is correct
36 Correct 50 ms 98884 KB Output is correct
37 Correct 46 ms 98968 KB Output is correct
38 Correct 54 ms 99120 KB Output is correct
39 Correct 50 ms 99072 KB Output is correct
40 Correct 51 ms 99056 KB Output is correct
41 Correct 46 ms 98900 KB Output is correct
42 Correct 49 ms 99112 KB Output is correct
43 Correct 56 ms 99080 KB Output is correct
44 Correct 48 ms 99004 KB Output is correct
45 Correct 47 ms 98956 KB Output is correct
46 Correct 47 ms 99000 KB Output is correct
47 Correct 48 ms 99064 KB Output is correct
48 Correct 52 ms 99088 KB Output is correct
49 Correct 49 ms 99100 KB Output is correct
50 Correct 52 ms 98988 KB Output is correct
51 Correct 50 ms 99120 KB Output is correct
52 Correct 50 ms 99088 KB Output is correct
53 Correct 50 ms 99168 KB Output is correct
54 Correct 52 ms 99020 KB Output is correct
55 Correct 49 ms 99028 KB Output is correct
56 Correct 50 ms 99088 KB Output is correct
57 Correct 51 ms 99188 KB Output is correct
58 Correct 50 ms 99076 KB Output is correct
59 Correct 48 ms 99032 KB Output is correct
60 Correct 112 ms 102412 KB Output is correct
61 Correct 211 ms 149644 KB Output is correct
62 Correct 134 ms 102428 KB Output is correct
63 Correct 150 ms 102480 KB Output is correct
64 Correct 136 ms 102552 KB Output is correct
65 Correct 114 ms 101748 KB Output is correct
66 Correct 72 ms 100000 KB Output is correct
67 Correct 228 ms 140404 KB Output is correct
68 Correct 224 ms 140292 KB Output is correct
69 Correct 209 ms 144868 KB Output is correct
70 Correct 221 ms 144840 KB Output is correct
71 Correct 223 ms 140512 KB Output is correct
72 Correct 252 ms 140484 KB Output is correct
73 Correct 196 ms 141632 KB Output is correct
74 Correct 225 ms 141636 KB Output is correct
75 Correct 74 ms 102680 KB Output is correct
76 Correct 240 ms 140176 KB Output is correct
77 Correct 45 ms 98892 KB Output is correct
78 Correct 54 ms 98916 KB Output is correct
79 Correct 61 ms 98968 KB Output is correct
80 Correct 48 ms 98940 KB Output is correct
81 Correct 57 ms 98976 KB Output is correct
82 Correct 48 ms 99032 KB Output is correct
83 Correct 46 ms 99056 KB Output is correct
84 Correct 51 ms 99020 KB Output is correct
85 Correct 46 ms 98900 KB Output is correct
86 Correct 55 ms 99020 KB Output is correct
87 Correct 53 ms 98928 KB Output is correct
88 Correct 55 ms 99072 KB Output is correct
89 Correct 97 ms 99584 KB Output is correct
90 Correct 121 ms 99592 KB Output is correct
91 Correct 128 ms 99512 KB Output is correct
92 Correct 333 ms 144540 KB Output is correct
93 Correct 800 ms 167904 KB Output is correct
94 Correct 1285 ms 201516 KB Output is correct
95 Correct 452 ms 149556 KB Output is correct
96 Correct 444 ms 149444 KB Output is correct
97 Correct 499 ms 149556 KB Output is correct
98 Correct 717 ms 165580 KB Output is correct
99 Correct 586 ms 164900 KB Output is correct
100 Correct 574 ms 167536 KB Output is correct
101 Correct 609 ms 167448 KB Output is correct
102 Correct 593 ms 167972 KB Output is correct
103 Correct 669 ms 167156 KB Output is correct
104 Correct 690 ms 170020 KB Output is correct
105 Correct 745 ms 164140 KB Output is correct
106 Correct 531 ms 160492 KB Output is correct
107 Correct 511 ms 159100 KB Output is correct
108 Correct 501 ms 158612 KB Output is correct
109 Correct 479 ms 159284 KB Output is correct
110 Correct 146 ms 100372 KB Output is correct
111 Correct 91 ms 99664 KB Output is correct
112 Correct 731 ms 178388 KB Output is correct
113 Correct 425 ms 156716 KB Output is correct
114 Correct 643 ms 166308 KB Output is correct
115 Correct 261 ms 141756 KB Output is correct
116 Correct 450 ms 153840 KB Output is correct
117 Correct 1707 ms 224816 KB Output is correct
118 Correct 262 ms 141584 KB Output is correct
119 Correct 244 ms 141448 KB Output is correct
120 Correct 81 ms 104032 KB Output is correct
121 Correct 621 ms 160200 KB Output is correct
122 Correct 607 ms 160256 KB Output is correct
123 Correct 464 ms 160208 KB Output is correct
124 Correct 461 ms 159208 KB Output is correct
125 Correct 463 ms 162000 KB Output is correct
126 Correct 1205 ms 215364 KB Output is correct
127 Correct 607 ms 185496 KB Output is correct
128 Correct 501 ms 188004 KB Output is correct
129 Correct 510 ms 188224 KB Output is correct
130 Correct 580 ms 187104 KB Output is correct