답안 #580635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
580635 2022-06-21T14:47:37 Z AriaH Jail (JOI22_jail) C++17
100 / 100
1809 ms 201900 KB
/* Ignore Others Only Focus On Yourself! */
/* Im the Best! */
 
#pragma GCC optimize("O3")
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
 
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
 
const int N = 2e5 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
 
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
 
mt19937 rng(time(nullptr));
 
int n, m, ts, ptr, St[N], Fi[N], node[N], sub[N], H[N], P[N], Hd[N], Hv[N], S[N], T[N], Start[N], End[N];

vector < int > G[N], adj[N << 3], adt[N << 3];

int mark[N << 3];

void add(int v, int u, int tp)
{
	if(!v || !u) return;
	if(tp) swap(v, u);
	adj[v].push_back(u);
	adt[u].push_back(v);
}

struct segment
{
	int tp, seg[N << 2]; /// tp = 0 -> End, tp = 1 -> Start
	segment(int _tp)
	{
		tp = _tp;
	}
	void build(int v = 1, int tl = 1, int tr = n)
	{
		seg[v] = ++ts;
		if(tl == tr) /// you can optimize this part with just puting the node in the last layer of segment
		{
			int now = node[tl];
			if(tp == 0 && End[now])
			{
				add(seg[v], End[now], tp);
			}
			if(tp == 1 && Start[now])
			{
				add(seg[v], Start[now], tp);
			}
			return;
		}
		int mid = (tl + tr) >> 1;
		build(v << 1, tl, mid), build(v << 1 | 1, mid + 1, tr);

		add(seg[v], seg[v << 1], tp);
		add(seg[v], seg[v << 1 | 1], tp);
	}
	void upd(int l, int r, int x, int v = 1, int tl = 1, int tr = n)
	{
		if(l > tr || r < tl || l > r) return;
		if(l <= tl && tr <= r)
		{
			add(x, seg[v], tp);
			return;
		}
		int mid = (tl + tr) >> 1;
		upd(l, r, x, v << 1, tl, mid);
		upd(l, r, x, v << 1 | 1, mid + 1, tr);
	}
};

void dfs(int v)
{
	Hv[v] = 0;
	sub[v] = 1;
	H[v] = H[P[v]] + 1;
	for(auto u : G[v])
	{
		if(u == P[v]) continue;
		P[u] = v;
		dfs(u);
		if(sub[Hv[v]] < sub[u])
		{
			Hv[v] = u;
		}
		sub[v] += sub[u];
	}
}
 
void Hld(int v)
{
	St[v] = ++ptr;
	node[ptr] = v;
	if(Hv[v])
	{
		Hd[Hv[v]] = Hd[v];
		Hld(Hv[v]);
	}
	for(auto u : G[v])
	{
		if(u == P[v] || u == Hv[v]) continue;
		Hd[u] = u;
		Hld(u);
	}
	Fi[v] = ptr;
}

int cnt;

vector < int > top;

void DFS(int v)
{
	mark[v] = 1;
	for(auto u : adj[v])
	{
		if(mark[u])
		{
			continue;
		}
		DFS(u);
	}
	top.push_back(v);
}

void SFD(int v)
{
	mark[v] = 1;
	if(v <= m) cnt ++;
	for(auto u : adt[v])
	{
		if(mark[u]) continue;
		SFD(u);
	}
}

