吝啬的国度
来源:互联网 发布:电商和淘宝有什么区别 编辑:程序博客网 时间:2024/04/28 08:11
- 输入
- 第一行输入一个整数M表示测试数据共有M(1<=M<=5)组
每组测试数据的第一行输入一个正整数N(1<=N<=100000)和一个正整数S(1<=S<=100000),N表示城市的总个数,S表示参观者所在城市的编号
随后的N-1行,每行有两个正整数a,b(1<=a,b<=N),表示第a号城市和第b号城市之间有一条路连通。 - 输出
- 每组测试数据输N个正整数,其中,第i个数表示从S走到i号城市,必须要经过的上一个城市的编号。(其中i=S时,请输出-1)
- 样例输入
110 11 91 88 1010 38 61 210 49 53 7
- 样例输出
-1 1 10 10 9 8 3 1 1 8
//标程:
#include<stdio.h>#define Max 100100struct node{int to;node *next;};struct vnode{int from;node *first;}a[Max];void fun(int x,int y){a[x].from=y;node *first=a[x].first;while(first!=NULL){if(a[first->to].from==0) fun(first->to,x);first=first->next;}}void Delete(int n){int i;node *p,*q;for(i=1;i<=n;i++){p=a[i].first;while(p!=NULL){q=p->next; delete p; p=q;}}}int main(){//freopen("a.txt","r",stdin); int s,n,i,j,t,m,e; scanf("%d",&t); node *p;while(t--){scanf("%d%d",&n,&m);for(i=0;i<=n;i++){a[i].from=0;a[i].first=NULL;} for(i=1;i<n;i++){scanf("%d%d",&s,&e);p=new node;p->to=e; p->next=a[s].first;a[s].first=p;p=new node;p->to=s; p->next=a[e].first;a[e].first=p;}/*for(i=1;i<=n;i++){for(node *k=a[i].first;k!=NULL;k=k->next)printf("%d\n",k->to);}*/fun(m,-1);for(i=1;i<n;i++)printf("%d ",a[i].from); printf("%d\n",a[n].from); Delete(n);}return 0;}
//用vector模拟
//标程:
#include<iostream>#include<vector>#include<stdio.h>#include<string.h>using namespace std;#define Max 100100int dp[Max];vector<int> map[Max];void fun(int x){ vector<int> :: iterator it;for(it=map[x].begin();it!=map[x].end();++it) if(dp[*it]!=-1 && dp[*it]==0){dp[*it]=x;fun(*it);}}int main(){// freopen("in.txt","r",stdin); int n,s,t,a,b,i,j; scanf("%d",&t);while(t--){for(i=0;i<=n;i++) map[i].clear();memset(dp,0,sizeof(dp));scanf("%d%d",&n,&s);for(i=1;i<n;i++) {scanf("%d%d",&a,&b); map[a].push_back(b);map[b].push_back(a);}dp[s]=-1;fun(s);for(i=1;i<n;i++)printf("%d ",dp[i]);printf("%d\n",dp[n]);}return 0;}
0 0
- nyoj_20 吝啬的国度
- 吝啬的国度
- noj 吝啬的国度
- 吝啬的国度
- NYOJ 吝啬的国度
- NYOJ 吝啬的国度
- 吝啬的国度
- nyoj20(吝啬的国度)
- 吝啬的国度
- 吝啬的国度
- 【搜索】吝啬的国度
- 吝啬的国度
- NY20 吝啬的国度
- NYOJ20-吝啬的国度
- 吝啬的国度
- 吝啬的国度
- 吝啬的国度
- 吝啬的国度 nyoj
- fork,vfork,clone与pthread_create之间的区别
- MFC 三类消息
- C语言 interview总结
- php 如何获得url参数中具有&的值
- 区间覆盖问题
- 吝啬的国度
- ARM Linux异常处理之data abort
- linux下I2C驱动架构全面分析
- Collections.sort的两种用法
- linux下svn升级
- 蓝桥杯 找素数 Java
- HashMap 使用 (二)
- Get free memory value
- MyISAM 与 InnoDB 的区别