标签:个数 限制 spl tor pac 连接 mat 难度 oid
1 10 1 1 9 1 8 8 10 10 3 8 6 1 2 10 4 9 5 3 7
-1 1 10 10 9 8 3 1 1 8
思路:深度优先算法
#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <vector> using namespace std; int *a; vector<int> *v; int m; void DFS(int cur){ for (int i = 0 ; i < v[cur].size() ;i++) { if (a[v[cur][i]]!=0) { continue; } a[v[cur][i]] = cur; DFS(v[cur][i]); } } int main(){ int n; cin>>n; while (n--) { int cur; cin>>m>>cur; a = new int[m+1]; for (int z = 0; z < m+1 ; z++) { a[z] = 0; } v = new vector<int>[m+1]; int x,y; for (int i = 1 ; i < m ; i++) { cin>>x>>y; v[x].push_back(y); v[y].push_back(x); } DFS(cur); int k; for (k = 0; k < m-1 ;k++) { if (k+1==cur) { cout<<"-1 "; }else { cout<<a[k+1]<<" "; } } if (k+1==cur) { cout<<"-1"<<endl; } else { cout<<a[k+1]<<endl; } } return 0; }
标签:个数 限制 spl tor pac 连接 mat 难度 oid
原文地址:https://www.cnblogs.com/lyc94620/p/9289226.html