Hey, cf finally changed color.
A question: I wrote a two-point, embarrassing ...
Question B: Simulation questions, water
C: Think for a while, Water king (a seniors) told me is DP, decisive knock
DP[I][J] Represents the I-character walk to the J-character's maximum weight View Code
int dp[30][30];
int max (int a,int b) {
return a>b?a:b;
}
int main ()
{
int n,i,j,k;
Char s[15];
while (scanf ("%d", &n)!=eof)
{
memset (dp,0,sizeof (DP));
for (k=1;k<=n;k++)
{
scanf ("%s", s);
int Len=strlen (s);
for (i=0;i<26;i++)
{
if (dp[i][s[0]-' a '])
dp[i][s[len-1]-' A ']=max (dp[i][s[len-1]-' a '],dp[i][s[ 0]-' a ']+len);
}
if (len>dp[s[0]-' a '][s[len-1]-' a '])
dp[s[0]-' A '][s[len-1]-' a ']=len;
}
int ans=0;
for (i=0;i<26;i++)
{
if (Dp[i][i]>ans)
ans=dp[i][i];
}
printf ("%d\n", ans);
}
return 0;
}
D:
To maintain a heap or a multiset from right to left.
E:
Test instructions: Give you a tree, give you some more pairs, the data range is 10W, for each point pair, the points on the simple path between the two points will be passed once
The number of times each edge is passed in the last output
This is the most magical, after the game for a long time, the death of the ladies of all kinds of code, and finally in a night black and high night was I got out, hereby mark my death knock Spirit, hehe
Search the tree first, draw the time stamp and the depth of each point, etc., in order to find a pair of LCA (recent public ancestor), and to turn the tree into a forward
Here's where the core is.
Record an array of c[];
For a pair of points (U v) their LCA (U,V) =x
C[u]++;c[v]++;c[x]+=2: That is, when a point is a point in a point pair, add a 1 to the tag array, and if it is the nearest public ancestor of a certain two points, add a 2
began to do not understand what to do, helpless under I can only make a group of data, single-step debugging, adjusted for a long time, finally is to figure out the magic of this tag array
Okay, no more nonsense, look at the picture.
As shown in the picture of a tree (has been adjusted to the direction of the 1 root), now if there are two pairs of points (12, 14) (14, 15)
c[12]=1,c[14]=2,c[15]=1,la[7]=2,la[3]=2;
Then we started the second DFS and judged that each side was passed several times mainly in the backtracking process.
Each time in the backtracking of the tree node corresponding to the information passed up, such as from V back to u, we must first determine that a few V of the child nodes will "rush out" V through the edge between the U->v
Of course, this number is certainly not the number of tokens in the subtree, because there may be two points of the LCA is also in the subtree, these two points will not come up, that will not go through the edge between u-v
So an LCA can block two points in the subtree (that is, a pair of pairs of inputs) out of the subtrees tree (which explains the la[x]+=2 step), which is expressed in the program as follows: When you go back to a point, subtract the value of La[u] from the point, and the number of points that are less la[u] comes up, Then the process of backtracking from V to u can draw the number of times the side of the U->v has passed, and all the answers come out.
For example, the figure on the left side of the red is the number of these edges (for convenience only)
First traversing to 12, beginning to 6 backtracking, edge[1] was passed once, the edges between edge[1]=1;13 and 6 were not passed, so back to 6
A total of sum=1 points came up, continue to 3 backtracking, minus 6 la[] Value (0), so the edge between 6 and 3 has been passed several times can be obtained
EDGE[4]=SUM+C[6] C[6] is 0, so edge[4]=1;
Backtracking on the right:
14, 15 back to 7,edge[2] plus 2,edge[3] added 1,sum=3 and then from 7 to 3 of the process found that 7 is LCA, so first subtract la[7] value
At this time sum=1, equivalent to 7 of the sub-tree within a point will come out, after edge[5], so edge[5]=1;
This complexity is for the complexity of the LCA, 2*n*log (2*n) recorded timestamp sequence of 2*n length
My Code View
#include <string.h> #include <stdio.h> #include <vector> #include <math.h> using namespace std;
const int M = 100100;
Const double inf = 1E20;
int min (int a,int b) {return a<b?a:b;} int n,k,tdfn,tot;
int dp[20][2*m],vis[m];
int b[2*m],log[2*m],used[m],f[2*m],pos[m],c[m],la[m];
Vector<int> Edge[m],eg[m];
int ans[m];
void Rmq_init (int n,int num[]) {int i,j;
for (j=1;j<=n;j++) dp[0][j]=num[j];
for (j=1;j<=log[n];j++) {int limit=n+1-(1<<J);
for (i=1;i<=limit;i++) {int x=i+ (1<<j>>1);
Dp[j][i]=min (Dp[j-1][x],dp[j-1][i]);
}}} int rmq (int l,int r,int num[]) {int m=log[r-l+1];
return min (dp[m][l],dp[m][r-(1<<m) +1]);
} void Dfs (int s) {int i,t;
Used[s]=1;
int TMP=++TDFN; b[++tot]=tmp;
F[tmp]=s;
Pos[s]=tot;
int sz=edge[s].size ();
for (i=0;i<sz;i++) {t=edge[s][i];
if (used[t]) continue; DFS (t);
B[++tot]=tmp;//backtrack} used[s]=0;
} int LCA (int a,int b) {if (Pos[a]>pos[b]) swap (A, b);
int ANS=RMQ (POS[A],POS[B],B);
return F[ans];
} void Init () {tdfn=0; tot=0;
memset (used,0,sizeof (used));
DFS (1);
Rmq_init (TOT,B);
} int solve (int u,int edge_num)//core {int sum=0;
Used[u]=1;
int sz=edge[u].size ();
for (int i=0;i<sz;i++) {int to=edge[u][i];
if (Used[to]) continue;
Sum+=solve (To,eg[u][i]);
} Sum-=la[u];
if (edge_num) ans[edge_num]=sum+c[u];
return Ans[edge_num];
} int main () {int i,n,m,a,b,w;
Log[0]=-1;
for (i=1;i<2*m;i++) log[i]=log[i>>1]+1;
while (scanf ("%d", &n)!=eof) {for (i=0;i<=n;i++) {edge[i].clear (); c[i]=0;}
Char str[5];
for (i=1;i<n;i++) {scanf ("%d%d", &a,&b);
Edge[a].push_back (b);
Edge[b].push_back (a);
Eg[a].push_back (i);Eg[b].push_back (i);
} init ();
scanf ("%d", &k);
while (k--) {scanf ("%d%d", &a,&b);
c[a]++;
c[b]++;
int X=lca (A, b);
la[x]+=2;
} solve (1,0);
for (i=1;i<n;i++) printf ("%d", ans[i]);
Puts ("");
} return 0; }