1
1

yukicoder No.26 シャッフルゲーム 解説

Last updated at Posted at 2024-07-23

問題文

解説

問題文の通りに実装する問題。
$ans$という変数を用いる。
$ans$=今の段階で〇印がついているコップの番号
もし$ans=p_i$なら$ans$を$q_i$に変更する。
もし$ans=q_i$なら$ans$を$p_i$に変更する。
もし$ans$がそのどちらでもないならそのまま。
これを繰り返していけば解は求まる。

C++での解法

#include <bits/stdc++.h>
using namespace std;

int main(){
  int n,m,ans;cin>>n>>m;
  ans=n;
  vector<int> p(m),q(m);
  for(int i=0;i<m;i++)cin>>p[i]>>q[i];
  for(int i=0;i<m;i++){
    if(ans==p[i])ans=q[i];
    else if(ans==q[i])ans=p[i];
  }
  cout<<ans<<endl;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1