吝啬的国度
来源:互联网 发布:网络隐私权的法律规定 编辑:程序博客网 时间:2024/06/05 19:55
就是暴力搜索即可
dfs+链式前向星
题目链接<-点击
#include<cmath>#include<iostream>#include<cstdio>#include<cstring>using namespace std;int pre[100005],head[100005];int cnt=0;struct edge{ int v,next;}edge[100005*2];void add_edge(int u,int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void dfs(int s){ for(int i=head[s];i!=-1;i=edge[i].next) { int to=edge[i].v; if(pre[to]) continue; pre[to]=s; dfs(to); }}int main (){ int t; cin>>t; while(t--) { int n,m; cin>>n>>m; int i; cnt=0; memset(head,-1,sizeof(head)); memset(pre,0,sizeof(pre)); pre[m]=-1; for(i=1;i<n;i++) { int a,b; scanf("%d%d",&a,&b); add_edge(a,b); add_edge(b,a); } dfs(m); for(i=1;i<n;i++) { printf("%d ",pre[i]); } printf("%d\n",pre[n]); }}
阅读全文
0 0
- nyoj_20 吝啬的国度
- 吝啬的国度
- noj 吝啬的国度
- 吝啬的国度
- NYOJ 吝啬的国度
- NYOJ 吝啬的国度
- 吝啬的国度
- nyoj20(吝啬的国度)
- 吝啬的国度
- 吝啬的国度
- 【搜索】吝啬的国度
- 吝啬的国度
- NY20 吝啬的国度
- NYOJ20-吝啬的国度
- 吝啬的国度
- 吝啬的国度
- 吝啬的国度
- 吝啬的国度 nyoj
- 算法之二叉树各种遍历
- csu1808 bfs +优先队列
- 2017年8月23日训练日记
- HDU6165(深搜+暴力)
- 再看框架
- 吝啬的国度
- 为什么中国人在中学阶段很厉害,后期表现却不如外国人?
- 选择器区分奇偶
- Linux之ls 命令
- g2o拟合曲线
- RE:JAVA学习-日期操作+步入集合
- 测试websocket接口
- javaAgent简单使用
- 自然语言处理学习(三):上下文无关文法和概率上下文无关文法