Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector <vector <int> > lista;
- bool odwiedzony[200005];
- int odl[200005];
- int m, n, you, grupy=1;
- void cler(){
- for(int i=0; i<n; i++){
- lista[i].clear();
- lista[i].shrink_to_fit();
- }
- lista.shrink_to_fit();
- }
- void input(){
- cin>>n>>m;
- lista.resize(n+1);
- for(int i=0; i<m; i++){
- int a, b;
- cin>>a>>b;
- lista[a].push_back(b);
- lista[b].push_back(a);
- }
- cin>>you;
- }
- void bfs_you(){
- queue <int> kolejka;
- kolejka.push(lista[you]);
- odl[you]=0;
- while(!kolejka.empty()){
- odwiedzony[kolejka.front()]=true;
- for(int i=0; i<lista[kolejka.front()].size(); i++){
- if(!odwiedzony[lista[kolejka.front()][i]]){
- odl[lista[kolejka.front()][i]]=odl[lista[kolejka.front()]]+1;
- kolejka.push(lista[kolejka.front()][i]);
- }
- }
- kolejka.pop();
- }
- }
- void bfs_grupy{
- for(int i=1; i<=n; i++){
- if(!odwiedzony[i]){
- queue <int> kolejka;
- kolejka.push(lista[i]);
- while(!kolejka.empty()){
- odwiedzony[kolejka.front()]=true;
- for(int j=0; j<lista[kolejka.front()].size(); j++){
- if(!odwiedzony[lista[kolejka.front()][j]]){
- kolejka.push(lista[kolejka.front()][j]);
- }
- kolejka.pop();
- }
- grupy++;
- }
- }
- }
- }
- void test(){
- input();
- bfs();
- cler();
- }
- int main(){
- int z;
- cin>>z;
- for(int i=0; i<z; i++){
- test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement