Submission #288451


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 vv2;
vvint vvrev;
vint f;
vint frev;
int n,m,k;
vector<char> c;
int vis[310]={-1};
vint vc;
vector<string> vs;
vector<string> vs2;
string ng;
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);
	vs[k]+=t;
	rep(i,vvrev[x].size()){
		rdfs(vvrev[x][i],k);
	}
	return;
}
bool rdfs2(int x,int k){
	if(vis[x]==1) return false;
	vis[x]=1;
	rep(i,vvrev[x].size()){
		if(vc[x]>vc[vvrev[x][i]]) vv2[k].PB(vc[vvrev[x][i]]);
		rdfs2(vvrev[x][i],k);
	}
	return true;
}
string update(string s,string in){
	rep(i,in.size()){
		string tmp;
		convert(in[i],tmp);
		s=tmp+s;
		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);
	}
	return s;
}
string dfs2(string s,int x){
	// cout<<s<<" "<<x<<endl;
	// if(vis[x]==1) return s;
	vis[x]=1;
	s=update(s,vs[x]);
	string ret=s;
	rep(i,vv2[x].size()){
		string tmp=dfs2(s,vv2[x][i]);
		if(ret.size()<k) ret=tmp;
		else ret=min(ret,tmp);
	}
	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,m){
		int x,y;
		cin>>x>>y;
		x--;
		y--;
		vv[x].PB(y);
		vvrev[y].PB(x);
	}
	rep(i,n){
		rep(j,vvrev[i].size()){
		}
	}
	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()){
		rdfs(f[f.size()-1-i],i);
		sort(ALL(vs[i]));
		reverse(ALL(vs[i]));
	}
	vv2=vvint(f.size());
	rep(i,310) vis[i]=-1;
	int start;
	rep(i,f.size()){
		if(rdfs2(f[f.size()-1-i],i)) start=i;
		sort(ALL(vv2[i]));
		// rep(j,vv2[i].size()){
		// }
		vv2[i].erase(unique(ALL(vv2[i])),vv2[i].end());
		// rep(j,vv2[i].size()){
			// cout<<vv2[i][j]<<" ";
		// }
		// cout<<endl;
	}
	rep(i,310) vis[i]=-1;
	ng="";
	rep(i,k+1) ng+="z";
	// vs2=vector<string>(f.size());
	string ans=dfs2("",start);
	rep(i,f.size()){
		if(vis[f[i]]==1) continue;
		string tmp=dfs2("",f[i]);
		if(tmp.size()==k) ans=min(ans,tmp);
	}
	// if(ans.size()!=k) ans=ng;
	// rep(i,f.size()-1){
		// string tmp=dfs2("",f[f.size()-2-i],-1);
		// if(tmp.size()==k) ans=min(ans,tmp);
		// rep(j,310) if(vis[j]!=-1) vis[j]=2;
	// }
	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 100
Code Size 4258 Byte
Status AC
Exec Time 32 ms
Memory 1052 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 4
AC × 32
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 924 KB
subtask0_sample_02.txt AC 24 ms 924 KB
subtask0_sample_03.txt AC 22 ms 912 KB
subtask0_sample_04.txt AC 22 ms 696 KB
subtask1_manual01.txt AC 23 ms 796 KB
subtask1_manual02.txt AC 22 ms 804 KB
subtask1_manual03.txt AC 24 ms 796 KB
subtask1_manual04.txt AC 22 ms 796 KB
subtask1_manual05.txt AC 23 ms 800 KB
subtask1_manual06.txt AC 22 ms 800 KB
subtask1_manual07.txt AC 23 ms 796 KB
subtask1_manual08.txt AC 21 ms 860 KB
subtask1_random01.txt AC 30 ms 920 KB
subtask1_random02.txt AC 28 ms 924 KB
subtask1_random03.txt AC 32 ms 800 KB
subtask1_random04.txt AC 31 ms 856 KB
subtask1_random05.txt AC 29 ms 804 KB
subtask1_special01.txt AC 24 ms 812 KB
subtask1_special02.txt AC 25 ms 924 KB
subtask1_special03.txt AC 25 ms 880 KB
subtask1_special04.txt AC 25 ms 920 KB
subtask1_special05.txt AC 24 ms 1052 KB
subtask1_special06.txt AC 24 ms 928 KB
subtask1_special07.txt AC 24 ms 796 KB
subtask1_special08.txt AC 23 ms 920 KB
subtask1_special09.txt AC 24 ms 924 KB
subtask1_special10.txt AC 25 ms 924 KB
subtask1_special11.txt AC 24 ms 924 KB
subtask1_special12.txt AC 24 ms 920 KB
subtask1_special13.txt AC 23 ms 796 KB
subtask1_special14.txt AC 24 ms 732 KB
subtask1_special15.txt AC 24 ms 868 KB