HDU 5876 Sparse Graph(補圖+BFS最短路)

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 617    Accepted Submission(s): 210


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 N1 other vertices.
 

Input
There are multiple test cases. The first line of input is an integer T(1T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2N200000) and M(0M20000). The following M lines each contains two distinct integers u,v(1u,vN) denoting an edge. And S (1SN) is given on the last line.
 

Output
For each of T test cases, print a single line consisting of N1 space separated integers, denoting shortest distances of the remaining N1 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,然後問在它的補圖中,點s到每個點的最短路是多長。 解題思路: 題意不難,但是第一次在樹上做BFS,感覺邊的關係不好維護,比賽時想到一個想法,但是不敢想,賽後寫出來了。 給每個點開一個vector,然後記錄跟這個點直接連接的點。然後以s爲起點進行bfs.
<span style="font-size:18px;"> #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<cmath>
 #include<set>
 #include<queue>
 using namespace std;
 const int maxn=200010;
 vector<int> v[maxn];
 int n,m,dis[maxn],s;
 void BFS()
 {
     set<int> s1,s2;
     set<int>::iterator it;
     queue<int> que;
     que.push(s);
     dis[s]=0;
     for(int i=1;i<=n;i++)
        if(i!=s) s1.insert(i);


     while(!que.empty())
     {
         int u=que.front();
         que.pop();
         int len=v[u].size();
         for(int i=0;i<len;i++)
         {
             int vv=v[u][i];
             if(!s1.count(vv))
                continue;
             s1.erase(vv);
             s2.insert(vv);
         }
         for(it=s1.begin();it!=s1.end();it++)
         {
             que.push(*it);
             dis[*it]=dis[u]+1;
         }
         s1.swap(s2);
         s2.clear();
     }
 }
 int main()
 {
     int t,a,b;
     scanf("%d",&t);
     while(t--)
     {
         scanf("%d %d",&n,&m);
         memset(dis,0x3f,sizeof(dis));
         for(int i=0;i<maxn;i++) v[i].clear();
         for(int i=0;i<m;i++)
         {
             scanf("%d %d",&a,&b);
             v[a].push_back(b);
             v[b].push_back(a);
         }
         scanf("%d",&s);
         BFS();
         for(int i=1;i<=n;i++)
         {
             if(i==s){
                if(i==n) printf("\n");
                continue;
             }
             printf("%d%c",dis[i],i==n? '\n' : ' ');
         }
     }
     return 0;
 }
</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章