Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define rep(i,k,n) for(long long int i=k;i<n;i++)
- #define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
- #define read freopen("input.txt","r",stdin)
- #define write freopen("output.txt","w",stdout)
- #define D(x) cout << '>' << #x << ':' << x << endl
- #define DD(x,y) cout << '>' << #x << ':' << x << ' ' << #y << ':' << y << endl
- #define DDD(x,y,z) cout << '>' << #x << ':' << x << ' ' << #y << ':' << y << ' ' << #z << ':' << z << endl
- #define PI acos(-1)
- #define MP(x, y) make_pair(x, y)
- #define PB(x) push_back(x)
- #define ALL(p) p.begin(),p.end()
- #define CLR(p) memset(p, 0, sizeof(p))
- #define MEM(a, b) memset(a, (b), sizeof(a))
- #define ff first
- #define ss second
- #define sf scanf
- #define pf printf
- #define PII pair<int, int>
- #define ll long long int
- #define ull unsigned long long int
- inline int two(int n) { return 1 << n; }
- inline int test(int n, int b) { return (n>>b)&1; }
- inline void set_bit(int & n, int b) { n |= two(b); }
- inline void unset_bit(int & n, int b) { n &= ~two(b); }
- inline int last_bit(int n) { return n & (-n); }
- inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
- template < class T > inline T gcd(T a, T b) {while(b) { a %= b; swap(a, b); } return a;}
- template < class T > inline T bigmod(T p, T e, T M){
- ll ret = 1;
- for(; e > 0; e >>= 1){ if(e & 1) ret = (ret * p) % M; p = (p * p) % M; }
- return (T)ret;
- }
- template < class T > inline T power(T a, T n) {
- if(n==0) return 1;
- if(n==1) return a;
- T R = power(a,n/2);
- return (n%2==0) ? R*R : R*a*R;
- }
- int fx4[] = {0, 0, -1, +1};
- int fy4[] = {+1, -1, 0, 0};
- int fx8[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
- int fy8[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
- int fx8Knight[] = {+2, +2, +1, -1, -2, -2, -1, +1};
- int fy8Knight[] = {+1, -1, -2, -2, -1, +1, +2, +2};
- const int MAXN = 107;
- map<string, vector<string>> adj;
- map<string, bool> vis;
- map<string, int> dist;
- int N, E;
- void bfs(string src)
- {
- queue<string> Q;
- Q.push(src);
- vis[src] = true;
- dist[src] = 0;
- while(!Q.empty())
- {
- string king = Q.front();
- Q.pop();
- cout << king << ' ';
- for(auto v : adj[king])
- {
- if(!vis[v])
- {
- vis[v] = true;
- dist[v] = dist[king] + 1;
- Q.push(v);
- }
- }
- }
- cout << "\n\n";
- }
- int main() {
- #ifndef ONLINE_JUDGE
- read;
- write;
- #endif
- fast;
- cin >> N >> E;
- for (int i = 0; i < E; i++)
- {
- string u, v;
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- bfs("shiary");
- cout << dist["nawreen"] << "\n\n";
- for(auto i : adj)
- {
- cout << i.first << ": ";
- for(auto j : i.second)
- {
- cout << j << ' ';
- }
- cout << endl;
- }
- return 0;
- }
- // key value
- // adj er ekta element, i = {shairy, {ruhan, sajid, abed}}
- // i.first = shiary
- // i.second.size() = 3;
- // i.second[0] = ruhan
- // i.second[1] = sajid
- // i.second[2] = abed
- /*
- input:
- 7 7
- shiary ruhan
- shiary sajid
- ruhan mappy
- ruhan anika
- anika nawreen
- abed shiary
- mappy nawreen
- output:
- shiary ruhan sajid abed mappy anika nawreen
- 3
- abed: shiary
- anika: ruhan nawreen
- mappy: ruhan nawreen
- nawreen: anika mappy
- ruhan: shiary mappy anika
- sajid: shiary
- shiary: ruhan sajid abed
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement