結果
問題 | No.177 制作進行の宮森あおいです! |
ユーザー |
![]() |
提出日時 | 2015-04-04 00:25:04 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 9,210 bytes |
コンパイル時間 | 1,550 ms |
コンパイル使用メモリ | 136,620 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-04 01:40:25 |
合計ジャッジ時間 | 2,144 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 13 |
ソースコード
#include <cassert>// c#include <iostream>// io#include <iomanip>#include <fstream>#include <sstream>#include <vector>// container#include <map>#include <set>#include <queue>#include <bitset>#include <stack>#include <algorithm>// other#include <complex>#include <numeric>#include <functional>#include <random>#include <regex>using namespace std;typedef long long ll;typedef unsigned long long ull;#define ALL(c) (begin(c)),(end(c))#define REP(i,n) FOR(i,0,n)#define REPr(i,n) FORr(i,0,n)#define FOR(i,l,r) for(int i=(int)(l);i<(int)(r);++i)#define FORr(i,l,r) for(int i=(int)(r)-1;i>=(int)(l);--i)#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)#define IN(l,v,r) ((l)<=(v) && (v)<(r))#define UNIQUE(v) v.erase(unique(ALL(v)),v.end())//debug#define DUMP(x) cerr << #x << " = " << (x)#define LINE() cerr<< " (L" << __LINE__ << ")"class range {private:struct Iter{int v;int operator*(){return v;}bool operator!=(Iter& itr) {return v < itr.v;}void operator++() {++v;}};Iter i, n;public:range(int n) : i({0}), n({n}) {}range(int i, int n) : i({i}), n({n}) {}Iter& begin() {return i;}Iter& end() {return n;}};//inputtemplate<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){is>>p.first>>p.second;return is;}template<typename T1> istream& operator >> (istream& is,tuple<T1>& t){is >> get<0>(t);return is;}template<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){is >> get<0>(t) >> get<1>(t);return is;}template<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){is >>get<0>(t)>>get<1>(t)>>get<2>(t);return is;}template<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);return is;}template<typename T1,typename T2,typename T3,typename T4,typename T5> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5>& t){is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t);return is;}template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5,T6>&t){is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t);return is;}template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5,T6,T7>& t){is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);return is;}template<typename T> istream& operator >> (istream& is,vector<T>& as){REP(i,as.size())is >>as[i];return is;}//outputtemplate<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){os<<p.first<<" "<<p.second;return os;}template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){os << get<0>(t);return os;}template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){os << get<0>(t)<<" "<<get<1>(t);return os;}template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);return os;}template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);return os;}template<typename T1,typename T2,typename T3,typename T4,typename T5> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t);return os;}template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6>&t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t);return os;}template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6,T7>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t)<<" "<<get<6>(t);return os;}template<typename T> ostream& operator << (ostream& os, const vector<T>& as){REP(i,as.size()){if(i!=0)os<<" "; os<<as[i];}return os;}template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){REP(i,as.size()){if(i!=0)os<<endl; os<<as[i];}return os;}//inputchar tmp[1000];#define nextInt(n) scanf("%d",&n)#define nextLong(n) scanf("%lld",&n) //I64d#define nextDouble(n) scanf("%lf",&n)#define nextChar(n) scanf("%c",&n)#define nextString(n) scanf("%s",tmp);n=tmp// valuestemplate<typename T> T INF(){assert(false);};template<> int INF<int>(){return 1<<28;};template<> ll INF<ll>(){return 1LL<<58;};template<> double INF<double>(){return 1e16;};template<> long double INF<long double>(){return 1e16;};template<class T> T EPS(){assert(false);};template<> int EPS<int>(){return 1;};template<> ll EPS<ll>(){return 1LL;};template<> double EPS<double>(){return 1e-8;};template<> long double EPS<long double>(){return 1e-8;};template<typename T,typename U> T pmod(T v,U M){return (v%M+M)%M;}class MaximamFlow{private:typedef int Flow;struct Edge{int to;Flow icap,cap;};typedef vector<vector<Edge>> Graph;//iter 次に調べる時の開始位置 距離vector<int> iter,dist;//sからのbfs距離void bfs_dist(int s){fill(ALL(dist),-1); dist[s]=0;queue<int> que; que.push(s);while(!que.empty()){int v=que.front();que.pop();for(Edge& e:G[v])if(e.cap > 0 && dist[e.to] == -1){dist[e.to]=dist[v]+1;que.push(e.to);}}}// s->t 増加パスFlow dfs_best_path(int s,int t,Flow f){if(s==t)return f;for(int &i = iter[s];i < (int)G[s].size();i++){Edge &e=G[s][i];Edge &reve = G[e.to][eid[e.to][s]];if(e.cap <= 0 || dist[s]>=dist[e.to])continue;//searchFlow d = dfs_best_path(e.to,t,min(f,e.cap));if(d==0)continue;//founde.cap-=d;reve.cap+=d;return d;}return 0;}public:int V;Graph G;vector<vector<int>> eid;MaximamFlow(int V):V(V){G=Graph(V);eid=vector<vector<int>>(V,vector<int>(V,-1));dist=vector<int>(V,-1);iter=vector<int>(V);}void add_edge(int s,int t,Flow cap){if(eid[s][t]<0){eid[s][t]=G[s].size();G[s].push_back({t,0,0});}if(eid[t][s]<0){eid[t][s]=G[t].size();G[t].push_back({s,0,0});}G[s][eid[s][t]].icap=cap;G[s][eid[s][t]].cap=cap;}Flow change_cost(int S,int T,int s,int t,int c){if(eid[s][t]<0)add_edge(s,t,c);Edge& e =G[s][eid[s][t]];Edge& reve = G[t][eid[t][s]];Flow flow = 0;if(e.icap <= c){// 単純に増やすe.cap += c - e.icap;e.icap = c;}else if(c >= e.icap-e.cap){ // 単純に減らすe.cap -= e.icap - c;e.icap = c;}else{// フローを戻してから減らすFlow d = e.icap - e.cap - c;e.cap = 0;reve.cap = 0;//(1) S <- s <- t <- T のフローを戻すFlow fs = max_flow(s,S,d),ft = max_flow(T,t,d),f = min(fs,ft);max_flow(S,s,fs-f); max_flow(t,T,ft-f);flow -= f;//(2) (1)で戻せない分は t <- s <- t 内で無駄な t <- s 路ができているので除去するmax_flow(s,t,d-f);e.cap = 0;reve.cap = c + reve.icap;e.icap = c;}flow+=max_flow(S,T);return flow;}int max_flow(int s,int t,Flow Mf=INF<Flow>()){if(s==t)return Mf;Flow flow=0;while(Mf>0){bfs_dist(s);if(dist[t] == -1)break;Flow f;fill(ALL(iter),0);while((f=dfs_best_path(s,t,Mf))>0)flow+=f,Mf-=f;}return flow;}};class Main{public:void run(){int W;cin >> W;int N;cin >> N;vector<int> js(N);cin >> js;int M;cin >> M;vector<int> cs(M);cin >> cs;MaximamFlow mf(N+M+2);int S=N+M,T=S+1;for(int i:range(M)){int q;cin >> q;set<int> xs;for(int j:range(q)){int v;cin >> v;v--;xs.insert(v);}for(int j:range(N))if(!xs.count(j)){mf.add_edge(j,N+i,INF<int>());}}for(int i:range(N))mf.add_edge(S,i,js[i]);for(int i:range(M))mf.add_edge(N+i,T,cs[i]);if(mf.max_flow(S, T)>=W){cout <<"SHIROBAKO"<<endl;}else{cout <<"BANSAKUTSUKITA"<<endl;}}};int main(){cout <<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);Main().run();return 0;}