QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#129794 | #6507. Recover the String | Sorting# | ML | 595ms | 79300kb | C++ | 3.1kb | 2023-07-23 00:16:08 | 2023-07-23 00:16:10 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
vector<int>in[N];
int n,m,k;
int out[N];
int ans1[N],ans2[N];
int pr[N];
int find(vector<int>&v,int c){
for(int i=0; i<v.size() ;i++){
if(c==v[i]) return i;
}
return -1;
}
vector<int>cyclic(vector<int>g,int p){
if(p==1) return g;
vector<int>head;
{
for(auto c:in[g[0]]){
bool ok=true;
int d=c;
for(int i=0; i<g.size() ;i++){
int p=find(in[g[i]],c);
if(p==-1) ok=false;
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
}
if(ok && d==c) head.push_back(c);
}
}
int c=head[0];
vector<int>ng;
ng.push_back(c);
for(int i=0; i<g.size() ;i++){
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
ng.push_back(c);
}
ng.pop_back();
return cyclic(ng,p-1);
}
vector<int>karina(vector<int>g,int p){//not guarenteed that g is distinct
/*cout << "karina " << p << endl;
for(auto c:g) cout << c << ' ';
cout << endl;*/
if(p==1) return g;
vector<int>head;
{
for(auto c:in[g[0]]){
bool ok=true;
int d=c;
for(int i=0; i<g.size() ;i++){
int p=find(in[g[i]],c);
if(p==-1) ok=false;
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
}
if(ok) head.push_back(d);
}
}
int c=head[0];
vector<int>ng;
ng.push_back(c);
for(int i=0; i<g.size() ;i++){
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
ng.push_back(c);
}
return karina(ng,p-1);
}
vector<int>winter(vector<int>g,int p){//guarenteed that g is distinct
if(g.size()>p || true) return karina(g,p);
vector<int>head;
{
for(auto c:in[g[0]]){
bool ok=true;
int d=c;
for(int i=0; i<g.size() ;i++){
int p=find(in[g[i]],c);
if(p==-1) ok=false;
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
}
if(ok) head.push_back(d);
}
}
int c=head[0];
vector<int>ng;
ng.push_back(c);
for(int i=0; i<g.size() ;i++){
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
ng.push_back(c);
}
}
int lb[N];
string greedy(vector<int>s){
for(int i=1; i<=n ;i++) lb[i]=0;
int ptr=0;
string res;
for(auto c:s){
//cout << "haha " << c << endl;
if(lb[c]==0){
lb[c]=++ptr;
}
res+='a'+lb[c]-1;
}
return res;
}
void solve(){
cin >> n >> m;
for(int i=1; i<=n ;i++){
in[i].clear();
out[i]=0;
}
for(int i=1; i<=m ;i++){
int u,v;cin >> u >> v;
in[v].push_back(u);
out[u]++;
}
if(n==1){
cout << "a\n";
return;
}
int boss=0;
for(int i=1; i<=n ;i++){
if(out[i]==0){
boss=i;break;
}
}
{//compute length
int x=boss;
k=1;
while(!in[x].empty()){
k++;
x=in[x][0];
}
}
if(in[boss].size()==1){
for(int i=1; i<=n ;i++) cout << "a";
cout << '\n';
return;
}
auto v1=winter({in[boss][0],in[boss][1]},k-1);
string s1=greedy(v1);
auto v2=winter({in[boss][1],in[boss][0]},k-1);
string s2=greedy(v2);
cout << min(s1,s2) << '\n';
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
int t;cin >> t;while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 30320kb
input:
3 1 0 5 6 2 4 2 5 5 3 4 3 1 5 1 4 8 11 1 2 1 4 1 6 2 5 3 4 3 6 4 5 4 7 5 8 6 7 7 8
output:
a aba aaba
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 195ms
memory: 30896kb
input:
26837 1 0 2 1 2 1 3 2 1 3 2 3 3 2 3 1 1 2 5 5 4 3 5 1 1 2 3 2 5 3 5 6 2 4 2 5 5 3 4 3 1 5 1 4 5 5 1 5 2 1 2 4 4 5 3 4 6 6 1 4 5 3 2 4 4 6 3 6 2 3 4 3 1 3 3 4 2 1 7 8 2 5 1 3 7 1 3 5 7 6 1 2 4 6 6 3 8 11 2 6 2 7 3 7 8 1 6 4 4 5 7 4 7 1 3 8 2 8 1 5 8 10 8 1 4 5 7 8 3 5 3 1 7 3 1 2 5 2 6 4 6 3 9 11 5 2...
output:
a aa ab aaa aab aba aab abc aaaa aaab aaba aabb aabc aaba abab abac abba aaab abbc abca abac aabc abcd aaaaa aaaab aaaba aaabb aaabc aabaa aabab aabac aabba aaabb aabbc aabca aabcb aabcc aabcd aaaba abaab abaac ababa aabab ababc abaca abacb aabcb abacd aabba abaab abbac abbba aaaab abbbc abbca abaac...
result:
ok 26837 lines
Test #3:
score: 0
Accepted
time: 200ms
memory: 29912kb
input:
6450 148 290 30 17 69 52 99 36 15 20 96 129 17 64 86 137 74 81 145 138 135 30 113 118 26 52 125 60 39 34 15 59 112 92 126 68 13 23 33 96 145 148 71 88 77 51 81 86 12 127 54 5 47 31 13 21 49 137 51 66 135 48 45 62 93 53 133 119 76 65 94 104 103 25 10 143 44 2 4 80 92 57 131 99 64 28 36 148 53 122 35 ...
output:
abbabaababbaabbabaab abbcacababccababcbca abcdefghijklmnopqrst abaababaabaababaabab abcabcabcabcabcabcab abaabaabaabaabaabaab ababaababaababaababa abaabaabaabaababaaba abababababababababab abacabacabacabacabac abacabadabacabaeabac aabaabbabbbaabbaabba aaababbcbbccaaaabcac abcadefghijfklmkcbdf aabaca...
result:
ok 6450 lines
Test #4:
score: 0
Accepted
time: 196ms
memory: 29436kb
input:
970 920 1834 730 734 56 197 114 794 161 101 228 454 791 135 423 9 877 886 875 387 840 727 730 643 587 823 112 816 672 87 911 365 251 329 478 863 76 580 869 898 906 598 222 116 523 621 349 645 601 897 109 271 26 594 877 691 409 891 245 286 146 591 289 95 786 850 576 8 131 507 6 285 71 695 853 713 265...
output:
ababbabaabbaababbaabbabaabbaababbabaababbaabbabaab ababccababcbcaabcbcacabcababcbcaabcbcacabbcacababc abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxz abaababaabaababaababaabaababaabaababaababaabaababa abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcab abaabaabaabaabaabaabaabaabaabaabaabaabaabaaba...
result:
ok 970 lines
Test #5:
score: 0
Accepted
time: 206ms
memory: 29956kb
input:
237 3672 7338 128 799 1687 1156 3289 2676 17 11 10 2456 3634 1671 1180 2885 1838 3438 272 406 1704 2120 2612 2145 3172 1407 2717 3154 2801 1157 1377 2383 3458 3086 1633 3374 1922 869 958 248 2150 2186 1010 2998 370 1368 3407 2495 3644 987 1582 1701 2231 3541 1600 1620 2574 2836 3238 2131 1373 1548 2...
output:
abbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababba aabcbcacabbcacababcbcacababccababcbcaabcbcacabcababcbcaabcbcacabbcacababcabcbcacabbcacababccababcbca abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvzxyabcdefghijklmnopqrstuvzwyabcdefghijklmnopqrstuvz...
result:
ok 237 lines
Test #6:
score: 0
Accepted
time: 285ms
memory: 31432kb
input:
30 33368 66730 8076 11100 29514 11018 19224 28482 7851 20178 15965 14582 27252 7790 21764 13370 14476 32499 32535 26445 13036 10117 21957 8939 27288 2575 94 31016 28912 8162 11851 20676 1998 14167 23076 4834 13894 23588 4510 16856 4125 24548 7174 31028 20038 16939 13363 29338 27074 24233 14616 29951...
output:
abbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaab...
result:
ok 30 lines
Test #7:
score: 0
Accepted
time: 471ms
memory: 49784kb
input:
8 380248 760490 124441 243214 318264 317073 80812 184051 371269 92314 147997 324777 348208 151765 277437 22744 135856 228557 170049 232045 211622 83057 272356 232823 47448 177829 101380 373767 357788 184436 373026 30555 89629 377654 213780 51067 227975 39349 216809 210156 42077 295276 195939 221049 ...
output:
abbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababba...
result:
ok 8 lines
Test #8:
score: 0
Accepted
time: 379ms
memory: 63804kb
input:
1 1000000 999999 486593 40134 567630 575732 673482 763704 372903 393266 657204 514133 677524 433408 258586 60505 668823 278755 406509 351060 877657 86847 393838 390004 323007 188430 833631 698149 362171 368078 725792 944166 810862 211037 579856 958663 293964 656704 398325 41189 393451 571249 585688 ...
output:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
result:
ok single line: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Test #9:
score: 0
Accepted
time: 595ms
memory: 77560kb
input:
1 999720 1999418 478960 367385 278842 195576 978938 976238 621173 945176 934972 578856 824323 381756 538870 587404 445290 681032 934823 385654 924723 412221 85667 342451 175667 376718 825906 546292 215330 897312 29111 214255 903178 224277 622872 949028 895275 674752 744266 238233 917807 485857 50574...
output:
ababbaaaabbaaababbbabaaababaabbaababbbabbbaababbaaaabbabbbbbababbbaababaababaabbaabbbbbaabaababbbbbbbaaabbabaaabbaabbbababaabaabbbaabbabababbababababbbaabbbbbbbabbbabbbbbaabbaaaaabbbbabbaaaaababaabbababaababababbababbaabbabaaaababaabbababbbbaaaabaaabbaababaaaabbbabaaaaabbbaaabbaababbbaaaabaabbbbaabb...
result:
ok single line: 'ababbaaaabbaaababbbabaaababaab...abbbaabaababbaaabbaabbbababbbba'
Test #10:
score: 0
Accepted
time: 578ms
memory: 77260kb
input:
1 999962 1999902 52152 909124 778034 197109 215551 980572 351009 284250 906426 112777 146723 864320 128930 437120 297457 358563 702334 350219 168315 3105 750352 326219 541338 400633 725295 147687 463415 95558 557045 884970 906696 895067 8701 56935 472957 784385 733678 464051 491501 368221 717902 907...
output:
aababbaaabbaaaabbabaabbbabbaabaaaabbabbabbbbabbbaaabbbbbbaabbaabbabbabbabaaaabaabbaaababbbbbaaababaaaababaabbababbbababbabaaaaaaabbaabbababbaabbaabaabababbaaabaaaaababababbbabbaabaaababbbaaaabaababbbbbaababbabbaaaabbbabbbbbaabaaabaaabbaaabbbbababbaabbbbbbbabababbbbaabaaaaabbbabbbbabaaaaabbaabbabbbaa...
result:
ok single line: 'aababbaaabbaaaabbabaabbbabbaab...aababaaaaaabbabaabbbbaabbaabbab'
Test #11:
score: 0
Accepted
time: 555ms
memory: 77300kb
input:
1 999932 1999842 994049 660788 387551 736624 508811 633488 317145 571345 906407 201785 430886 611119 183526 415265 720900 956714 207924 571189 781568 293026 258860 300050 29668 968606 711768 267147 140099 193742 353070 17072 117128 701452 842704 198461 918561 757129 879182 813948 245889 282501 46994...
output:
aabaaabababbabbababbbaaabbababaabaabaabbababbbababababbbabbbaabbabaabbaaaaaabababbaaababbabbabbabaaabaabbabaaabbbabbaabaaaaaaaabaabbaababbaaababababbbabaaaaaababbbabbbabbabbbabbaaaababbbbabbbabbaababaabbbabaaabbabaaabbaaabababbbabaaabbaaababbbaaababbaabbbabbbbbabbbabbbabbbbabaaaaaaabaababbaaabababaa...
result:
ok single line: 'aabaaabababbabbababbbaaabbabab...baabaaababbaaaaabaaabbbaaaabbaa'
Test #12:
score: 0
Accepted
time: 561ms
memory: 77268kb
input:
1 999960 1999898 696875 9265 618298 741593 334125 932167 316155 284255 923880 106712 771194 353262 31998 417868 845589 256875 591867 976859 220019 209003 829310 14740 460460 37659 538159 801865 462765 878326 602060 979546 157850 511382 761195 500165 433201 295913 87888 947492 306572 761401 978020 91...
output:
aaaaabbabaabbbabababbaaaabbaaaabaabaababbabbbaabbaaabbbbaaaaabababbaabaabbbabbaaabbabbaabbaabaaabbaaaababbababaabbbaabaababaabbbbabbbbbbaabbaababbabbbbbabbaabbaabaaababbaaaaaabbbaaaaaabbababaabaaababbabaaaabbbbbababbabaabbabbabbaaabaaaaabaaaabbabaaababbabbbbbbaaaabaabbaababbaaaaaaaababbaababaaabbbab...
result:
ok single line: 'aaaaabbabaabbbabababbaaaabbaaa...bbbbbbbbabaaaaaaaaabbabbaaaabbb'
Test #13:
score: 0
Accepted
time: 571ms
memory: 77324kb
input:
1 999286 1998549 332293 847900 781086 328251 89438 882788 175083 14440 572430 574257 809569 484269 663930 438265 122446 76616 651689 649277 10930 366807 865522 459778 415599 426786 191310 197100 747014 674992 395012 954730 372947 143720 681954 55327 377093 403183 960062 910427 919504 396335 314041 2...
output:
aabbacbacbccccabcacccabbbabcccacbaaaaaaccacbccaacbbaaacbcbaccbbacaacabcaccccaabcabccaaccbaaabbabbccbbbcbcbbbccababbbbbcaabbacaaacaabbaacbaabbbbbcbccaccbbcbabcbacccabcbbcbcaaabbbabbaccacacbaacaabbaacccacbaabbccbbcabacabbcbababccbbcbaacacbaccaaabbabbbbbaacbbbaaabacaabcacaaaccaaccbcababacbbabbababacbcc...
result:
ok single line: 'aabbacbacbccccabcacccabbbabccc...acbacbbabaaacaccbccaabbbbaaaaba'
Test #14:
score: 0
Accepted
time: 580ms
memory: 77240kb
input:
1 999331 1998641 729147 538731 921253 798894 885546 677818 298747 97157 180087 234737 418721 783414 901052 913813 595106 405323 295245 567060 70011 388501 377766 19230 874327 848890 928988 650497 238183 939267 593826 388828 573609 454635 81052 5415 661081 742732 500902 266652 996206 520864 328011 14...
output:
aabacbaaccbabbabbacaccbbabcbccabcabbbacbccacbabccbbcacbabbcbbabbacacccaaccccbaaccbaabcbcaaaaaacacbccbbcaabcbaabaccbcabacaccbbbabccabaaabbbbbcbacacacaabcacaabccccabcaabaacacbccabaaccabaccaabcacbccbbabbacbbcbabccaabbbcbacbbbcabaabcabacbaaccaccacabcacbaccaabacacccbabaacbbbcbcabcbabcababbaabacaacacccacb...
result:
ok single line: 'aabacbaaccbabbabbacaccbbabcbcc...cacacacaaaaacbbaaaccbaacbccaaca'
Test #15:
score: 0
Accepted
time: 556ms
memory: 77256kb
input:
1 999390 1998754 371866 116100 905495 392764 44483 862055 894183 603825 297556 557791 204825 860195 258135 783387 213801 233286 733327 526541 291442 435626 497561 467258 125745 889357 162794 595141 727020 316815 329022 931460 658882 219351 855248 963142 453518 646179 49612 456747 168958 167848 23953...
output:
aabbbccaaddebdeebadbabadcdddbacaacababeeabbeebddadedaddaadedcdebccabeaebcdbaabceaceebabeeeddeecaecccddabdbbacadaedceddbecaecdcbdaebbdbaacaaebddaeaaabaddbecaacacacedccdabbaaacdeaccacdbaeeecaecbeddcdaecabceaebdebaeceebbbcdbdeacdcbacceaaebeecbecdaadcabbdbcadbaaeabcdaeabcdcaabdbcebecacebcdceeeacebbabada...
result:
ok single line: 'aabbbccaaddebdeebadbabadcdddba...eeccdaabdcbadbdcbaecedbdccdaeba'
Test #16:
score: 0
Accepted
time: 543ms
memory: 79300kb
input:
1 999332 1998641 340224 312609 496357 621563 282231 31016 9727 518497 345711 368257 231510 162608 370656 157276 520228 905900 444817 419597 284400 368374 619796 817878 542290 224955 388559 105743 824688 371866 837389 734221 600626 944539 93565 550551 676681 298291 663021 283487 761202 315067 748929 ...
output:
aaaabbcaddcacaecdedaadbcbeadddbebbedeededceddedcadadacadceddbddbcadcdabedaabdbccededaccedabecacbacdcacebaecaabbdeedcdaacaacaceaeeedeecbbbeedacdeaaebcadaddcdecaadebabbcbcbbddcadcbeabeaededbcacdbbdcabeaaeaaedabdbcbeadaddadedddaebcacacbdbdedcccaabbbdaebdadecbecabaedadcbceecbdadacdbbdabeaaddebcbacdcbaca...
result:
ok single line: 'aaaabbcaddcacaecdedaadbcbeaddd...eacabbeddaaebbabcadcccbccbacdaa'
Test #17:
score: 0
Accepted
time: 579ms
memory: 77268kb
input:
1 999739 1999440 205098 344014 307300 441050 151960 549625 675401 498542 254496 388186 804623 686320 449469 974202 149674 935006 81205 629864 388824 158142 4307 138602 862844 464387 337179 904964 437580 558766 679226 445604 698011 971021 661578 367602 698306 240195 179735 321954 638187 260543 541268...
output:
ababcdefdgfcdafhhceahiifjgafafcigfgfagjdjigccjgfcjifaegcghjiadafegiaajfbjjbajdddjjjfggggjgaccdchcafcddbhcfchbjjcjfjbihebdjbbcfafbfhdfhibdjhcgajahjibfgicfibadjacggejebbgjijaafdidgbajgidffahebabbghecffcfgiaficgichiffadidjfheaibhfbhjegeghjbecdjdjjhadjaieieeebagcehdicjdgbdadgceaaefahgfjjaebiajdjdibeheeb...
result:
ok single line: 'ababcdefdgfcdafhhceahiifjgafaf...cfhdacdjbjjddfgcabahhcdeiicjjce'
Test #18:
score: 0
Accepted
time: 568ms
memory: 77288kb
input:
1 999710 1999383 937916 289472 397745 223193 993084 450771 753199 164546 143627 180490 834071 338355 930758 765524 949290 91931 660057 139978 516021 518052 871186 290530 170047 970212 327356 621467 28958 564133 69866 550611 793613 404090 468057 410337 75929 627503 834050 483273 545988 569261 672096 ...
output:
aabcdefgfbhhccdeigdgjbffiadhbfiidghdehbddedajggcahedibjegediaeeafjbiedbgaagfehgigbghdgbjjfcbggfgcdghahahijjiagaiihbeiiefchhhgdjchecefccfhdadfeffibffhebdbjicccgehhchdcfjibbjbajbcigefgiahjhahgdahfjjifjgcjbggjbbhhhahbebbaeeajabcgahjiaajaaejcjgffhcadeggjhdibddggjibajejdbaahiigbdcdafbcdebjiedbgjbidfhddgg...
result:
ok single line: 'aabcdefgfbhhccdeigdgjbffiadhbf...jgaagcajigbaejchgeebccacidafbfd'
Test #19:
score: 0
Accepted
time: 370ms
memory: 62112kb
input:
1 1000000 999999 278296 106049 911981 972246 893106 498127 783362 841190 100615 892567 336171 546759 879578 798806 821558 165334 240626 948176 766870 782719 698585 659818 433374 475363 210776 45956 160561 881213 929083 451179 276955 528871 172610 1150 945837 980812 612046 510874 516551 688101 401500...
output:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
result:
ok single line: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Test #20:
score: -100
Memory Limit Exceeded
input:
1 999999 1999994 581178 432299 504497 15192 717769 924100 36894 56780 350786 794293 246364 706614 179777 929163 22940 704272 114548 224999 620984 147378 124534 370669 505076 273968 57565 156162 541654 824947 916216 571797 789038 941852 350195 648312 405505 10481 468099 554657 404064 883737 532801 98...