CF 1702 G2. Passable Paths (hard version) 最近公共祖先(lca)2000

1 篇文章 0 订阅

题意:一颗由 n 个顶点,n - 1 条边组成的树,判断能否一条路走完给出的 k 个顶点。

思路:这 k 个点会有两种情况,一种情况是从一个点只有一条路到底,还有一种情况从一个点分成两条路走下去。我们先从 k 个点中找出两个不在同一条路最深的点 t1 和点 t2。如果只能找到一个点,说明是第一种情况,直接输出 YES。第二种情况:我们求出点 t1 和点 t2 的 lca 点m,那么这 k 个点必须在点 t1 和点 m 之间或者是点 t2 和点 m 之间。如果在点 t1 和点 m 之间, 那么这个点和点 t1 的 lca 就是这个点本身,和点 t2 的 lca 就是点 m,同理可以判断这个点是否在 t2 和 m 之间。如果所有点都满足情况,那么说明这 k 个点在一条路上。

代码

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5 + 10, P = 1e9 + 7, mod = 998244353;
vector<int>a[N];
int dep[N], fa[N][30];
int p[N];
int sz;
void dfs(int x, int f){
	dep[x] = dep[f] + 1;
	fa[x][0] = f;
	for(int i = 1; (1 << i) <= dep[x]; i++)
		fa[x][i] = fa[fa[x][i - 1]][i - 1];
	for(int i = 0; i < a[x].size(); i++)
		if(a[x][i] != f) dfs(a[x][i], x);
}
int lca(int x, int y){
	if(dep[x] < dep[y]) swap(x, y);
	for(int i = sz; i >= 0; i--)
		if(dep[fa[x][i]] >= dep[y]) 
			x = fa[x][i];
	if(x == y) return x;
	for(int i = sz; i >= 0; i--)
		if(fa[x][i] != fa[y][i]) 
			x = fa[x][i], y = fa[y][i];
	return fa[x][0];
}
void solve(){
	int n, q;
	cin >> n;
	sz = 0;
	while((1 << sz) < n) sz++;
	for(int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		a[u].pb(v), a[v].pb(u);
	}
	dfs(1, 0);
	cin >> q;
	while(q--){
		int k; cin >> k;
		for(int i = 1; i <= k; i++) cin >> p[i];
		int t1 = 0, t2 = 0;
		for(int i = 1; i <= k; i++)
			if(dep[p[i]] > dep[t1]) t1 = p[i];
		for(int i = 1; i <= k; i++)
			if(lca(p[i], t1) != p[i] && dep[p[i]] > dep[t2]) 
				t2 = p[i];
		if(!t2){
			cout << "YES" << endl;
			continue;
		}
		int m = lca(t1, t2);
		bool ok = 1;
		for(int i = 1; i <= k; i++){
			int m1 = lca(p[i], t1);
			int m2 = lca(p[i], t2);
			if(!((m1 == p[i] && m2 == m) || (m2 == p[i] && m1 == m)))
				ok = 0;
		}
		if(ok) cout << "YES" << endl;
		else cout << "NO"  << endl;
	}
}                 
int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//int t;
	//cin >>t;
	//while(t--){
		solve();
	//}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Title: Android Games Practical Programming By Example: Quickstart 1 (Volume 1) Author: Fred Yang Length: 140 pages Edition: 1 Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2014-05-12 ISBN-10: 1499514697 ISBN-13: 9781499514698 This book offers a step-by-step Android game development guide that's easy to follow with practical tips, illustrations, diagrams, and images, including a full game project explained gradually in each unit. After reading the whole 7 units in this book, you gain the basic knowledge and experience to create stunning Android games that can make you money on Google Play and turn your passion for games into a full time gig. As you work on the game project Raccoon Rob, there is an online forum - Apphex Forums (apphex.com ), dedicated to this book that you can go to, to download the code projects, ask questions and look for technical support, our support team is always there so you can get help directly from the community. What This Book Covers? Unit 1, Setting up Development Environment, begins by teaching you how to set up an Android development environment on your computer no matter which OS you have. You'll learn how to install Android developer tools bundle and configure environment variables. Lastly, we'll introduce a set of Android tools that help you debug and profile your apps. Unit 2, Project Framework, discusses the storyline and framework of your first game Raccoon Rob. You'll be able to implement the activity's lifecycle callback methods and utilize handlers to switch views in game. This unit also goes into detail on how to write the main thread and view for your app. Unit 3, Sprites and Objects, explains how to create sprites, major characters, monsters, and objects. You'll also learn how to implement the animations of sprites and objects using the Bitmap sheets. Unit 4, Layers and Maps, explores the basics of tiles, layers, maps, and the design process that surrounds these terms. You'll be able to easily make game maps. This unit also guides you on how to build a leaderboard and scoring system in game. Unit 5, Game Controls, covers the foundations and implementation process of AI (artificial intelligence) as well as collision detection between objects. You'll also learn how to make a virtual D-Pad on the screen to move the main character on the maps and use an owl icon to control the game state. Unit 6, Sound Effects, introduces the basics of sound effects and the implementation process that surrounds them. You'll learn how to add audio and sounds to games using the Android sound pools, and how to add background music to games using media players. Unit 7, Publishing Games, explains the app submission process on the Google Play Store. This unit will guide you through the process of building, testing, and publishing games onto the Google Play Store. You'll also learn how to monetize your games by two ways: promoting ads and selling in-app features. Who This Book Is For? This book is for aspiring programmers and artists trying to break into the game industry quickly and looking for a practical guide to kick start their projects. It assumes a passable understanding of Java, including how to write classes and handle basic inheritance structures. Table of Contents Unit 1 Setting Up Development Environment Unit 2 Project Framework Unit 3 Sprites And Objects Unit 4 Layers And Maps Unit 5 Game Controls Unit 6 Sound Effects Unit 7 Publishing Your Games
