Codeforces round #267 div.2 D Fedor and essay -- strongly connected DFS

Source: Internet
Author: User

Question: Give an article and replace a B with some words, which means that word a can be replaced by B and can be replaced multiple times. After a question is given, replace the article (or do not replace it) the minimum number of 'r' can be reached. If the number of 'r' is equal, try to minimize the number of articles.

Solution: it is easy to know that words have a binary relationship. We have a directed edge for each binary relationship, and then draw a graph. The graph may have a strongly connected component (Ring, etc ), so we can find all the contraction points of strongly connected components. The minr and Len of that point are assigned to the minimum minr and Len of the strongly connected component, and then re-create the graph, run a DFS command to obtain the minr and Len of each strongly connected component. Finally, O (n) scans and replaces the word, and counts the word.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> # include <algorithm> # include <string> # include <vector> # include <stack> # include <map> # define INF 0x3f3f3f3f # define lll _ int64using namespace STD; # define n 100007 struct edge {int V, next;} G [4 * n], G2 [4 * n]; string ss [N]; Map <string, int> MP; int minr [4 * n], Len [4 * n]; int Nr [4 * n], nlen [4 * n]; int head [4 * n], TOT, CNT, vis [4 * n]; int Last [4 * n], tot2; stack <int> STK; int instk [4 * n], now, time; int low [4 * n], dfn [4 * n], bel [4 * n]; lll sumr, sumlen; void addedge (int u, int v) {G [tot]. V = V; G [tot]. next = head [u]; head [u] = tot ++;} void addedge2 (int u, int v) // Create a New Graph {G2 [tot2]. V = V; G2 [tot2]. next = last [u]; last [u] = tot2 ++;} void Tarjan (int u) {LOW [u] = dfn [u] = ++ time; STK. push (U); instk [u] = 1; for (INT I = head [u]; I! =-1; I = G [I]. Next) {int v = G [I]. V; If (! Dfn [v]) {Tarjan (V); low [u] = min (low [u], low [v]);} else if (instk [v]) low [u] = min (low [u], dfn [v]);} If (low [u] = dfn [u]) {CNT ++; int V; do {v = STK. top (); STK. pop (); instk [v] = 0; bel [v] = CNT; if (minr [v] <Nr [CNT] | (minr [v] = nR [CNT] & Len [v] <nlen [CNT]) NR [CNT] = minr [v], nlen [CNT] = Len [v];} while (u! = V) ;}} void Tarjan () {memset (BEL, 0, sizeof (BEL); memset (instk, 0, sizeof (instk); memset (dfn, 0, sizeof (dfn); memset (low, 0, sizeof (low); time = 0, CNT = 0; while (! STK. Empty () STK. Pop (); int I; for (I = 1; I <= now; I ++) if (! Dfn [I]) Tarjan (I);} void build () {int I, j; memset (last,-1, sizeof (last); tot2 = 0; for (I = 1; I <= now; I ++) {for (j = head [I]; J! =-1; j = G [J]. Next) {int v = G [J]. V; If (bel [I]! = Bel [v]) addedge2 (bel [I], bel [v]) ;}} void DFS (INT U) {If (vis [u]) return; vis [u] = 1; for (INT I = last [u]; I! =-1; I = G2 [I]. next) {int v = G2 [I]. v; DFS (V); If (NR [v] <Nr [u]) | (NR [v] = nR [u] & nlen [v] <nlen [u]) Nr [u] = nR [v], nlen [u] = nlen [v] ;}} int main () {int n, m, I, j, Len; while (scanf ("% d ", & N )! = EOF) {now = 0; MP. clear (); Tot = 0; memset (Head,-1, sizeof (head); memset (VIS, 0, sizeof (VIS); for (I = 1; I <= N; I ++) {CIN> SS [I]; Len = ss [I]. length (); int cntr = 0; For (j = 0; j <Len; j ++) {If (ss [I] [J]> = 'A' & SS [I] [J] <= 'Z ') ss [I] [J] = ss [I] [J]-'A' + 'a'; If (ss [I] [J] = 'R ') cntr ++;} If (! MP [ss [I]) MP [ss [I] = ++ now; Len [MP [ss [I] = Len; minr [MP [ss [I] = cntr;} scanf ("% d", & M); string SA, Sb; for (I = 1; I <= m; I ++) {SA = "", SB = ""; CIN> Sa> Sb; Len = sa. length (); int cntr = 0; For (j = 0; j <Len; j ++) {If (SA [J]> = 'A' & SA [J] <= 'Z ') sa [J] = sa [J]-'A' + 'a'; If (SA [J] = 'R') cntr ++;} If (! MP [SA]) MP [SA] = ++ now; int A = MP [SA]; Len [a] = Len; minr [a] = cntr; Len = sb. length (); cntr = 0; For (j = 0; j <Len; j ++) {If (SB [J]> = 'A' & SB [J] <= 'Z ') SB [J] = Sb [J]-'A' + 'a'; If (SB [J] = 'R') cntr ++;} If (! MP [SB]) MP [SB] = ++ now; int B = MP [SB]; Len [B] = Len; minr [B] = cntr; addedge (, b);} memset (NR, INF, sizeof (NR); // minr, Len memset (nlen, INF, sizeof (nlen) of the vertex of the new graph )); tarjan (); Build (); for (I = 1; I <= now; I ++) if (! Vis [I]) DFS (I); sumr = 0, sumlen = 0; for (I = 1; I <= N; I ++) {int u = bel [MP [ss [I]; sumr + = nR [u]; sumlen + = nlen [u];} printf ("% i64d % i64d \ n", sumr, sumlen);} return 0 ;}
View code

 

Another way to do this is to create a reverse graph, and then use sort to prioritize DFS from the optimum and finally get the result. But I don't know why this is correct. If you know, so please let me know with comments :)

Codeforces round #267 div.2 D Fedor and essay -- strongly connected DFS

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.