标签:
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
原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=20
如果采用邻接矩阵建图,你懂的,进而想到链式前向星建图+BFS解决,当然用DFS也行。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn=100000+5; int n,s; struct Edge { int u,v,next; } edge[maxn<<1]; int head[maxn]; int cnt; bool vis[maxn]; int pre[maxn]; void Init() { cnt=0; for(int i=1; i<=n; i++) { vis[i]=false; head[i]=-1; pre[i]=-1; } } void addEdge(int u,int v) { edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void BFS() { vis[s]=true; pre[s]=-1; queue<int>q; q.push(s); while(!q.empty()) { int p=q.front(); q.pop(); for(int i=head[p]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(!vis[v]) { vis[v]=true; pre[v]=p; q.push(v); } } } } int main() { int T; //freopen("data/20.txt","r",stdin); cin>>T; while(T--) { cin>>n>>s; int x,y; Init(); for(int i=1; i<n; i++) { scanf("%d%d",&x,&y); addEdge(x,y); addEdge(y,x); } /** for(int i=1; i<=n; i++) { cout<<i<<":"; for(int j=head[i]; ~j; j=edge[j].next) { cout<<edge[j].v<<" "; } cout<<endl; } */ BFS(); for(int i=1; i<=n; i++) { if(i!=1) printf(" "); printf("%d",pre[i]); } cout<<endl; } return 0; }
#include <iostream> #include <stdio.h> #include <vector> using namespace std; vector<int>a[100005]; int b[100005],n,s; void dfs(int x,int y) { for (int i = 0; i <a[x].size(); i++) if (a[x][i] != y) dfs(a[x][i],b[a[x][i]]=x); return; } int main() { int M,x,y; scanf("%d",&M); while (M--) { scanf("%d%d",&n,&s); for (int i = 1; i <n; i++) { a[i].clear(); } for (int i = 1; i <n; i++) { scanf("%d%d",&x,&y); a[x].push_back(y); //相当二维数组 a[y].push_back(x); } b[s] = -1;//与S相等则为-1 dfs(s,-1); for (int i = 1; i <= n; i++) printf("%d ",b[i]); printf("\n"); } return 0; }
#include<iostream> #include<vector> #include<iterator> using namespace std; void MakeRoot(int search,int root,const vector<vector<int> >& g,vector<int> &result) { for(vector<int>::const_iterator it=g[search].begin();it!=g[search].end();++it) { if(result[*it]==0) { result[*it]=search; MakeRoot(*it,root,g,result); } } } int main() { int n; cin>>n; while(n--) { int m,a,b; cin>>m; int root; cin>>root; //m个点 vector<vector<int> > g(m+1,vector<int>()); for(int i=0;i!=m-1;i++) { cin>>a>>b; g[a].push_back(b); g[b].push_back(a); } vector<int> result(m+1); result[root]=-1; MakeRoot(root,root,g,result); copy(result.begin()+1,result.end(),ostream_iterator<int>(cout," ")); } }
用户:李泉,运行号:1470045
#include <stdio.h> #include <memory.h> int map[100005]; void Adjust(int currentCity) { int priorCity = map[currentCity]; if (priorCity != 0) { Adjust(priorCity); map[priorCity] = currentCity; } } int main() { int i, testNum, cityNum, startCity, cityA, cityB; scanf("%d", &testNum); while (testNum-- != 0) { scanf("%d%d", &cityNum, &startCity); memset(map, 0, sizeof(int)*cityNum + 1); for (i = 1; i < cityNum; i++) { scanf("%d%d", &cityA, &cityB); if (map[cityB] == 0) { map[cityB] = cityA; } else { Adjust(cityA); map[cityA] = cityB; } } Adjust(startCity); map[startCity] = - 1; for (i = 1; i < cityNum; i++) { printf("%d ", map[i]); } printf("%d\n", map[i]); } return 0; }
NYOJ 20 吝啬的国度 【BFS+链式前向星建图,Vector建图】
标签:
原文地址:http://blog.csdn.net/hurmishine/article/details/52301291