Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a to b
- //
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
- typedef pair<ll,ll>pll;
- typedef pair<ll,pair<ll,ll>>plll;
- #define fi first
- #define se second
- #define pb push_back
- const int mod = 1e9+7;
- #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
- #define eb emplace_back
- int X[] = {-1,0,1,1,1,0,-1,-1};
- int Y[] = {-1,-1,-1,0,1,1,1,0};
- //int X[] = {-1,1,0,0};
- //int Y[] = {0,0,-1,1};
- char a[510][510];
- bool check(ll i,ll j)
- {
- ll n,m;
- if(i>=0 and j>=0 and i<n and j<m)
- return true;
- else
- return false;
- }
- bool iset(ll n, ll k)
- {
- if (n & (1ll << k))
- return true;
- else
- return false;
- }
- const int N=2e5+100;
- vector<ll>adj[N+1];
- ll n,m;
- ll u,v;
- bool f=true;
- bool vis[N];
- void bfs(ll s){
- queue<ll>q;
- q.push(s);
- while(!q.empty()){
- ll x=q.front();
- cout<<x<<endl;
- vis[x]=true;
- q.pop();
- for(ll i=0;i<adj[x].size();i++){
- if(!vis[adj[x][i]]){
- q.push(adj[x][i]);
- vis[adj[x][i]]=true;
- }
- }
- }
- }
- int main()
- {
- fastread();
- cin>>n>>m;
- for(ll i=0;i<m;i++){
- cin>>u>>v;
- adj[u].pb(v);
- adj[v].pb(u);
- }
- for(ll i=0;i<n;i++){
- cout<<"node no : "<<i<<" -->";
- for(ll j=0;j<adj[i].size();j++){
- cout<<adj[i][j]<<" ";
- }cout<<endl;
- }
- while(1){
- cin>>u>>v;
- bfs(u);
- if(vis[v])cout<<"yes"<<endl;
- else cout<<"no"<<endl;
- for(ll i=0;i<=N;i++)vis[i]=false;
- }
- }
- /*
- */
- ================================
- a to b with query
- https://judge.u-aizu.ac.jp/onlinejudge/signin.jsp
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
- typedef pair<ll,ll>pll;
- typedef pair<ll,pair<ll,ll>>plll;
- #define fi first
- #define se second
- #define pb push_back
- const int mod = 1e9+7;
- #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
- #define eb emplace_back
- int X[] = {-1,0,1,1,1,0,-1,-1};
- int Y[] = {-1,-1,-1,0,1,1,1,0};
- //int X[] = {-1,1,0,0};
- //int Y[] = {0,0,-1,1};
- char a[510][510];
- bool check(ll i,ll j)
- {
- ll n,m;
- if(i>=0 and j>=0 and i<n and j<m)
- return true;
- else
- return false;
- }
- bool iset(ll n, ll k)
- {
- if (n & (1ll << k))
- return true;
- else
- return false;
- }
- const int N=1e6+10;
- vector<ll>adj[N+1];
- ll p[N+10];
- ll n,m;
- ll u,v;
- bool f=true;
- bool vis[N];
- void bfs(ll s){
- queue<ll>q;
- q.push(s);
- while(!q.empty()){
- ll x=q.front();
- //cout<<x<<endl;
- vis[x]=true;
- q.pop();
- for(ll i=0;i<adj[x].size();i++){
- if(!vis[adj[x][i]]){
- // cout<<"in "<<x<<" "<<adj[x][i]<<" "<<p[x]<<endl;
- q.push(adj[x][i]);
- p[adj[x][i]]=p[x];
- // cout<<p[adj[x][i]]<<endl;
- vis[adj[x][i]]=true;
- }
- }
- }
- }
- int main()
- {
- fastread();
- for(ll i=0;i<=N;i++){
- p[i]=i;
- }
- cin>>n>>m;
- for(ll i=0;i<m;i++){
- cin>>u>>v;
- adj[u].pb(v);
- adj[v].pb(u);
- }
- for(ll i=0;i<n;i++){
- // cout<<"node no : "<<i<<" -->";
- for(ll j=0;j<adj[i].size();j++){
- // cout<<adj[i][j]<<" ";
- }//cout<<endl;
- }
- for(ll i=0;i<n;i++){
- if(!vis[i])bfs(i);
- }
- for(ll i=0;i<n;i++){
- // cout<<i<<" "<<p[i]<<endl;
- }
- ll q;
- cin>>q;
- while(q--){
- cin>>u>>v;
- if(p[u]==p[v])cout<<"yes"<<endl;
- else cout<<"no"<<endl;
- //for(ll i=0;i<=N;i++)vis[i]=false;
- }
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement