标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1563 Accepted Submission(s): 549
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <set> using namespace std; const int MAXN=200005; vector<int> arc[MAXN]; set<int> node; int n,m,s; int vis[MAXN],d[MAXN]; void bfs(int src) { memset(vis,0,sizeof(vis)); memset(d,-1,sizeof(d)); queue<int> que; que.push(src); vis[src]=1; d[src]=0; node.erase(src); while(!que.empty()) { int u=que.front();que.pop(); vector<int> vec; for(set<int>::iterator it=node.begin();it!=node.end();it++) { int to=*it; if(!vis[to]&&!binary_search(arc[u].begin(),arc[u].end(),to)) { d[to]=d[u]+1; vis[to]=1; que.push(to); vec.push_back(to); } } for(int i=0;i<vec.size();i++) { node.erase(vec[i]); } } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); node.clear(); for(int i=1;i<=n;i++) { node.insert(i); arc[i].clear(); } for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); arc[u].push_back(v); arc[v].push_back(u); } for(int i=1;i<=n;i++) { sort(arc[i].begin(),arc[i].end()); } scanf("%d",&s); bfs(s); bool tag=false; for(int i=1;i<=n;i++) { if(i==s) continue; if(tag) printf(" "); tag=true; printf("%d",d[i]); } printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5874351.html