답안 #887156

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
887156 2023-12-13T22:57:13 Z MilosMilutinovic Jail (JOI22_jail) C++14
100 / 100
1198 ms 382684 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=120005;
const int M=45*N;

int _,n,m,s[N],t[N],sidx[N][20],tidx[N][20],dep[N],p[N][20],sa[N],ta[N],deg[M];
VI e[N],g[M];

void dfs(int u,int f){
	dep[u]=dep[f]+1;
	p[u][0]=f;
	rep(i,1,20) p[u][i]=p[p[u][i-1]][i-1];
	for (int v:e[u]) {
		if (v==f) continue;
		dfs(v,u);
	}
}

int lca(int u,int v) {
	if (dep[u]>dep[v]) swap(u,v);
	per(i,0,20) if (dep[p[v][i]]>=dep[u]) v=p[v][i];
	if (u==v) return u;
	per(i,0,20) if (p[u][i]!=p[v][i]) u=p[u][i],v=p[v][i];
	return p[u][0];
}

void adds(int x,int k,int i) {
	if (k<0) return;
	k++;
	per(j,0,20) {
		if (k>>j&1) {
			g[i].pb(sidx[x][j]);
			x=p[x][j];
		}
	}
}

void addt(int x,int k,int i) {
	if (k<0) return;
	k++;
	per(j,0,20) {
		if (k>>j&1) {
			g[tidx[x][j]].pb(i);
			x=p[x][j];
		}
	}
}

bool solve() {
	scanf("%d",&n);
	rep(i,1,n) {
		int u,v;
		scanf("%d%d",&u,&v);
		e[u].pb(v); 
		e[v].pb(u);
	}
	scanf("%d",&m);
	rep(i,1,m+1) scanf("%d%d",&s[i],&t[i]);
	dfs(1,1);
	int idx=m;
	rep(i,1,n+1) {
		sidx[i][0]=++idx;
		tidx[i][0]=++idx;
	}
	rep(j,1,20) {
		rep(i,1,n+1) {
			sidx[i][j]=++idx;
			tidx[i][j]=++idx;
			g[sidx[i][j]].pb(sidx[i][j-1]);
			g[sidx[i][j]].pb(sidx[p[i][j-1]][j-1]);
			g[tidx[i][j-1]].pb(tidx[i][j]);
			g[tidx[p[i][j-1]][j-1]].pb(tidx[i][j]);
		}
	}
	rep(i,1,n+1) sa[i]=ta[i]=0;
	rep(i,1,m+1) sa[s[i]]=i,ta[t[i]]=i;
	rep(i,1,n+1) {
		if (sa[i]!=0) g[sidx[i][0]].pb(sa[i]);
		if (ta[i]!=0) g[ta[i]].pb(tidx[i][0]);
	}
	rep(i,1,m+1) {
		int l=lca(s[i],t[i]);
		adds(t[i],dep[t[i]]-dep[l]-(s[i]==l?1:0),i);
		if (s[i]!=l) {
			adds(p[s[i]][0],dep[s[i]]-dep[l]-1,i);
		}
	}
	rep(i,1,m+1) {
		int l=lca(s[i],t[i]);
		addt(s[i],dep[s[i]]-dep[l]-(t[i]==l?1:0),i);
		if (t[i]!=l) {
			addt(p[t[i]][0],dep[t[i]]-dep[l]-1,i);
		}
	}
	rep(i,1,idx+1) {
		for (int j:g[i]) {
			deg[j]+=1;
		}
	}
	VI que;
	rep(i,1,idx+1) if (deg[i]==0) que.pb(i);
	rep(b,0,SZ(que)) {
		int x=que[b];
		for (int y:g[x]) {
			deg[y]-=1;
			if (deg[y]==0) que.pb(y);
		}
	}
	bool ok=true;
	rep(i,1,idx+1) {
		if (deg[i]!=0) ok=false;
		g[i].clear();
		deg[i]=0;
	}
	rep(i,1,n+1) {
		dep[i]=0;
		rep(j,0,20) {
			p[i][j]=0;
			sidx[i][j]=0;
			tidx[i][j]=0;
		}
		e[i].clear();
	}
	return ok;
}

int main() {
	for (scanf("%d",&_);_;_--) {
		printf(solve()?"Yes\n":"No\n");
	}
}

Compilation message