Pro Android 5 shows you how to build real-world and fun mobile apps using the Android 5 SDK. This book updates the best-selling Pro Android and covers everything from the fundamentals of building apps for smartphones, tablets, and embedded devices to advanced concepts such as custom components, multi-tasking, sensors/augmented reality, better accessories support and much more. Using the tutorials and expert advice, you'll quickly be able to build cool mobile apps and run them on dozens of Android-based smartphones. You'll explore and use the Android APIs, including those for media and sensors. And you'll check out what's new in Android, including the improved user interface across all Android platforms, integration with services, and more. By reading this definitive tutorial and reference, you'll gain the knowledge and experience to create stunning, cutting-edge Android apps that can make you money, while keeping you agile enough to respond to changes in the future. What you’ll learn How to use Android to build Java-based mobile apps for Android smartphones and tablets How to build irresistible user interfaces (UIs) and user experiences (UXs) across Android devices How to populate your application with data from data sources, using Content Providers How to build multimedia and game apps using Android's media APIs How to use Android's location-based services, network-based services, and security How to use key Android features, such as Fragments and the ActionBar Who this book is for This book is for professional software engineers and programmers looking to move their ideas and applications into the mobile space with Android. It assumes a passable understanding of Java, including how to write classes and handle basic inheritance structures. Table of Contents Chapter 1 Hello, World Chapter 2 Introduction to Android Applications Chapter 3 Basic User Interface Controls Chapter 4 Adapters and List Controls Chapter 5 Making Advanced UI Layouts Chapter 6 Adding Menus and ActionBar Chapter 7 Styles and Themes Chapter 8 Fragments Chapter 9 Responding to Configuration Changes Chapter 10 Dialogs: Regular and Fragment Chapter 11 Working with Preferences and Saving State Chapter 12 Compatibility Library Chapter 13 Exploring Packages, Processes, Components, Threads and Handlers Chapter 14 Working with Services Chapter 15 Advanced Async Task & Progress Dialogs Chapter 16 Exploring Broadcast Receivers and Long Running Services Chapter 17 Exploring the Alarm Manager Chapter 18 Unveiling 2D Animation Chapter 19 Exploring Maps and Location Services Chapter 20 Understanding the Media Frameworks Chapter 21 Home Screen Widgets Chapter 22 Touchscreens Chapter 23 Drag and Drop Chapter 24 Using Sensors Chapter 25 Understanding Content Providers Chapter 26 Understanding the Contacts API Chapter 27 Loaders Chapter 28 Security and Permissions Chapter 29 Google Cloud messaging and services Chapter 30 Deploying Your Application: Google Play Store and Beyond

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值