結果

問題 No.154 市バス
ユーザー koyumeishikoyumeishi
提出日時 2015-02-18 01:12:04
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,598 bytes
コンパイル時間 884 ms
コンパイル使用メモリ 85,480 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 07:58:36
合計ジャッジ時間 10,506 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,279 ms
5,248 KB
testcase_01 AC 1,274 ms
5,248 KB
testcase_02 AC 1,289 ms
5,376 KB
testcase_03 AC 289 ms
5,376 KB
testcase_04 AC 1,315 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 TLE -
testcase_08 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:58:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |         scanf("%s", c);
      |         ~~~~~^~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

struct edge{
	int to;
	int cap;
	int rev;
};

int Ford_Fulkerson_dfs(vector<vector<edge> > &G, int pos, int flow, vector<bool> &used, int t){
	if(pos == t){
		return flow;
	}else{
		used[pos] = true;
		for(int i=0; i<G[pos].size(); i++){
			if(!used[G[pos][i].to] && G[pos][i].cap >0){
				int myflow = Ford_Fulkerson_dfs(G, G[pos][i].to, min(flow, G[pos][i].cap), used, t);
				if(myflow > 0){
					G[pos][i].cap -= myflow;
					G[ G[pos][i].to ][ G[pos][i].rev ].cap += myflow;
					return myflow;
				}
			}
		}
		return 0;
	}
}

int Ford_Fulkerson(vector<vector<edge> > &G, int s, int t){
	const int INF = 100000000;
	int flow=0;
	for(;;){
		vector<bool> used(G.size(), false);
		int f = Ford_Fulkerson_dfs(G, s, INF, used, t);
		if(f==0) break;
		else flow += f;
	}
	return flow;
}

void add_edge(vector<vector<edge> > &G, int from, int to, int cap){
	G[from].push_back( (edge){to, cap, (int)G[to].size()} );
	G[to].push_back( (edge){from, 0, (int)G[from].size()-1} );
}


void solve(){
	char c[2000];
	scanf("%s", c);
	string s = c;

	int n=s.size();
	vector<int> G;
	vector<int> R;
	vector<int> W;
	for(int i=0; i<n; ++i){
		if(s[i]=='W') W.push_back(i);
		else if(s[i] == 'G') G.push_back(i);
		else if(s[i] == 'R') R.push_back(i);
	}

	if(W.size() * G.size() * R.size() == 0){
		cout << "impossible" << endl;
		return;
	}else if(G.size() != R.size() || W.size() < G.size()){
		cout << "impossible" << endl;
		return;
	}

	vector<vector<edge>> Graph(n+2);
	int source = n;
	int sink = n+1;

	for(int i=0; i<W.size(); ++i){

		add_edge(Graph, source, W[i], 1);
/*
		if(i>0){
			add_edge(Graph, W[i-1], W[i], 1000);
		}
*/
		
		auto next = lower_bound(G.begin(), G.end(), W[i]);
		if(next == G.end()){
			cout << "impossible" << endl;
			return;
		}
		add_edge(Graph, W[i], *next, 1);
	}


	for(int i=0; i<G.size(); ++i){

		if(i>0){
			add_edge(Graph, G[i-1], G[i], 1000);
		}

		auto next = lower_bound(R.begin(), R.end(), G[i]);
		if(next == R.end()){
			cout << "impossible" << endl;
			return;
		}
		add_edge(Graph, G[i], *next, 1);

	}

	for(int i=0; i<R.size(); ++i){
		add_edge(Graph, R[i], sink, 1);
		if(i>0){
			add_edge(Graph, R[i-1], R[i], 1000);
		}
	}

	int ans = Ford_Fulkerson(Graph, source, sink);

	if(ans == G.size() && W.size()>=ans){
		cout << "possible" << endl;
	}else{
		cout << "impossible" << endl;
	}
}

int main(){
	int T;
	cin >> T;
	while(T--){
		solve();
	}

}
0