jail.cpp: In function 'bool solve()':
jail.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
jail.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |   scanf("%d%d",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~
jail.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |  scanf("%d",&m);
      |  ~~~~~^~~~~~~~~
jail.cpp:79:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |  rep(i,1,m+1) scanf("%d%d",&s[i],&t[i]);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~
jail.cpp: In function 'int main()':
jail.cpp:149:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |  for (scanf("%d",&_);_;_--) {
      |       ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 138072 KB Output is correct
2 Correct 29 ms 138076 KB Output is correct
3 Correct 30 ms 137956 KB Output is correct
4 Correct 66 ms 138272 KB Output is correct
5 Correct 107 ms 138124 KB Output is correct
6 Correct 33 ms 138328 KB Output is correct
7 Correct 33 ms 138328 KB Output is correct
8 Correct 35 ms 138588 KB Output is correct
9 Correct 180 ms 150516 KB Output is correct
10 Correct 478 ms 369728 KB Output is correct
11 Correct 48 ms 138076 KB Output is correct
12 Correct 132 ms 139932 KB Output is correct
13 Correct 609 ms 376436 KB Output is correct
14 Correct 513 ms 352316 KB Output is correct
15 Correct 715 ms 348892 KB Output is correct
16 Correct 1063 ms 361024 KB Output is correct
17 Correct 532 ms 376748 KB Output is correct
18 Correct 591 ms 380036 KB Output is correct
19 Correct 527 ms 375776 KB Output is correct
20 Correct 497 ms 358496 KB Output is correct
21 Correct 547 ms 349588 KB Output is correct
22 Correct 469 ms 359420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 138076 KB Output is correct
2 Correct 29 ms 138104 KB Output is correct
3 Correct 33 ms 138320 KB Output is correct
4 Correct 35 ms 138504 KB Output is correct
5 Correct 33 ms 138472 KB Output is correct
6 Correct 33 ms 138588 KB Output is correct
7 Correct 34 ms 138588 KB Output is correct
8 Correct 34 ms 138456 KB Output is correct
9 Correct 33 ms 138468 KB Output is correct
10 Correct 35 ms 138588 KB Output is correct
11 Correct 34 ms 138576 KB Output is correct
12 Correct 33 ms 138332 KB Output is correct
13 Correct 31 ms 138332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 138076 KB Output is correct
2 Correct 29 ms 138104 KB Output is correct
3 Correct 33 ms 138320 KB Output is correct
4 Correct 35 ms 138504 KB Output is correct
5 Correct 33 ms 138472 KB Output is correct
6 Correct 33 ms 138588 KB Output is correct
7 Correct 34 ms 138588 KB Output is correct
8 Correct 34 ms 138456 KB Output is correct
9 Correct 33 ms 138468 KB Output is correct
10 Correct 35 ms 138588 KB Output is correct
11 Correct 34 ms 138576 KB Output is correct
12 Correct 33 ms 138332 KB Output is correct
13 Correct 31 ms 138332 KB Output is correct
14 Correct 31 ms 138332 KB Output is correct
15 Correct 29 ms 138076 KB Output is correct
16 Correct 32 ms 138524 KB Output is correct
17 Correct 34 ms 138588 KB Output is correct
18 Correct 34 ms 138576 KB Output is correct
19 Correct 29 ms 138076 KB Output is correct
20 Correct 34 ms 138580 KB Output is correct
21 Correct 34 ms 138324 KB Output is correct
22 Correct 33 ms 138584 KB Output is correct
23 Correct 29 ms 138072 KB Output is correct
24 Correct 30 ms 138584 KB Output is correct
25 Correct 34 ms 138588 KB Output is correct
26 Correct 31 ms 138588 KB Output is correct
27 Correct 34 ms 138592 KB Output is correct
28 Correct 30 ms 137964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 138076 KB Output is correct
2 Correct 29 ms 138104 KB Output is correct
3 Correct 33 ms 138320 KB Output is correct
4 Correct 35 ms 138504 KB Output is correct
5 Correct 33 ms 138472 KB Output is correct
6 Correct 33 ms 138588 KB Output is correct
7 Correct 34 ms 138588 KB Output is correct
8 Correct 34 ms 138456 KB Output is correct
9 Correct 33 ms 138468 KB Output is correct
10 Correct 35 ms 138588 KB Output is correct
11 Correct 34 ms 138576 KB Output is correct
12 Correct 33 ms 138332 KB Output is correct
13 Correct 31 ms 138332 KB Output is correct
14 Correct 31 ms 138332 KB Output is correct
15 Correct 29 ms 138076 KB Output is correct
16 Correct 32 ms 138524 KB Output is correct
17 Correct 34 ms 138588 KB Output is correct
18 Correct 34 ms 138576 KB Output is correct
19 Correct 29 ms 138076 KB Output is correct
20 Correct 34 ms 138580 KB Output is correct
21 Correct 34 ms 138324 KB Output is correct
22 Correct 33 ms 138584 KB Output is correct
23 Correct 29 ms 138072 KB Output is correct
24 Correct 30 ms 138584 KB Output is correct
25 Correct 34 ms 138588 KB Output is correct
26 Correct 31 ms 138588 KB Output is correct
27 Correct 34 ms 138592 KB Output is correct
28 Correct 30 ms 137964 KB Output is correct
29 Correct 36 ms 138736 KB Output is correct
30 Correct 35 ms 138808 KB Output is correct
31 Correct 34 ms 138576 KB Output is correct
32 Correct 35 ms 138584 KB Output is correct
33 Correct 34 ms 138588 KB Output is correct
34 Correct 34 ms 138332 KB Output is correct
35 Correct 33 ms 138468 KB Output is correct
36 Correct 33 ms 138332 KB Output is correct
37 Correct 35 ms 138324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 138076 KB Output is correct
2 Correct 29 ms 138104 KB Output is correct
3 Correct 33 ms 138320 KB Output is correct
4 Correct 35 ms 138504 KB Output is correct
5 Correct 33 ms 138472 KB Output is correct
6 Correct 33 ms 138588 KB Output is correct
7 Correct 34 ms 138588 KB Output is correct
8 Correct 34 ms 138456 KB Output is correct
9 Correct 33 ms 138468 KB Output is correct
10 Correct 35 ms 138588 KB Output is correct
11 Correct 34 ms 138576 KB Output is correct
12 Correct 33 ms 138332 KB Output is correct
13 Correct 31 ms 138332 KB Output is correct
14 Correct 31 ms 138332 KB Output is correct
15 Correct 29 ms 138076 KB Output is correct
16 Correct 32 ms 138524 KB Output is correct
17 Correct 34 ms 138588 KB Output is correct
18 Correct 34 ms 138576 KB Output is correct
19 Correct 29 ms 138076 KB Output is correct
20 Correct 34 ms 138580 KB Output is correct
21 Correct 34 ms 138324 KB Output is correct
22 Correct 33 ms 138584 KB Output is correct
23 Correct 29 ms 138072 KB Output is correct
24 Correct 30 ms 138584 KB Output is correct
25 Correct 34 ms 138588 KB Output is correct
26 Correct 31 ms 138588 KB Output is correct
27 Correct 34 ms 138592 KB Output is correct
28 Correct 30 ms 137964 KB Output is correct
29 Correct 36 ms 138736 KB Output is correct
30 Correct 35 ms 138808 KB Output is correct
31 Correct 34 ms 138576 KB Output is correct
32 Correct 35 ms 138584 KB Output is correct
33 Correct 34 ms 138588 KB Output is correct
34 Correct 34 ms 138332 KB Output is correct
35 Correct 33 ms 138468 KB Output is correct
36 Correct 33 ms 138332 KB Output is correct
37 Correct 35 ms 138324 KB Output is correct
38 Correct 179 ms 150496 KB Output is correct
39 Correct 467 ms 368492 KB Output is correct
40 Correct 194 ms 152252 KB Output is correct
41 Correct 202 ms 152012 KB Output is correct
42 Correct 177 ms 152712 KB Output is correct
43 Correct 205 ms 152924 KB Output is correct
44 Correct 54 ms 140256 KB Output is correct
45 Correct 557 ms 372320 KB Output is correct
46 Correct 574 ms 371840 KB Output is correct
47 Correct 540 ms 368844 KB Output is correct
48 Correct 533 ms 368348 KB Output is correct
49 Correct 516 ms 372152 KB Output is correct
50 Correct 513 ms 371088 KB Output is correct
51 Correct 566 ms 370732 KB Output is correct
52 Correct 610 ms 371780 KB Output is correct
53 Correct 86 ms 155888 KB Output is correct
54 Correct 631 ms 372520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 138064 KB Output is correct
2 Correct 29 ms 138064 KB Output is correct
3 Correct 30 ms 138068 KB Output is correct
4 Correct 29 ms 138072 KB Output is correct
5 Correct 53 ms 138344 KB Output is correct
6 Correct 33 ms 138588 KB Output is correct
7 Correct 31 ms 138588 KB Output is correct
8 Correct 29 ms 138072 KB Output is correct
9 Correct 30 ms 138088 KB Output is correct
10 Correct 30 ms 138332 KB Output is correct
11 Correct 29 ms 137944 KB Output is correct
12 Correct 33 ms 138588 KB Output is correct
13 Correct 93 ms 139140 KB Output is correct
14 Correct 136 ms 139092 KB Output is correct
15 Correct 112 ms 138832 KB Output is correct
16 Correct 560 ms 356764 KB Output is correct
17 Correct 639 ms 363132 KB Output is correct
18 Correct 732 ms 362816 KB Output is correct
19 Correct 590 ms 375184 KB Output is correct
20 Correct 597 ms 375064 KB Output is correct
21 Correct 616 ms 374988 KB Output is correct
22 Correct 568 ms 363356 KB Output is correct
23 Correct 581 ms 362324 KB Output is correct
24 Correct 659 ms 379248 KB Output is correct
25 Correct 674 ms 379192 KB Output is correct
26 Correct 661 ms 379036 KB Output is correct
27 Correct 701 ms 382684 KB Output is correct
28 Correct 576 ms 366144 KB Output is correct
29 Correct 629 ms 366528 KB Output is correct
30 Correct 672 ms 378160 KB Output is correct
31 Correct 715 ms 361016 KB Output is correct
32 Correct 699 ms 375724 KB Output is correct
33 Correct 592 ms 358972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 138072 KB Output is correct
2 Correct 29 ms 138076 KB Output is correct
3 Correct 30 ms 137956 KB Output is correct
4 Correct 66 ms 138272 KB Output is correct
5 Correct 107 ms 138124 KB Output is correct
6 Correct 33 ms 138328 KB Output is correct
7 Correct 33 ms 138328 KB Output is correct
8 Correct 35 ms 138588 KB Output is correct
9 Correct 180 ms 150516 KB Output is correct
10 Correct 478 ms 369728 KB Output is correct
11 Correct 48 ms 138076 KB Output is correct
12 Correct 132 ms 139932 KB Output is correct
13 Correct 609 ms 376436 KB Output is correct
14 Correct 513 ms 352316 KB Output is correct
15 Correct 715 ms 348892 KB Output is correct
16 Correct 1063 ms 361024 KB Output is correct
17 Correct 532 ms 376748 KB Output is correct
18 Correct 591 ms 380036 KB Output is correct
19 Correct 527 ms 375776 KB Output is correct
20 Correct 497 ms 358496 KB Output is correct
21 Correct 547 ms 349588 KB Output is correct
22 Correct 469 ms 359420 KB Output is correct
23 Correct 29 ms 138076 KB Output is correct
24 Correct 29 ms 138104 KB Output is correct
25 Correct 33 ms 138320 KB Output is correct
26 Correct 35 ms 138504 KB Output is correct
27 Correct 33 ms 138472 KB Output is correct
28 Correct 33 ms 138588 KB Output is correct
29 Correct 34 ms 138588 KB Output is correct
30 Correct 34 ms 138456 KB Output is correct
31 Correct 33 ms 138468 KB Output is correct
32 Correct 35 ms 138588 KB Output is correct
33 Correct 34 ms 138576 KB Output is correct
34 Correct 33 ms 138332 KB Output is correct
35 Correct 31 ms 138332 KB Output is correct
36 Correct 31 ms 138332 KB Output is correct
37 Correct 29 ms 138076 KB Output is correct
38 Correct 32 ms 138524 KB Output is correct
39 Correct 34 ms 138588 KB Output is correct
40 Correct 34 ms 138576 KB Output is correct
41 Correct 29 ms 138076 KB Output is correct
42 Correct 34 ms 138580 KB Output is correct
43 Correct 34 ms 138324 KB Output is correct
44 Correct 33 ms 138584 KB Output is correct
45 Correct 29 ms 138072 KB Output is correct
46 Correct 30 ms 138584 KB Output is correct
47 Correct 34 ms 138588 KB Output is correct
48 Correct 31 ms 138588 KB Output is correct
49 Correct 34 ms 138592 KB Output is correct
50 Correct 30 ms 137964 KB Output is correct
51 Correct 36 ms 138736 KB Output is correct
52 Correct 35 ms 138808 KB Output is correct
53 Correct 34 ms 138576 KB Output is correct
54 Correct 35 ms 138584 KB Output is correct
55 Correct 34 ms 138588 KB Output is correct
56 Correct 34 ms 138332 KB Output is correct
57 Correct 33 ms 138468 KB Output is correct
58 Correct 33 ms 138332 KB Output is correct
59 Correct 35 ms 138324 KB Output is correct
60 Correct 179 ms 150496 KB Output is correct
61 Correct 467 ms 368492 KB Output is correct
62 Correct 194 ms 152252 KB Output is correct
63 Correct 202 ms 152012 KB Output is correct
64 Correct 177 ms 152712 KB Output is correct
65 Correct 205 ms 152924 KB Output is correct
66 Correct 54 ms 140256 KB Output is correct
67 Correct 557 ms 372320 KB Output is correct
68 Correct 574 ms 371840 KB Output is correct
69 Correct 540 ms 368844 KB Output is correct
70 Correct 533 ms 368348 KB Output is correct
71 Correct 516 ms 372152 KB Output is correct
72 Correct 513 ms 371088 KB Output is correct
73 Correct 566 ms 370732 KB Output is correct
74 Correct 610 ms 371780 KB Output is correct
75 Correct 86 ms 155888 KB Output is correct
76 Correct 631 ms 372520 KB Output is correct
77 Correct 29 ms 138064 KB Output is correct
78 Correct 29 ms 138064 KB Output is correct
79 Correct 30 ms 138068 KB Output is correct
80 Correct 29 ms 138072 KB Output is correct
81 Correct 53 ms 138344 KB Output is correct
82 Correct 33 ms 138588 KB Output is correct
83 Correct 31 ms 138588 KB Output is correct
84 Correct 29 ms 138072 KB Output is correct
85 Correct 30 ms 138088 KB Output is correct
86 Correct 30 ms 138332 KB Output is correct
87 Correct 29 ms 137944 KB Output is correct
88 Correct 33 ms 138588 KB Output is correct
89 Correct 93 ms 139140 KB Output is correct
90 Correct 136 ms 139092 KB Output is correct
91 Correct 112 ms 138832 KB Output is correct
92 Correct 560 ms 356764 KB Output is correct
93 Correct 639 ms 363132 KB Output is correct
94 Correct 732 ms 362816 KB Output is correct
95 Correct 590 ms 375184 KB Output is correct
96 Correct 597 ms 375064 KB Output is correct
97 Correct 616 ms 374988 KB Output is correct
98 Correct 568 ms 363356 KB Output is correct
99 Correct 581 ms 362324 KB Output is correct
100 Correct 659 ms 379248 KB Output is correct
101 Correct 674 ms 379192 KB Output is correct
102 Correct 661 ms 379036 KB Output is correct
103 Correct 701 ms 382684 KB Output is correct
104 Correct 576 ms 366144 KB Output is correct
105 Correct 629 ms 366528 KB Output is correct
106 Correct 672 ms 378160 KB Output is correct
107 Correct 715 ms 361016 KB Output is correct
108 Correct 699 ms 375724 KB Output is correct
109 Correct 592 ms 358972 KB Output is correct
110 Correct 138 ms 139508 KB Output is correct
111 Correct 123 ms 133000 KB Output is correct
112 Correct 854 ms 353644 KB Output is correct
113 Correct 835 ms 367016 KB Output is correct
114 Correct 699 ms 351040 KB Output is correct
115 Correct 487 ms 368392 KB Output is correct
116 Correct 847 ms 374404 KB Output is correct
117 Correct 943 ms 361380 KB Output is correct
118 Correct 753 ms 368496 KB Output is correct
119 Correct 699 ms 369532 KB Output is correct
120 Correct 89 ms 158672 KB Output is correct
121 Correct 989 ms 375924 KB Output is correct
122 Correct 949 ms 375912 KB Output is correct
123 Correct 880 ms 370192 KB Output is correct
124 Correct 678 ms 345720 KB Output is correct
125 Correct 863 ms 370200 KB Output is correct
126 Correct 1198 ms 362216 KB Output is correct
127 Correct 746 ms 356804 KB Output is correct
128 Correct 636 ms 356876 KB Output is correct
129 Correct 838 ms 378636 KB Output is correct
130 Correct 796 ms 354116 KB Output is correct