寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

Aizu Online Judge in C++ #Volume20 : 2011 - Gather the Maps!

Aizu Online Judge(AOJ)の過去問から、その提出コードの解答例です。

・問題 "Gather the Maps!"
https://onlinejudge.u-aizu.ac.jp/problems/2011
・地図を集めよう!
僕が作成、提出したコードは、以下のとおりです。

・Aizu Online Judge in C++ #Volume20 - 2011 : Gather the Maps!
/*
Aizu Online Judge in C++ #Volume20 - 2011 : Gather the Maps!
 https://onlinejudge.u-aizu.ac.jp/problems/2011
 提出コードの解答例
 https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(void){
    ll n;
    while(cin>>n,n){
        bool tf[50][30]={};
        ll m,g[50]={};
        for(ll i=0;i<n;i++){
            cin>>m;
            for(ll x,j=0;j<m;j++){
                cin>>x,x--;
                tf[i][x]=1;
                g[x]|=1LL<<i;
            }
        }
        ll ans=-1,a=1,v[50]={};
        for(ll i=0;i<30 && a;i++){
            for(ll j=0;j<n;j++){
                if(tf[j][i]){
                    for(ll k=0;k<n;k++){
                        if(tf[k][i]){
                            v[j]|=v[k]|g[i];
                        }
                    }
                }
                if(v[j]+1==(1LL<<n)){
                    a=0;
                    ans=i+1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

設問の出典は、プログラミング問題のオンライン採点システム「Aizu Online Judge(AOJ)」です。
http://judge.u-aizu.ac.jp/onlinejudge/