CodeForces 993B Open Communication(STL 模拟)

 

https://codeforces.com/problemset/problem/993/b

 

 

 

这题不难,暴力就能过,主要是题意太难懂了 

 

题意:

现在有两个人,每个人手中有一对数,第一个人手中的数是n组数中的一对,第二个人手中的数是m组数中的一对

如果这两组数中只有一个数字相等,这该数为共享数字,怎样输出看思路吧

 

思路:

其实就是n对和m对数中,找共享数字,直接看样例吧:

在第一示例中,第一参与者通信对(1,2)和(3,4),第二参与者通信对(1,5),(3,4)。因为我们知道,他们收到的实际对共享一个数字,这不可能是他们都有(3,4)。因此,第一个参与者有(1,2),第二个参与者有(1,5),此时您已经知道共享号码是1。

在第二个例子中,第一个参与者有(1,2),第二个有(1,5),或者第一个有(3,4),第二个有(6,4)。在第一种情况下,他们都知道共享号码是1,在第二种情况下,他们都知道共享号码是4。你没有足够的信息来区分1和4。

在第三种情况下,如果第一个参与者被给予(1,2),他们不知道共享号码是什么,因为从他们的角度来看,第二个参与者可能被给予(1,3),在这种情况下共享号码是1,或者(2,3),在这种情况下共享号码是2。虽然第二个参与者确实知道数字,但您和第一个参与者都不知道,因此输出为-1。

 

比如 第一个人有一对数(a,b),第二个人有一对数(c,d)假设没对数第一个数均小于第二个数,题目保证a!=b&&c!=d;

如果出现a==c&&b==d,则直接跳过这组去判断下一组;

如果a==c&&b!=d || a==d&&b!=c ,则a为一个共享数字

同理b==d&&a!=c || b==d&&a!=d ,b为一个共享数字

 

如果在比较时,一组数(x,y)中x和y均为共享数字,则输出-1;

否则看共享数字的个数,如果只有一个共享数字,则输出这个共享数字,否则输出0;

 

注意:两个人都要遍历对方一遍

 

代码如下:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <sstream>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
 15 const int mod=1e9+7;
 16 //const double PI=acos(-1);
 17 #define Bug cout<<"---------------------"<<endl
 18 const int maxn=1e5+10;
 19 using namespace std;
 20 
 21 vector<pair<int,int> > vt1;//第一个人的数 
 22 vector<pair<int,int> > vt2;//第二个人的数 
 23 
 24 int main()
 25 {
 26     int n,m;
 27     scanf("%d %d",&n,&m);
 28     //输入
 29     for(int i=1;i<=n;i++)
 30     {
 31         int x,y;
 32         scanf("%d %d",&x,&y);
 33         vt1.push_back(make_pair(min(x,y),max(x,y)));//pair的first中为小的数 
 34     }
 35     for(int i=1;i<=m;i++)
 36     {
 37         int x,y;
 38         scanf("%d %d",&x,&y);
 39         vt2.push_back(make_pair(min(x,y),max(x,y)));
 40     }
 41     int f1[10]={0};
 42     int tag=0;//判断一组中两个数是否均为共享数字  
 43     for(vector<pair<int,int> >::iterator it1=vt1.begin();it1!=vt1.end();it1++)//遍历第一个人 
 44     {
 45         int a=it1->first;//第一个人这对数中的较小值 
 46         int b=it1->second;//第一个人这对数中的较大值 
 47         int f[2]={0};//看该组两个数字是否均为共享数字
 48         for(vector<pair<int,int> >::iterator it2=vt2.begin();it2!=vt2.end();it2++)
 49         {
 50             int c=it2->first;//第二个人这对数中的较小值 
 51             int d=it2->second;//第二个人这对数中的较大值 
 52             if(a==c&&b==d)
 53                 continue;
 54             if(a==c&&b!=d)
 55             {
 56                 f1[a]++;//共享数字a出现次数增加 
 57                 f[0]=1;//第一个数为共享数字 
 58             }
 59             else if(b==c)
 60             {
 61                 f1[b]++;//共享数字b出现次数增加
 62                 f[1]=1;//第二个数为共享数字
 63             }
 64             else if(a==d)
 65             {
 66                 f1[a]++;//共享数字a出现次数增加 
 67                 f[0]=1;//第一个数为共享数字 
 68             }
 69             else if(b==d)
 70             {
 71                 f1[b]++;//共享数字b出现次数增加
 72                 f[1]=1;//第二个数为共享数字
 73             }
 74         }
 75         if(f[0]&&f[1])//如果第一个人该组数字两个数均为共享数字,结果为-1 
 76             tag=1;
 77     }
 78     int f2[10]={0};//其实没用 
 79     for(vector<pair<int,int> >::iterator it1=vt2.begin();it1!=vt2.end();it1++)//再从第二个人跑一遍
 80     {
 81         int a=it1->first;
 82         int b=it1->second;
 83         int f[2]={0};
 84         for(vector<pair<int,int> >::iterator it2=vt1.begin();it2!=vt1.end();it2++)
 85         {
 86             int c=it2->first;
 87             int d=it2->second;
 88             if(a==c&&b==d)
 89                 continue;
 90             if(a==c&&b!=d)
 91             {
 92                 f2[a]++;
 93                 f[0]=1;
 94             }
 95             else if(b==c)
 96             {
 97                 f2[b]++;
 98                 f[1]=1;
 99             }    
100             else if(a==d)
101             {
102                 f2[a]++;
103                 f[0]=1;
104             }
105             else if(b==d)
106             {
107                 f2[b]++;
108                 f[1]=1;    
109             }
110         }
111         if(f[0]&&f[1])
112             tag=1;
113     }
114     int num=0;//共享数字的个数 
115     int ans=0;//如果共享数字只有一个,ans则为该共享数字 
116     for(int i=1;i<=9;i++)
117     {
118         if(f1[i])
119         {
120             num++;
121             ans=i;
122         }
123     }
124     if(tag)
125         printf("-1\n");
126     else
127     {
128         if(num==1)//共享数字有且只有一个 
129             printf("%d\n",ans);
130         else//共享数字有多个
131             printf("0\n"); 
132     }
133     return 0;
134 }

 

posted @ 2019-10-20 17:05  jiamian22  阅读(325)  评论(0编辑  收藏  举报