Submission #288325


Source Code Expand

Copy
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
// int a[310][310];
vvint vv;
vvint vvrev;
vint f;
vint frev;
int n,m,k;
vector<char> c;
int vis[310]={-1};
vint vc;
vector<string> vs;
void dfs(int x){
	if(vis[x]==1) return;
	vis[x]=1;
	rep(i,vv[x].size()){
		dfs(vv[x][i]);
	}
	f.PB(x);
}
void rdfs(int x,int k){
	if(vis[x]==1) return;
	vis[x]=1;
	vc[x]=k;
	string t;
	convert(c[x],t);
	// cout<<"t "<<t<<endl;
	vs[k]+=t;
	rep(i,vvrev[x].size()){
		rdfs(vvrev[x][i],k);
	}
	return;
}
string update(string s,string in){
	rep(i,in.size()){
		string tmp;
		convert(in[i],tmp);
		s+=tmp;
		if(s.size()<=k) continue;
		bool flag=true;
		rep(j,s.size()-1){
			if(s[j]>s[j+1]){
				flag=false;
				s=s.substr(0,j)+s.substr(j+1);
				break;
			}
		}
		if(flag) s=s.substr(0,s.size()-1);
		// cout<<"s "<<s<<endl;
	}
	return s;
}
string dfs2(string s,int x,int r){
	// cout<<s<<" "<<x<<" "<<r<<endl;
	if(vis[x]==2) return s;
	vis[x]++;
	if(vc[x]!=r){
		// cout<<vs[vc[x]]<<endl;
		s=update(s,vs[vc[x]]);
		// cout<<s<<endl;
		r=vc[x];
	}
	// cout<<s<<endl;
	bool flag=false;
	string ret="";
	rep(i,k+1) ret+="z";
	if(s.size()==k) ret=s;
	if(vis[x]==0){
		rep(i,vv[x].size()){
			if(vc[x]==vc[vv[x][i]]){
				flag=true;
				string t=dfs2(s,vv[x][i],r);
				ret=min(ret,t);
			}
		}
	}
	if(vis[x]==1||!flag){
		rep(i,vv[x].size()){
			// cout<<vv[x][i]<<" "<<vc[vv[x][i]]<<endl;
			if(vc[x]<=vc[vv[x][i]]){
				string t=dfs2(s,vv[x][i],r);
				ret=min(ret,t);
			}
		}
	}
	// cout<<ret<<endl;
	return ret;
}
void mainmain(){
	// int n,m,k;
	cin>>n>>m>>k;
	c=vector<char>(n);
	vv=vvint(n);
	vvrev=vvint(n);
	rep(i,n) cin>>c[i];
	// rep(i,n) cout<<c[i]<<" ";
	// cout<<endl;
	rep(i,m){
		int x,y;
		cin>>x>>y;
		x--;
		y--;
		vv[x].PB(y);
		vvrev[y].PB(x);
	}
	rep(i,n) vis[i]=-1;
	rep(i,n) dfs(i);
	rep(i,n) vis[i]=-1;
	vc=vint(f.size());
	vs=vector<string>(f.size(),"");
	rep(i,f.size()){
		// cout<<f[f.size()-1-i]<<endl;
		rdfs(f[f.size()-1-i],i);
		sort(ALL(vs[i]));
		// cout<<vs[i]<<endl;
	}
	rep(i,n) vis[i]=-1;
	string ans=dfs2("",f[f.size()-1],-1);
	// cout<<ans<<endl;
	if(ans.size()!=k) ans="";
	rep(i,f.size()-1){
		string tmp=dfs2("",f[f.size()-2-i],-1);
		// cout<<tmp<<endl;
		if(tmp.size()==k) ans=min(ans,tmp);
	}
	// cout<<ans<<endl;
	if(ans.size()!=k) cout<<-1<<endl;
	else cout<<ans<<endl;

}



}
main() try{
    mainmain();
}
catch(...){
}

Submission Info

Submission Time
Task C - 有向グラフ
User j_gui0121
Language C++11 (GCC 4.8.1)
Score 0
Code Size 4062 Byte
Status WA
Exec Time 30 ms
Memory 1440 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 4
AC × 28
WA × 4
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask0_sample_04.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask0_sample_04.txt, subtask1_manual01.txt, subtask1_manual02.txt, subtask1_manual03.txt, subtask1_manual04.txt, subtask1_manual05.txt, subtask1_manual06.txt, subtask1_manual07.txt, subtask1_manual08.txt, subtask1_random01.txt, subtask1_random02.txt, subtask1_random03.txt, subtask1_random04.txt, subtask1_random05.txt, subtask1_special01.txt, subtask1_special02.txt, subtask1_special03.txt, subtask1_special04.txt, subtask1_special05.txt, subtask1_special06.txt, subtask1_special07.txt, subtask1_special08.txt, subtask1_special09.txt, subtask1_special10.txt, subtask1_special11.txt, subtask1_special12.txt, subtask1_special13.txt, subtask1_special14.txt, subtask1_special15.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 23 ms 804 KB
subtask0_sample_02.txt AC 22 ms 700 KB
subtask0_sample_03.txt AC 21 ms 924 KB
subtask0_sample_04.txt AC 27 ms 804 KB
subtask1_manual01.txt AC 24 ms 796 KB
subtask1_manual02.txt AC 22 ms 920 KB
subtask1_manual03.txt AC 22 ms 672 KB
subtask1_manual04.txt AC 23 ms 736 KB
subtask1_manual05.txt AC 22 ms 804 KB
subtask1_manual06.txt AC 22 ms 920 KB
subtask1_manual07.txt AC 23 ms 924 KB
subtask1_manual08.txt AC 22 ms 796 KB
subtask1_random01.txt WA 28 ms 920 KB
subtask1_random02.txt WA 25 ms 928 KB
subtask1_random03.txt WA 27 ms 1056 KB
subtask1_random04.txt AC 27 ms 936 KB
subtask1_random05.txt AC 25 ms 928 KB
subtask1_special01.txt AC 28 ms 936 KB
subtask1_special02.txt AC 25 ms 924 KB
subtask1_special03.txt AC 26 ms 932 KB
subtask1_special04.txt AC 27 ms 940 KB
subtask1_special05.txt AC 29 ms 1176 KB
subtask1_special06.txt WA 28 ms 1308 KB
subtask1_special07.txt AC 27 ms 1060 KB
subtask1_special08.txt AC 28 ms 1308 KB
subtask1_special09.txt AC 29 ms 1440 KB
subtask1_special10.txt AC 29 ms 1316 KB
subtask1_special11.txt AC 29 ms 1324 KB
subtask1_special12.txt AC 30 ms 1316 KB
subtask1_special13.txt AC 28 ms 1248 KB
subtask1_special14.txt AC 24 ms 924 KB
subtask1_special15.txt AC 29 ms 1060 KB