Submission #891072

# Submission time Handle Problem Language Result Execution time Memory
891072 2023-12-22T07:18:28 Z vjudge1 Jail (JOI22_jail) C++17
0 / 100
4 ms 16180 KB
#include <bits/stdc++.h>
 
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define rnd(l, r) uniform_int_distribution<int>(l, r)(rng)
 
using namespace std;

void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);} 
int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;}
int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int N = 2e5;

vector <int> g[N];

int vis[N], parn[N];
int tin[N], tout[N];

int timer;
int up[N][30];

int s[N], t[N];

void dfs(int v, int p){
	parn[v] = p;
	tin[v] = timer++;
	up[v][0] = p;
	for (int i=1; i<=25; ++i)
		up[v][i] = up[up[v][i-1]][i-1];
	for(auto to : g[v]){
		if(to == p)continue;
		dfs(to, v);
	}
	tout[v] = timer++;
}
 
bool upper (int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}
 
 
int lca (int a, int b) {
	if (upper (a, b))  return a;
	if (upper (b, a))  return b;
	for (int i=25; i>=0; --i)
		if (! upper (up[a][i], b))
			a = up[a][i];
	return up[a][0];
}

void solve(){
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		g[i].clear();
		vis[i] = 0;
		tin[i] = 0;
	}
	timer = 0;
	for(int i = 0; i < n - 1; i++){
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	int m;
	cin >> m;
	for(int i = 0; i < m; i++){
		cin >> s[i] >> t[i];
	}
	
	dfs(1, 1);
	
	for(int i = 0; i < m; i++){
			
			int x1 = s[i], y1 = t[i];
			
			//cout << x1 <<"-"<< x2 << endl;
			
			//first
			int lc = lca(x1, y1);
			vis[lc]++;
			int cnt = 1, cntvis = (vis[lc] >= 2);
			while(x1 != lc){
				vis[x1]++;
				x1 = parn[x1];
				cnt++;
				cntvis += (vis[x1] >= 2);
			}
			while(y1 != lc){
				vis[y1]++;
				y1 = parn[y1];
				cnt++;
				cntvis += (vis[y1] >= 2);
			}
			if(cnt == cntvis){
				cout << "No\n";
				return;
			}
			
	}
	cout << "Yes\n";
	
}
 
main(){
	iostream::sync_with_stdio(false);	
    cin.tie(nullptr);
    cout.tie(nullptr);
	int tests;
	cin >> tests;
	while(tests--)
		solve();
}	
/*
 * Before implementing the problem:
	
	Do I understand the problem correctly?
	Which places are tricky? What do I need to remember to implement them correctly?
	Which place is the heaviest by implementation? Can I do it simpler?
	Which place is the slowest? Where do I need to be careful about constant factors and where I can choose slower but simpler implementation?
	----------------------------------
	If not AC:
 
	Did you remember to do everything to handle the tricky places you thought about before?
	Is the solution correct?
	Do I understand the problem correctly?
	----------------------------------
	If you have a test on which the solution gives wrong answer:
 
	Is the solution doing what it was supposed to do?
	Is the problem in the code or in the idea?
*/

Compilation message

jail.cpp:112:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  112 | main(){
      | ^~~~
jail.cpp: In function 'void fp(std::string)':
jail.cpp:13:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:13:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Incorrect 3 ms 15964 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16180 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16180 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16180 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16180 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 3 ms 15964 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Incorrect 3 ms 15964 KB Output isn't correct
3 Halted 0 ms 0 KB -