Pet

47 篇文章 1 订阅
32 篇文章 1 订阅

Pet

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 4
Problem Description
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.
 

Input
The input contains multiple test cases. Thefirst line is a positive integer T (0<T<=10), cases. For test cases, line positive (0<N<="100000)" D(0<D<N), space. N number locations school D affective distance of trap. The following N-1lines descripts map, each has two integer y(0<="x,y<N)," separated by a single space, meaning that x and y adjacent in the map. Lin Ji’s room is always at location 0. div first <>
 

Output
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
 

Sample Input
   
   
1 10 2 0 1 0 2 0 3 1 4 1 5 2 6 3 7 4 8 6 9
 

Sample Output
   
   
2 //题目大意:他的老鼠跑了,输入n和d。表示有n个点,接下来n-1行, //每行两个数a,b。表示a连接b,每条连接的边权值为1,问距离大于d的点的个数, //转载会长的(明天再仔细研究一下) #include<stdio.h>   #include<string.h>   #include<vector>   #include<string>   #include<iostream>   using namespace std;  int dis[100100];  vector<int>v[100100];  void dfs(int x)  {      int i;      for(i=0;i<v[x].size();i++)      {          dis[v[x][i]]=dis[x]+1;          dfs(v[x][i]);      }      return;  }  int main()  {         int t;      scanf("%d",&t);      while(t--)      {          int n,d,i;          //vector<int>v[100005];                 scanf("%d%d",&n,&d);          for(i=0;i<n;i++)              v[i].clear();          for(i=0;i<n-1;i++)          {              int a,b;              scanf("%d%d",&a,&b);              v[a].push_back(b);          }          memset(dis,0,sizeof(dis));          dfs(0);          int ans=0;          for(i=0;i<n;i++)          {              if(dis[i]>d)                  ans++;          }          printf("%d\n",ans);      }  } 
/* 用邻接表解 */ #include<stdio.h> #include<string.h> int head[100005],cnt,ans,m,n; struct node {  int v,step;  int next; }egde[100005]; void add(int u,int v) {  egde[cnt].v=v;  egde[cnt].next=head[u];  head[u]=cnt++;//head用来存放下标 } void dfs(int x,int step) {  int i,j,k;  if(head[x]==-1)  return ;  for(i=head[x];i!=-1;i=egde[i].next)  {   k=egde[i].v;   egde[i].step=step+1;   if(egde[i].step>n)//记录深度    ans++;    dfs(k,egde[i].step);//深搜,找连在这一分支上的数  } } int main() {  int t,a,b,i;  scanf("%d",&t);  while(t--)  {   cnt=0;   memset(head,-1,sizeof(head));         memset(egde,0,sizeof(egde));   scanf("%d%d",&m,&n);   for(i=1;i<m;i++)   {    scanf("%d%d",&a,&b);    add(a,b);   }   ans=0;   dfs(0,0);   printf("%d\n",ans);  }  return 0; } /* 用并查集解 */ #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #define MAXN 110000 using namespace std; int pri[MAXN]; int sum,m; int find(int x) {     int r=x;     while(r!=pri[r])     r=pri[r];     return r; } int num(int a) {     int b=0;     while(a!=pri[a])     {         a=pri[a];         b++;//深度累加     }     return b; } void fun() {     for(int i=MAXN;i>0;i--)     {         if(find(i)==0&&num(i)>m)         sum++;     } } int main() {     int n,i,a,b,t;     scanf("%d",&t);     while(t--)     {         sum=0;         for(i=0;i<MAXN;i++)         pri[i]=i;         scanf("%d%d",&n,&m);         for(i=0;i<n-1;i++)         {             scanf("%d%d",&a,&b);             pri[b]=a;         }         fun();         printf("%d\n",sum);     }     return 0; }
/*邻接表模板*/ #include<stdio.h> #include<string.h> int head[100100],cnt; struct s {  int u,v,w;  int next; }edge[100010]; void add(int u,int v,int w) {  edge[cnt].u=u;  edge[cnt].v=v;  edge[cnt].w=w;  edge[cnt].next=head[u];  head[u]=cnt++; } int main() {  int n;  while(scanf("%d",&n)!=EOF)  {   int i;   cnt=0;   memset(head,-1,sizeof(head));   for(i=0;i<n;i++)   {    int u,v,w;    scanf("%d%d%d",&u,&v,&w);    add(u,v,w);   }   int u;   scanf("%d",&u);   for(i=head[u];i!=-1;i=edge[i].next)   {    int v=edge[i].v;    int w=edge[i].w;   }  }  return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值