CF1363C Game On Leaves【题解】

embedded/2025/3/5 10:30:24/

洛谷题目链接

CF1363C Game On Leaves

题目描述

Ayush and Ashish play a game on an unrooted tree consisting of $ n $ nodes numbered $ 1 $ to $ n $ . Players make the following move in turns:

  • Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to $ 1 $ .

A tree is a connected undirected graph without cycles.

There is a special node numbered $ x $ . The player who removes this node wins the game.

Ayush moves first. Determine the winner of the game if each player plays optimally.

输入格式

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 10 ) (1 \leq t \leq 10) (1t10) — the number of testcases. The description of the test cases follows.

The first line of each testcase contains two integers n n n and x x x ( 1 ≤ n ≤ 1000 , 1 ≤ x ≤ n ) (1\leq n \leq 1000, 1 \leq x \leq n) (1n1000,1xn) — the number of nodes in the tree and the special node respectively.

Each of the next n − 1 n-1 n1 lines contain two integers u u u , v v v ( 1 ≤ u , v ≤ n , u ≠ v ) (1 \leq u, v \leq n, \text{ } u \ne v) (1u,vn, u=v) , meaning that there is an edge between nodes u u u and v v v in the tree.

输出格式

For every test case, if Ayush wins the game, print “Ayush”, otherwise print “Ashish” (without quotes).

输入输出样例 #1

输入 #1

1
3 1
2 1
3 1

输出 #1

Ashish

输入输出样例 #2

输入 #2

1
3 2
1 2
1 3

输出 #2

Ayush

说明/提示

For the 1 1 1 st test case, Ayush can only remove node 2 2 2 or 3 3 3 , after which node 1 1 1 becomes a leaf node and Ashish can remove it in his turn.

For the 2 2 2 nd test case, Ayush can remove node 2 2 2 in the first move itself.

题意翻译

给定 n 个节点的无根树。

两名选手轮流操作,每次可以选择一个叶节点并删除它以及所有和它相连的边。叶节点指度数不超过 1 的节点。删除节点 x 的选手胜利。

你需要判断先手是否有必胜策略。具体来讲,如果先手有必胜策略,输出 Ayush,否则输出 Ashish。

多组数据,数据组数 t≤10,n≤1000

分析:

我们考虑两种情况

①x在叶子节点上:此时先手只需直接删 x x x点便能获胜

②x不在叶结点上:先手何时能胜?就是在 x x x的度为 2 2 2并且只剩 3 3 3个点的时候(如图所示)将树扔给后手,此时后手只能删掉非 x x x点中的一个,那么 x x x的度就变为 1 1 1,先手必胜。
分析:我们考虑两种情况

在这里插入图片描述

所以,我们首先特判 x x x是否在叶节点上,再看边的条数的奇偶性.是奇数, x x x最后就被先手删了。

#include<bits/stdc++.h>
using namespace std;
int n,T,a,b,x;
int degree[100001];//记录每个点的度数
int main(){cin>>T;while(T--){cin>>n>>x;memset(degree,0,sizeof degree);//多组数据要清空for(int i=1;i<n;i++){cin>>a>>b;degree[a]++;degree[b]++;}if(degree[x]<=1) cout<<"Ayush";//特判else{if(n%2==1){cout<<"Ashish";}else{cout<<"Ayush";}}cout<<endl; }return 0;
}

给个赞呗QAQ


http://www.ppmy.cn/embedded/170158.html

相关文章

DeepSeek架构革命:动态异构计算

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;精通Java编…

知识图谱的推荐实现方案(Vue)

使用 Vue 实现知识图谱思维导图展示的完整方案&#xff0c;结合主流库和最佳实践&#xff1a; 一、技术选型 组件库特点适用场景MindElixir国产开源、中文文档完善、支持关系线教育类知识图谱GoJS功能强大、商业许可、适合复杂交互企业级应用&#xff08;需付费&#xff09;D3…

quillEditor 禁用复制粘贴图片,以及class转style等问题

<template><div><div class"search-term"><el-form :inline"true" :model"searchInfo" class"demo-form-inline"><el-form-item label"案例标题"><el-input v-model"searchInfo.titl…

鸿蒙启动页开发

鸿蒙启动页开发 1.1 更改应用名称和图标 1.更改应用图标 找到moudle.json5文件&#xff0c;找到应用启动的EntryAbility下面的icon,将原来的图标改成自己设置的即可 2.更改应用名称 3.效果展示 2.1 广告页面开发 3.1 详细介绍 3.1.1 启动页面 import { PrivacyDialog } fr…

腾讯 TDF 即将开源 Kuikly 跨端框架,Kotlin 支持全平台

今天&#xff0c;在腾讯的 Shiply 平台看 Flutter 动态化自研框架 Conch 时&#xff0c;在侧边栏看到了有「跨端开发框架」的介绍&#xff0c;点开发现有两个产品&#xff1a; Hippy&#xff1a;面向前端技术栈的跨端开发框架&#xff0c;Web原生开发体验&#xff0c;支持 Rea…

【氮化镓】基于SiC脉冲I-V系统研究Schottky型p-GaN HEMT正栅极ESD机制

这篇文章题为《Investigating Forward Gate ESD Mechanism of Schottky-Type p-GaN Gate HEMTs Using a SiC-Based High-Speed Pulsed I-V Test System》,发表于《IEEE Electron Device Letters》2024年7月刊。研究重点是探讨肖特基型p-GaN门极高电子迁移率晶体管(HEMTs)在正…

C++核心编程之Vector容器

C vector容器详解&#xff1a;从基础到实战应用 引言 在C STL&#xff08;标准模板库&#xff09;中&#xff0c;vector是最常用的动态数组容器之一。它结合了数组的高效随机访问和动态扩展的灵活性&#xff0c;是处理动态数据集合的利器。本文将深入剖析vector的各个核心功能…

springBoot集成emqx 实现mqtt消息的发送订阅

介绍 我们可以想象这么一个场景&#xff0c;我们java应用想要采集到电表a的每小时的用电信息&#xff0c;我们怎么拿到电表的数据&#xff1f;一般我们会想 直接 java 后台发送请求给电表&#xff0c;然后让电表返回数据就可以了&#xff0c;事实上&#xff0c;我们java应用发…