#include<bits/stdc++.h>
#define fr(i,m) for(int i=0;i<m;i++)
using namespace std;
vector <int> woman[501];
vector <int> man[501];
int check[501];
int main()
{
int N;
scanf("%d",&N);
int who,whom;
queue <int> Circle;
for(int i=1; i<=N; i++)
{
woman[i].clear();
man[i].clear();
check[i]=0;
Circle.push(i);
}
for(int i=1; i<=N; i++)
{
scanf("%d",&who);
for(int j=1; j<=N; j++)
{
scanf("%d",&whom);
woman[who].push_back(whom);
}
}
for(int i=1; i<=N; i++)
{
scanf("%d",&who);
for(int j=1; j<=N; j++)
{
scanf("%d",&whom);
man[who].push_back(whom);
}
}
while(Circle.size()!=0)
{
int person=Circle.front();
Circle.pop();
for(int i=0; i<man[person].size(); i++)
{
int person_like=man[person][i];
if(check[person_like]==0)
{
check[person_like]=person;//both are free
break;
}
else
{
cout<<person<<"--> "<<person_like<<"\n";
int flag=1;
for(int j=0; j<woman[person_like].size(); j++)
{
if(woman[person_like][j]==check[person_like])//woman likes her husband much
break;
else if(woman[person_like][j]==person)
{
Circle.push(check[person_like]);//making free her present husband
check[person_like]=person;//get married with new person
flag=0;
}
}
if(flag==0)
break;
}
}
}
for(int i=1; i<=N; i++)
cout<<check[i]<<" "<<i<<"\n";
return 0;
}
#define fr(i,m) for(int i=0;i<m;i++)
using namespace std;
vector <int> woman[501];
vector <int> man[501];
int check[501];
int main()
{
int N;
scanf("%d",&N);
int who,whom;
queue <int> Circle;
for(int i=1; i<=N; i++)
{
woman[i].clear();
man[i].clear();
check[i]=0;
Circle.push(i);
}
for(int i=1; i<=N; i++)
{
scanf("%d",&who);
for(int j=1; j<=N; j++)
{
scanf("%d",&whom);
woman[who].push_back(whom);
}
}
for(int i=1; i<=N; i++)
{
scanf("%d",&who);
for(int j=1; j<=N; j++)
{
scanf("%d",&whom);
man[who].push_back(whom);
}
}
while(Circle.size()!=0)
{
int person=Circle.front();
Circle.pop();
for(int i=0; i<man[person].size(); i++)
{
int person_like=man[person][i];
if(check[person_like]==0)
{
check[person_like]=person;//both are free
break;
}
else
{
cout<<person<<"--> "<<person_like<<"\n";
int flag=1;
for(int j=0; j<woman[person_like].size(); j++)
{
if(woman[person_like][j]==check[person_like])//woman likes her husband much
break;
else if(woman[person_like][j]==person)
{
Circle.push(check[person_like]);//making free her present husband
check[person_like]=person;//get married with new person
flag=0;
}
}
if(flag==0)
break;
}
}
}
for(int i=1; i<=N; i++)
cout<<check[i]<<" "<<i<<"\n";
return 0;
}
1400 NO
#include<bits/stdc++.h>
#define fr(i,m) for(int i=0;i<m;i++)
using namespace std;
vector <int> woman[501];
vector <int> man[501];
int check[501];
int main()
{
int test,ll=1;
scanf("%d",&test);
while(test--)
{
int N;
scanf("%d",&N);
int who,whom;
queue <int> Circle;
for(int i=1; i<=N*2; i++)
{
woman[i].clear();
man[i].clear();
check[i]=0;
Circle.push(i);
}
for(int i=1; i<=N; i++)
{
for(int j=1; j<=N; j++)
{
scanf("%d",&whom);
man[i].push_back(whom);
}
}
for(int i=1; i<=N; i++)
{
for(int j=1; j<=N; j++)
{
scanf("%d",&whom);
woman[i+N].push_back(whom);
}
}
while(Circle.size()!=0)
{
int person=Circle.front();
Circle.pop();
for(int i=0; i<man[person].size(); i++)
{
int person_like=man[person][i];
if(check[person_like]==0)
{
check[person_like]=person;
break;
}
else
{
int flag=1;
for(int j=0; j<woman[person_like].size(); j++)
{
if(woman[person_like][j]==check[person_like])
break;
else if(woman[person_like][j]==person)
{
Circle.push(check[person_like]);
check[person_like]=person;
flag=0;
}
}
if(flag==0)
break;
}
}
}
printf("Case %d:",ll++);
for(int i=N+1; i<=N*2; i++)
printf(" (%d %d)",check[i],i);
cout<<"\n";
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন