Codeforces 508 D. Tanya and Password (Fleury algorithm)

Source: Internet
Author: User
Tags explode

Codeforces 508 D. Tanya and Password (Fleury algorithm)

Topic Links:

Http://codeforces.ru/problemset/problem/508/D

Test instructions
Give n a string of length 3, such as: ABC bca AAB if the suffix of a string is 2 and the length of the other string is a prefix of 2, then the two strings can be connected, for example: AABCA, and then the N strings can form a graph, and a Oraton path on the graph is obtained.
Limit:
1 <= N <= 2*10^5, with uppercase letters in the string, lowercase letters
Ideas:
Each string is treated as an edge, and its prefix suffix is used as a point, such as: ABC, AB to BC.
Then the problem is: there is a 62*62 point, the 2*10^5 edge of the map, the question of a European pull path.
With Fleury (Frolles), the personal feeling uses adjacency table to achieve efficiency ratio adjacency matrix

PS: This problem DFS will explode stack, change it to non-recursive on the line.


Attach Fleury algorithm link

Http://www.cnblogs.com/Lyush/archive/2013/04/22/3036659.html


C + + Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*codeforces 508 D. Tanya and Password
Test instructions
Give n a string of length 3, such as: ABC bca AAB if the suffix of a string is 2 and the length of the other string is a prefix of 2, then the two strings can be connected, for example: AABCA, and then the N strings can form a graph, and a Oraton path on the graph is obtained.
Limit:
1 <= N <= 2*10^5, with uppercase letters in the string, lowercase letters
Ideas:
Each string is treated as an edge, and its prefix suffix is used as a point, such as: ABC, AB to BC.
Then the problem is: there is a 62*62 point, the 2*10^5 edge of the map, the question of a European pull path.
With Fleury (Frolles), the personal feeling uses adjacency table to achieve efficiency ratio adjacency matrix
PS: This problem DFS will explode stack, change it to non-recursive on the line.
*/
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<map>
#include<stack>
usingnamespaceStd
Constintm=200005;
Constintn= $* $;
vector<int> Mp[n];
intFa[n];
intIn[n],out[n];
CharANS[M],REV[M];
intlen=0;
String I2s[n];
Map<string,int> s2i;
intstk[m],top=0;

intGETFA (intx) {
if(X!=fa[x])returnFA[X]=GETFA (Fa[x]);
returnX
}
voidDfsintx) {
while(Mp[x].size () >0){
Stk[++top]=x;
intCh=mp[x][mp[x].size ()-1];
Mp[x].pop_back ();
X=ch;
}
Stk[++top]=x;
}
voidFleury (intS) {
intBrige;
Stk[++top]=s;
intflag=0;
intTp
while(top>0){
tp=stk[top--];
Brige= (mp[tp].size () = =0);
if(Brige) {
if(flag)
ans[len++]=i2s[tp][0];
Else{
ans[len++]=i2s[tp][1];
ans[len++]=i2s[tp][0];
flag=1;
}
}
Else
DFS (TP);
}
}

intMain () {
intN,m;
Charstr[5];
String fr,to;
n=0;
scanf"%d", &m);
for(intI=0;i< $* $; ++i)
Fa[i]=i;
for(intI=0; i<m;++i) {
intx, y;
intFx,fy;
scanf'%s ', str);
fr=str[0];
fr+=str[1];
to=str[1];
to+=str[2];

X=S2I[FR];
if(x==0{s2i[fr]=++n; i2s[n]=fr; x=n;}
Y=s2i[to];
if(y==0{s2i[to]=++n; i2s[n]=to; y=n;}

Mp[x].push_back (y);
FX=GETFA (x);
FY=GETFA (y);
if(FX!=FY)
FA[FY]=FX;
++OUT[X];
++in[y];
}
intlt=1;
intROOT=GETFA (1);
for(intI=2; i<=n;++i) {
if(GETFA (i)!=root) {lt=0; Break; }
}
ints=0, t=0, cnt=0;
for(intI=1; i<=n;++i) {
if(In[i]==out[i])Continue;
Else{
cnt++;
if(in[i]-out[i]==1) t=i;
Elseif(in[i]-out[i]==-1) S=i;
Else{cnt=-1; Break; }
}
}
if(LT && (cnt==2&& s!=t) | | cnt==0)){
Puts"YES");
if(cnt==0) s=1;
Fleury (S);
intLen=strlen (ANS);
for(intI=0; i<len;++i) {
rev[len-1-i]=ans[i];
}
Puts (rev.);
}
Else{
Puts"NO");
}
return0;
}

Codeforces 508 D. Tanya and Password (Fleury algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.