Pet

来源:互联网 发布:强生研发 知乎 编辑:程序博客网 时间:2024/04/30 02:03
<div class="plm" style="text-align: center; font-size: 12pt; color: rgb(34, 34, 34); clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;"><span style="color: green;">I - </span>Pet</div><span id="crawlSuccess" class="crawlInfo" style="display: inline;"><strong>Time Limit:</strong><span id="timeLimit">2000</span>MS     <strong>Memory Limit:</strong><span id="memoryLimit">32768</span>KB     <strong>64bit IO Format:</strong><span id="_64IOFormat">%I64d & %I64u</span></span><div id="problem_opt" style="font-size: 12px; margin-top: 10px;"><a target=_blank id="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank id="problem_status" href="http://acm.hust.edu.cn/vjudge/contest/125257#status//I/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Status</span></a></div></div><div style="color: rgb(34, 34, 34); font-family: Verdana, Arial, sans-serif; font-size: 14px; width: 960px; margin: auto;"><div id="desc_I_0" class="desc_template"><div class="vj_description"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="panel_content">One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room and waited for three days. Nothing but cockroaches was caught. He got the map of the school and foundthat there is no cyclic path and every location in the school can be reached from his room. The trap’s manual mention that the pet will always come back if it still in somewhere nearer than distance D. Your task is to help Lin Ji to find out how many possible locations the hamster may found given the map of the school. Assume that the hamster is still hiding in somewhere in the school and distance between each adjacent locations is always one distance unit.</div></div></div><div class="vj_input"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Input</p><div class="textBG" style="border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);">The input contains multiple test cases. Thefirst line is a positive integer T (0<T<=10), the number of test cases. For each test cases, the first line has two positive integer N (0<N<=100000) and D(0<D<N), separated by a single space. N is the number of locations in the school and D is the affective distance of the trap. The following N-1lines descripts the map, each has two integer x and y(0<=x,y<N), separated by a single space, meaning that x and y is adjacent in the map. Lin Ji’s room is always at location 0. </div></div><div class="vj_output"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Output</p><div class="textBG" style="border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);">For each test case, outputin a single line the number of possible locations in the school the hamster may be found.</div></div><div class="vj_sampleInput"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Sample Input</p><div class="textBG" style="border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><pre style="white-space: pre-wrap; word-wrap: break-word;">110 20 10 20 31 41 52 63 74 86 9

Sample Output

2

#include<stdio.h>//i#include<string.h>#include<iostream>#include<algorithm>using namespace std;#define MAXN 100010int d[MAXN],f[MAXN];int n,deep,a,b,fa,fb;int finds(int i){int temp;if(f[i]!=i){temp = finds(f[i]);if(f[f[i]]!=f[i])d[i] = d[f[i]]+1;f[i] = temp;}return f[i];}int main(){int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&deep);for(int i=0; i<=n; ++i){f[i] = i; d[i] = 0;}for(int i=1; i<n; ++i){scanf("%d%d",&a,&b);fa = finds(a);fb = finds(b);if(fa == fb && abs(d[a] - d[b])>1){//新get,abs取绝对值,判断之间是否隔着其他点; if(d[a] < d[b]){d[a] = d[b]+1;}else{d[b] = d[a]+1;}}else{f[fb] = a;d[fb] = d[a]+1;}}for(int i=0; i<n; ++i){finds(i);}int sum = 0;for(int i=0; i<n; ++i){if(abs(d[i]-d[0] > deep)){++sum;}}printf("%d\n",sum);}}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米note4x玩游戏卡怎么办 华为4c死屏怎么办 华为4c充电很慢怎么办? 华为4c突然死机了怎么办 华为畅玩4c内存不足怎么办 荣耀4c一直亮屏怎么办 华为手机返回键失灵怎么办 荣耀6p死机了怎么办 荣耀6主板烧坏了怎么办 虚拟运营商倒闭了号怎么办 买到二次放号怎么办 新运动鞋鞋穿着有点紧怎么办 一件代发被买家退货后怎么办? 洗了翻毛的鞋子怎么办 猫眼竹芋泡根了怎么办 双线花叶子卷了怎么办 华为云收藏满了怎么办 小米6云空间满了怎么办 华为云存储已满怎么办 苹果云备份空间不足怎么办 oppor9指纹与密码忘记怎么办 oppo显示密码格式不对怎么办 oppo云服务密码忘了怎么办 小米云内存满了怎么办 id储存空间满了怎么办 苹果手机邮箱服务器连接失败怎么办 苹果查看id闪退怎么办 域名卖出去后涉及赌博怎么办 发票认证后对方作废了怎么办 手机在屋里没信号怎么办 百度云上传文件和谐怎么办 收货地址写错了怎么办 阿里巴巴国际版出现加密令牌怎么办 淘宝寄货到转运仓拒收怎么办 淘宝已发货买家申请退货怎么办 买家给了一个差评不接电话怎么办 卖家单号填错了怎么办 拼多多虚假发货买家怎么办 买家退款后又收到货怎么办 买家确认收货已超时怎么办 淘宝买家不确认收货怎么办