답안 #890873

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
890873 2023-12-22T04:39:12 Z vjudge1 Jail (JOI22_jail) C++17
0 / 100
4 ms 16220 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++){
		for(int j = i + 1; j < m; j++){
			
			int x1 = s[i], y1 = t[i], x2 = s[j], y2 = t[j];
			
			//cout << x1 <<"-"<< x2 << endl;
			
			//first
			int lc = lca(x1, y1);
			vis[lc]++;
			while(x1 != lc){
				vis[x1]++;
				x1 = parn[x1];
			}
			while(y1 != lc){
				vis[y1]++;
				y1 = parn[y1];
			}
			//second
			lc = lca(x2, y2);
			vis[lc]++;
			while(x2 != lc){
				vis[x2]++;
				x2 = parn[x2];
			}
			
			while(y2 != lc){
				vis[y2]++;
				y2 = parn[y2];
			}
		}
	}
	
	for(int i = 0; i < m; i++){
		for(int j = i + 1; j < m; j++){
			
			int x1 = s[i], y1 = t[i], x2 = s[j], y2 = t[j];
			x1 = s[i], x2 = s[j];
			
			int lc = lca(x1, x2);
			
			int cnt = 1, cntvis = (vis[lc] >= 2);
			while(x1 != lc){
				cntvis += (vis[x1] >= 2);
				cnt++;
				x1 = parn[x1];
			}
			
			while(x2 != lc){
				cntvis += (vis[x2] >= 2);
				cnt++;
				x2 = parn[x2];
			}
			//cout <<s[i] <<"->" <<s[j] <<" "<< cnt <<" "<< cntvis << endl;
			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: In function 'void solve()':
jail.cpp:116:19: warning: unused variable 'y1' [-Wunused-variable]
  116 |    int x1 = s[i], y1 = t[i], x2 = s[j], y2 = t[j];
      |                   ^~
jail.cpp:116:41: warning: unused variable 'y2' [-Wunused-variable]
  116 |    int x1 = s[i], y1 = t[i], x2 = s[j], y2 = t[j];
      |                                         ^~
jail.cpp: At global scope:
jail.cpp:144:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  144 | 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);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15960 KB Output is correct
2 Incorrect 3 ms 15964 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16216 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16220 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16216 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16220 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16216 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16220 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16216 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Incorrect 4 ms 16220 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 16200 KB Output is correct
3 Incorrect 3 ms 16192 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15960 KB Output is correct
2 Incorrect 3 ms 15964 KB Output isn't correct
3 Halted 0 ms 0 KB -