YEZAELP

PROG-1050: Chemical Reactant

Jun 19th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using pii=pair<int,int>;
  4. int main(){
  5.  
  6.     int n,p;
  7.     scanf("%d%d",&n,&p);
  8.     vector <int> g[n+1];
  9.     map < int , vector<int> > mp;
  10.     for(int i=1;i<=n;i++){
  11.         for(int j=0;j<p;j++){
  12.             int x;
  13.             scanf("%d",&x);
  14.             g[i].push_back(x);
  15.             if(j==0) mp[x].push_back(i);
  16.         }
  17.     }
  18.  
  19.     int product[p];
  20.     for(int j=0;j<p;j++) {
  21.         scanf("%d",&product[j]);
  22.     }
  23.  
  24.     bool found;
  25.     for(auto i:mp[product[0]]){
  26.         found=true;
  27.         for(int j=0;j<p;j++){
  28.             if(product[j]!=g[i][j]) {
  29.                 found=false;
  30.                 break;
  31.             }
  32.         }
  33.         if(found){
  34.             printf("%d",i);
  35.             return 0;
  36.         }
  37.     }
  38.  
  39.     for(int k=1;k<=n;k++){
  40.         int x;
  41.         x=product[0]-g[k][0];
  42.         for(auto i:mp[x]){
  43.             found=true;
  44.             for(int j=0;j<p;j++) {
  45.                 if(g[k][j]+g[i][j]!=product[j]){
  46.                     found=false;
  47.                     break;
  48.                 }
  49.             }
  50.             if(found && k!=i) {
  51.                 if(k<i) printf("%d %d",k,i);
  52.                 else printf("%d %d",i,k);
  53.                 return 0;
  54.             }
  55.         }
  56.     }
  57.  
  58.     printf("NO");
  59.  
  60.     return 0;
  61. }
Add Comment
Please, Sign In to add comment