#include<iostream>  #include<vector>  #include<stack>  #include<queue>  #include<unordered_map>  #include<string>  #include<map>  #include<cstring>  #include<algorithm>  using namespace  std;     int main()      {                  int n;         cin>>n;         map<int,int> m;         for(int i=0;i<n;i++)         {             int a,b;             cin>>a>>b;             m[a]=b;         }         int qn;         cin>>qn;         vector<int> fir;         vector<int> sec;         for(int i=0;i<qn;i++)         {             int aa,bb;             cin>>aa>>bb;             fir.push_back(aa);             sec.push_back(bb);         }         vector<int> ans;         for(int i=0;i<qn;i++)         {             bool flag1=false;             bool flag2=false;             if(m.find(fir[i])==m.end()&&m.find(sec[i])==m.end())             {                 ans.push_back(0);                 break;             }             int temp=fir[i];             while(m.find(temp)!=m.end())             {                 if(m[temp]==sec[i])                 {                     flag1=true;                     ans.push_back(2);                      break;                 }                 else                 {                     temp=m[temp];                 }             }             int temp2=sec[i];             while(m.find(temp2)!=m.end())             {                 if(m[temp2]==fir[i])                 {                     flag2=true;                     ans.push_back(1);                      break;                 }                 else                 {                     temp2=m[temp2];                 }             }             if(!flag1&&!flag2)                ans.push_back(0);         }         int size2=ans.size();         for(int i=0;i<size2;i++)         {             cout<<ans[i]<<endl;         }                                                                          return 0;              }                     
点赞 1

相关推荐

11-15 18:39
已编辑
西安交通大学 Java
全村最靓的仔仔:卧槽,佬啥bg呢,本也是西交么
点赞 评论 收藏
分享
11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
牛客网
牛客企业服务