---恢复内容开始---
Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2590 Accepted Submission(s): 902
Problem Description
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G.
Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H, you are required to compute the shortest distances from S to all N?1 other vertices.
Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H, you are required to compute the shortest distances from S to all N?1 other vertices.
Input
There are multiple test cases. The first line of input is an integer T(1≤T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000) and M(0≤M≤20000). The following M lines each contains two distinct integers u,v(1≤u,v≤N) denoting an edge. And S (1≤S≤N) is given on the last line.
Output
For each of T test cases, print a single line consisting of N?1 space separated integers, denoting shortest distances of the remaining N?1 vertices from S (if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number.
Sample Input
1
2 0
1
Sample Output
1
题意 求源点 s 在补图中到其它点的最短路长
解析 因为 给的边比较少 点比较多 补图就是一个非常稠密的图 我们不能建补图 迪杰斯特拉跑一边 会超时的 我们注意到 边权都为1 我们可以直接BFS一层一层求最短路
每个点都入队一次 然后取队顶在未被访问过的点中找在原图中不与其相邻的点 加入队列 。因为在原图中不与点s相连的点 在补图中最短路都为1 待访问的不超过m个
所以bfs是可行的 但是我们在找 与 当前队顶其不相邻的点 用数组标记 花费时间很大(每次遍历n会超时)我们用set存一下 就好了 只留下待访问的点 时间复杂度就降下来了
set AC代码
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <queue> 8 #include <map> 9 #include <set> 10 #include <vector> 11 using namespace std; 12 const int maxn=2e5+10; 13 int n,m,s; 14 vector<int> g[maxn]; 15 int ans[maxn],vis[maxn]; 16 set<int> a,b; 17 void bfs(int x) 18 { 19 for(int i=1;i<=n;i++) 20 if(s!=i) 21 a.insert(i); 22 queue<int> q; 23 q.push(x); 24 while(!q.empty()) 25 { 26 int u=q.front(); 27 q.pop(); 28 for(int i=0;i<g[u].size();i++) 29 { 30 int v=g[u][i]; 31 if(a.count(v)) 32 { 33 b.insert(v); 34 a.erase(v); 35 } 36 } 37 set<int>::iterator it; 38 for(it=a.begin();it!=a.end();it++) 39 { 40 q.push(*it); 41 ans[*it]=ans[u]+1; 42 } 43 a.swap(b); 44 b.clear(); 45 } 46 } 47 int main() 48 { 49 int t; 50 cin>>t; 51 while(t--) 52 { 53 cin>>n>>m; 54 a.clear(),b.clear(); 55 for(int i=1;i<=n;i++) 56 { 57 g[i].clear(); 58 } 59 memset(ans,-1,sizeof(ans)); 60 for(int i=0;i<m;i++) 61 { 62 int u,v; 63 cin>>u>>v; 64 g[u].push_back(v); 65 g[v].push_back(u); 66 } 67 cin>>s; 68 ans[s]=0; 69 // set<int>::iterator j; 70 // for(j=a.begin();j!=a.end();j++) 71 // cout<<*j<<endl; 72 bfs(s); 73 if(s!=n) 74 for(int i=1;i<=n;i++) 75 { 76 if(i!=s) 77 { 78 if(i!=n) 79 cout<<ans[i]<<" "; 80 else 81 cout<<ans[n]<<endl; 82 } 83 } 84 else 85 for(int i=1;i<=n-1;i++) 86 { 87 if(i==n-1) 88 cout<<ans[n-1]<<endl; 89 else 90 cout<<ans[i]<<" "; 91 } 92 } 93 return 0; 94 }
数组超时代码
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <queue> 8 #include <map> 9 #include <vector> 10 using namespace std; 11 const int maxn=2e5+10; 12 int n,m,s; 13 vector<int> g[maxn]; 14 int ans[maxn],vis[maxn],a[maxn]; 15 void bfs(int x) 16 { 17 vis[x]=1; 18 queue<int> q; 19 q.push(x); 20 int cnt=1; 21 while(!q.empty()) 22 { 23 int u=q.front(); 24 q.pop(); 25 for(int i=0;i<g[u].size();i++) 26 { 27 int v=g[u][i]; 28 if(cnt%2==1) 29 a[v]=0; 30 else 31 a[v]=1; 32 } 33 for(int i=1;i<=n;i++) 34 { 35 if(cnt%2==1&&a[i]==1&&!vis[i]) 36 { 37 ans[i]=ans[u]+1; 38 q.push(i); 39 vis[i]=1; 40 } 41 if(cnt%2==0&&a[i]==0&&!vis[i]) 42 { 43 ans[i]=ans[u]+1; 44 q.push(i); 45 vis[i]=1; 46 } 47 } 48 cnt++; 49 } 50 } 51 int main() 52 { 53 int t; 54 cin>>t; 55 while(t--) 56 { 57 cin>>n>>m; 58 for(int i=1;i<=n;i++) 59 g[i].clear(),a[i]=1; 60 memset(ans,-1,sizeof(ans)); 61 memset(vis,0,sizeof(vis)); 62 for(int i=0;i<m;i++) 63 { 64 int u,v; 65 cin>>u>>v; 66 g[u].push_back(v); 67 g[v].push_back(u); 68 } 69 cin>>s;ans[s]=0; 70 bfs(s); 71 for(int i=1;i<=n;i++) 72 { 73 if(i!=s) 74 cout<<ans[i]<<" "; 75 } 76 cout<<endl; 77 } 78 return 0; 79 }