void solve()
{
	cin >> n;
	for(int i = 1; i < n; i ++)
	{
		int a, b;
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs(1);
	Hld(Hd[1] = 1);
	cin >> m;
	ts = m;
	for(int i = 1; i <= m; i ++)
	{
		cin >> S[i] >> T[i];
		Start[S[i]] = i;
		End[T[i]] = i;
	}
	segment S0(0), S1(1);
	S0.build();
	S1.build();
	for(int i = 1; i <= m; i ++)
	{
		int v = S[i], u = T[i];
		while(Hd[v] != Hd[u])
		{
			if(H[Hd[v]] < H[Hd[u]]) swap(u, v);
			S0.upd(St[Hd[v]], St[v], i);
			S1.upd(St[Hd[v]], St[v], i);
			v = P[Hd[v]];
		}
		if(H[v] < H[u]) swap(u, v);
		S0.upd(St[u], St[v], i);
		S1.upd(St[u], St[v], i);
	}
	for(int i = 1; i <= m; i ++)
	{
		if(!mark[i])
		{
			DFS(i);
		}
	}
	for(int i = 0; i <= ts; i ++) mark[i] = 0;
	reverse(all(top));
	int ans = 1;
	for(auto v : top)
	{
		if(!mark[v])
		{
			cnt = 0;
			SFD(v);
			if(cnt > 1)
			{
				ans = 0;
				break;
			}
		}
	}
	if(!ans)
	{
		cout << "No\n";
	}
	else
	{
		cout << "Yes\n";
	}
	for(int i = 0; i <= ts; i ++) adj[i].clear(), adt[i].clear(), mark[i] = 0;
	for(int i = 0; i <= n; i ++) Start[i] = 0, End[i] = 0, G[i].clear();
	top.clear();
	cnt = 0;
	ts = 0;
	ptr = 0;
}
 
int main()
{
	fast_io;
	int q = 1;
	cin >> q;
	while(q --)
	{
		solve();
	}
}

/* check corner case(n = 1?), watch for negetive index or overflow */
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 86384 KB Output is correct
2 Correct 45 ms 86428 KB Output is correct
3 Correct 47 ms 86488 KB Output is correct
4 Correct 72 ms 86792 KB Output is correct
5 Correct 106 ms 86900 KB Output is correct
6 Correct 46 ms 86580 KB Output is correct
7 Correct 46 ms 86644 KB Output is correct
8 Correct 49 ms 86660 KB Output is correct
9 Correct 104 ms 89484 KB Output is correct
10 Correct 157 ms 130476 KB Output is correct
11 Correct 84 ms 86644 KB Output is correct
12 Correct 141 ms 86996 KB Output is correct
13 Correct 279 ms 144860 KB Output is correct
14 Correct 314 ms 145368 KB Output is correct
15 Correct 641 ms 155296 KB Output is correct
16 Correct 1168 ms 193704 KB Output is correct
17 Correct 371 ms 163408 KB Output is correct
18 Correct 290 ms 152264 KB Output is correct
19 Correct 413 ms 159700 KB Output is correct
20 Correct 368 ms 159664 KB Output is correct
21 Correct 419 ms 161544 KB Output is correct
22 Correct 250 ms 141268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 86400 KB Output is correct
2 Correct 44 ms 86388 KB Output is correct
3 Correct 49 ms 86496 KB Output is correct
4 Correct 54 ms 86532 KB Output is correct
5 Correct 50 ms 86584 KB Output is correct
6 Correct 54 ms 86584 KB Output is correct
7 Correct 49 ms 86608 KB Output is correct
8 Correct 48 ms 86496 KB Output is correct
9 Correct 49 ms 86588 KB Output is correct
10 Correct 54 ms 86492 KB Output is correct
11 Correct 49 ms 86612 KB Output is correct
12 Correct 47 ms 86556 KB Output is correct
13 Correct 47 ms 86540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 86400 KB Output is correct
2 Correct 44 ms 86388 KB Output is correct
3 Correct 49 ms 86496 KB Output is correct
4 Correct 54 ms 86532 KB Output is correct
5 Correct 50 ms 86584 KB Output is correct
6 Correct 54 ms 86584 KB Output is correct
7 Correct 49 ms 86608 KB Output is correct
8 Correct 48 ms 86496 KB Output is correct
9 Correct 49 ms 86588 KB Output is correct
10 Correct 54 ms 86492 KB Output is correct
11 Correct 49 ms 86612 KB Output is correct
12 Correct 47 ms 86556 KB Output is correct
13 Correct 47 ms 86540 KB Output is correct
14 Correct 47 ms 86584 KB Output is correct
15 Correct 44 ms 86484 KB Output is correct
16 Correct 52 ms 86604 KB Output is correct
17 Correct 60 ms 86600 KB Output is correct
18 Correct 48 ms 86612 KB Output is correct
19 Correct 45 ms 86444 KB Output is correct
20 Correct 47 ms 86568 KB Output is correct
21 Correct 47 ms 86520 KB Output is correct
22 Correct 53 ms 86580 KB Output is correct
23 Correct 50 ms 86484 KB Output is correct
24 Correct 47 ms 86444 KB Output is correct
25 Correct 46 ms 86604 KB Output is correct
26 Correct 46 ms 86584 KB Output is correct
27 Correct 48 ms 86496 KB Output is correct
28 Correct 48 ms 86492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 86400 KB Output is correct
2 Correct 44 ms 86388 KB Output is correct
3 Correct 49 ms 86496 KB Output is correct
4 Correct 54 ms 86532 KB Output is correct
5 Correct 50 ms 86584 KB Output is correct
6 Correct 54 ms 86584 KB Output is correct
7 Correct 49 ms 86608 KB Output is correct
8 Correct 48 ms 86496 KB Output is correct
9 Correct 49 ms 86588 KB Output is correct
10 Correct 54 ms 86492 KB Output is correct
11 Correct 49 ms 86612 KB Output is correct
12 Correct 47 ms 86556 KB Output is correct
13 Correct 47 ms 86540 KB Output is correct
14 Correct 47 ms 86584 KB Output is correct
15 Correct 44 ms 86484 KB Output is correct
16 Correct 52 ms 86604 KB Output is correct
17 Correct 60 ms 86600 KB Output is correct
18 Correct 48 ms 86612 KB Output is correct
19 Correct 45 ms 86444 KB Output is correct
20 Correct 47 ms 86568 KB Output is correct
21 Correct 47 ms 86520 KB Output is correct
22 Correct 53 ms 86580 KB Output is correct
23 Correct 50 ms 86484 KB Output is correct
24 Correct 47 ms 86444 KB Output is correct
25 Correct 46 ms 86604 KB Output is correct
26 Correct 46 ms 86584 KB Output is correct
27 Correct 48 ms 86496 KB Output is correct
28 Correct 48 ms 86492 KB Output is correct
29 Correct 52 ms 86660 KB Output is correct
30 Correct 49 ms 86672 KB Output is correct
31 Correct 50 ms 86636 KB Output is correct
32 Correct 49 ms 86608 KB Output is correct
33 Correct 47 ms 86604 KB Output is correct
34 Correct 51 ms 86668 KB Output is correct
35 Correct 51 ms 86604 KB Output is correct
36 Correct 56 ms 86640 KB Output is correct
37 Correct 51 ms 86624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 86400 KB Output is correct
2 Correct 44 ms 86388 KB Output is correct
3 Correct 49 ms 86496 KB Output is correct
4 Correct 54 ms 86532 KB Output is correct
5 Correct 50 ms 86584 KB Output is correct
6 Correct 54 ms 86584 KB Output is correct
7 Correct 49 ms 86608 KB Output is correct
8 Correct 48 ms 86496 KB Output is correct
9 Correct 49 ms 86588 KB Output is correct
10 Correct 54 ms 86492 KB Output is correct
11 Correct 49 ms 86612 KB Output is correct
12 Correct 47 ms 86556 KB Output is correct
13 Correct 47 ms 86540 KB Output is correct
14 Correct 47 ms 86584 KB Output is correct
15 Correct 44 ms 86484 KB Output is correct
16 Correct 52 ms 86604 KB Output is correct
17 Correct 60 ms 86600 KB Output is correct
18 Correct 48 ms 86612 KB Output is correct
19 Correct 45 ms 86444 KB Output is correct
20 Correct 47 ms 86568 KB Output is correct
21 Correct 47 ms 86520 KB Output is correct
22 Correct 53 ms 86580 KB Output is correct
23 Correct 50 ms 86484 KB Output is correct
24 Correct 47 ms 86444 KB Output is correct
25 Correct 46 ms 86604 KB Output is correct
26 Correct 46 ms 86584 KB Output is correct
27 Correct 48 ms 86496 KB Output is correct
28 Correct 48 ms 86492 KB Output is correct
29 Correct 52 ms 86660 KB Output is correct
30 Correct 49 ms 86672 KB Output is correct
31 Correct 50 ms 86636 KB Output is correct
32 Correct 49 ms 86608 KB Output is correct
33 Correct 47 ms 86604 KB Output is correct
34 Correct 51 ms 86668 KB Output is correct
35 Correct 51 ms 86604 KB Output is correct
36 Correct 56 ms 86640 KB Output is correct
37 Correct 51 ms 86624 KB Output is correct
38 Correct 105 ms 89396 KB Output is correct
39 Correct 153 ms 130384 KB Output is correct
40 Correct 117 ms 89452 KB Output is correct
41 Correct 149 ms 89500 KB Output is correct
42 Correct 102 ms 89612 KB Output is correct
43 Correct 84 ms 88636 KB Output is correct
44 Correct 70 ms 87452 KB Output is correct
45 Correct 187 ms 120232 KB Output is correct
46 Correct 175 ms 120268 KB Output is correct
47 Correct 155 ms 125248 KB Output is correct
48 Correct 164 ms 125236 KB Output is correct
49 Correct 171 ms 120440 KB Output is correct
50 Correct 171 ms 120568 KB Output is correct
51 Correct 160 ms 121376 KB Output is correct
52 Correct 161 ms 121380 KB Output is correct
53 Correct 70 ms 89316 KB Output is correct
54 Correct 200 ms 120104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 86476 KB Output is correct
2 Correct 53 ms 86448 KB Output is correct
3 Correct 47 ms 86412 KB Output is correct
4 Correct 44 ms 86488 KB Output is correct
5 Correct 88 ms 86652 KB Output is correct
6 Correct 46 ms 86520 KB Output is correct
7 Correct 48 ms 86584 KB Output is correct
8 Correct 47 ms 86460 KB Output is correct
9 Correct 48 ms 86400 KB Output is correct
10 Correct 48 ms 86484 KB Output is correct
11 Correct 45 ms 86504 KB Output is correct
12 Correct 50 ms 86628 KB Output is correct
13 Correct 121 ms 86904 KB Output is correct
14 Correct 153 ms 87036 KB Output is correct
15 Correct 128 ms 86896 KB Output is correct
16 Correct 276 ms 124544 KB Output is correct
17 Correct 873 ms 148588 KB Output is correct
18 Correct 1493 ms 178852 KB Output is correct
19 Correct 445 ms 129712 KB Output is correct
20 Correct 434 ms 129520 KB Output is correct
21 Correct 507 ms 129580 KB Output is correct
22 Correct 839 ms 146344 KB Output is correct
23 Correct 579 ms 145872 KB Output is correct
24 Correct 623 ms 146096 KB Output is correct
25 Correct 630 ms 146232 KB Output is correct
26 Correct 675 ms 146348 KB Output is correct
27 Correct 765 ms 141444 KB Output is correct
28 Correct 821 ms 146396 KB Output is correct
29 Correct 788 ms 142912 KB Output is correct
30 Correct 556 ms 137096 KB Output is correct
31 Correct 543 ms 137916 KB Output is correct
32 Correct 548 ms 135296 KB Output is correct
33 Correct 489 ms 137332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 86384 KB Output is correct
2 Correct 45 ms 86428 KB Output is correct
3 Correct 47 ms 86488 KB Output is correct
4 Correct 72 ms 86792 KB Output is correct
5 Correct 106 ms 86900 KB Output is correct
6 Correct 46 ms 86580 KB Output is correct
7 Correct 46 ms 86644 KB Output is correct
8 Correct 49 ms 86660 KB Output is correct
9 Correct 104 ms 89484 KB Output is correct
10 Correct 157 ms 130476 KB Output is correct
11 Correct 84 ms 86644 KB Output is correct
12 Correct 141 ms 86996 KB Output is correct
13 Correct 279 ms 144860 KB Output is correct
14 Correct 314 ms 145368 KB Output is correct
15 Correct 641 ms 155296 KB Output is correct
16 Correct 1168 ms 193704 KB Output is correct
17 Correct 371 ms 163408 KB Output is correct
18 Correct 290 ms 152264 KB Output is correct
19 Correct 413 ms 159700 KB Output is correct
20 Correct 368 ms 159664 KB Output is correct
21 Correct 419 ms 161544 KB Output is correct
22 Correct 250 ms 141268 KB Output is correct
23 Correct 48 ms 86400 KB Output is correct
24 Correct 44 ms 86388 KB Output is correct
25 Correct 49 ms 86496 KB Output is correct
26 Correct 54 ms 86532 KB Output is correct
27 Correct 50 ms 86584 KB Output is correct
28 Correct 54 ms 86584 KB Output is correct
29 Correct 49 ms 86608 KB Output is correct
30 Correct 48 ms 86496 KB Output is correct
31 Correct 49 ms 86588 KB Output is correct
32 Correct 54 ms 86492 KB Output is correct
33 Correct 49 ms 86612 KB Output is correct
34 Correct 47 ms 86556 KB Output is correct
35 Correct 47 ms 86540 KB Output is correct
36 Correct 47 ms 86584 KB Output is correct
37 Correct 44 ms 86484 KB Output is correct
38 Correct 52 ms 86604 KB Output is correct
39 Correct 60 ms 86600 KB Output is correct
40 Correct 48 ms 86612 KB Output is correct
41 Correct 45 ms 86444 KB Output is correct
42 Correct 47 ms 86568 KB Output is correct
43 Correct 47 ms 86520 KB Output is correct
44 Correct 53 ms 86580 KB Output is correct
45 Correct 50 ms 86484 KB Output is correct
46 Correct 47 ms 86444 KB Output is correct
47 Correct 46 ms 86604 KB Output is correct
48 Correct 46 ms 86584 KB Output is correct
49 Correct 48 ms 86496 KB Output is correct
50 Correct 48 ms 86492 KB Output is correct
51 Correct 52 ms 86660 KB Output is correct
52 Correct 49 ms 86672 KB Output is correct
53 Correct 50 ms 86636 KB Output is correct
54 Correct 49 ms 86608 KB Output is correct
55 Correct 47 ms 86604 KB Output is correct
56 Correct 51 ms 86668 KB Output is correct
57 Correct 51 ms 86604 KB Output is correct
58 Correct 56 ms 86640 KB Output is correct
59 Correct 51 ms 86624 KB Output is correct
60 Correct 105 ms 89396 KB Output is correct
61 Correct 153 ms 130384 KB Output is correct
62 Correct 117 ms 89452 KB Output is correct
63 Correct 149 ms 89500 KB Output is correct
64 Correct 102 ms 89612 KB Output is correct
65 Correct 84 ms 88636 KB Output is correct
66 Correct 70 ms 87452 KB Output is correct
67 Correct 187 ms 120232 KB Output is correct
68 Correct 175 ms 120268 KB Output is correct
69 Correct 155 ms 125248 KB Output is correct
70 Correct 164 ms 125236 KB Output is correct
71 Correct 171 ms 120440 KB Output is correct
72 Correct 171 ms 120568 KB Output is correct
73 Correct 160 ms 121376 KB Output is correct
74 Correct 161 ms 121380 KB Output is correct
75 Correct 70 ms 89316 KB Output is correct
76 Correct 200 ms 120104 KB Output is correct
77 Correct 55 ms 86476 KB Output is correct
78 Correct 53 ms 86448 KB Output is correct
79 Correct 47 ms 86412 KB Output is correct
80 Correct 44 ms 86488 KB Output is correct
81 Correct 88 ms 86652 KB Output is correct
82 Correct 46 ms 86520 KB Output is correct
83 Correct 48 ms 86584 KB Output is correct
84 Correct 47 ms 86460 KB Output is correct
85 Correct 48 ms 86400 KB Output is correct
86 Correct 48 ms 86484 KB Output is correct
87 Correct 45 ms 86504 KB Output is correct
88 Correct 50 ms 86628 KB Output is correct
89 Correct 121 ms 86904 KB Output is correct
90 Correct 153 ms 87036 KB Output is correct
91 Correct 128 ms 86896 KB Output is correct
92 Correct 276 ms 124544 KB Output is correct
93 Correct 873 ms 148588 KB Output is correct
94 Correct 1493 ms 178852 KB Output is correct
95 Correct 445 ms 129712 KB Output is correct
96 Correct 434 ms 129520 KB Output is correct
97 Correct 507 ms 129580 KB Output is correct
98 Correct 839 ms 146344 KB Output is correct
99 Correct 579 ms 145872 KB Output is correct
100 Correct 623 ms 146096 KB Output is correct
101 Correct 630 ms 146232 KB Output is correct
102 Correct 675 ms 146348 KB Output is correct
103 Correct 765 ms 141444 KB Output is correct
104 Correct 821 ms 146396 KB Output is correct
105 Correct 788 ms 142912 KB Output is correct
106 Correct 556 ms 137096 KB Output is correct
107 Correct 543 ms 137916 KB Output is correct
108 Correct 548 ms 135296 KB Output is correct
109 Correct 489 ms 137332 KB Output is correct
110 Correct 155 ms 87644 KB Output is correct
111 Correct 103 ms 87128 KB Output is correct
112 Correct 803 ms 157688 KB Output is correct
113 Correct 409 ms 137680 KB Output is correct
114 Correct 555 ms 147684 KB Output is correct
115 Correct 180 ms 121972 KB Output is correct
116 Correct 459 ms 132768 KB Output is correct
117 Correct 1809 ms 201900 KB Output is correct
118 Correct 196 ms 121552 KB Output is correct
119 Correct 189 ms 121624 KB Output is correct
120 Correct 67 ms 90872 KB Output is correct
121 Correct 546 ms 139644 KB Output is correct
122 Correct 537 ms 139660 KB Output is correct
123 Correct 391 ms 140864 KB Output is correct
124 Correct 401 ms 141352 KB Output is correct
125 Correct 417 ms 142432 KB Output is correct
126 Correct 1146 ms 193556 KB Output is correct
127 Correct 589 ms 166816 KB Output is correct
128 Correct 433 ms 169448 KB Output is correct
129 Correct 429 ms 166780 KB Output is correct
130 Correct 463 ms 168448 KB Output is correct