zoukankan      html  css  js  c++  java
  • Codeforces Round #460 (Div. 2) D. Substring

    D. Substring


     

    You are given a graph with n nodes and m directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is 3. Your task is find a path whose value is the largest.

    Input

    The first line contains two positive integers n, m (1 ≤ n, m ≤ 300 000), denoting that the graph has n nodes and m directed edges.

    The second line contains a string s with only lowercase English letters. The i-th character is the letter assigned to the i-th node.

    Then m lines follow. Each line contains two integers x, y (1 ≤ x, y ≤ n), describing a directed edge from x to y. Note that x can be equal to y and there can be multiple edges between xand y. Also the graph can be not connected.

    Output

    Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.

    Examples

    input

    5 4
    abaca
    1 2
    1 3
    3 4
    4 5
    output
    3

    input

    6 6
    xzyabc
    1 2
    3 1
    2 3
    5 4
    4 3
    6 4

    output

    -1

    input

    10 14
    xzyzyzyzqx
    1 2
    2 4
    3 5
    4 5
    2 6
    6 8
    6 5
    2 10
    3 9
    10 9
    4 6
    1 10
    2 8
    3 7

    output

    4

    Note

    In the first sample, the path with largest value is 1 → 3 → 4 → 5. The value is 3 because the letter 'a' appears 3 times.

     题意 :给你一个n长度的字符串 和一个m条边的图 字符串是图的顶点上的字符 问在图上的一条路上有几个相同字符 输出最多的字符个数,有环输 -1

    1 拓扑排序 +dfs 搜索  

      搜索每个顶点的路上出现每个字母出现的最大次数(次数可能为0 因为这TLE了好多发),输出最大的就行 

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,m;
     4 int flag=0;
     5 vector<int>q[300007];
     6 int vi[300007][29];
     7 char s[300007];
     8 int ru[300007];
     9 #define rd(a) scanf("%d",&a)
    10 int dfs2(int i,char c)
    11 {    
    12     if(vi[i][c-'a']!=-1) return vi[i][c-'a'];
    13     int maxx=0;
    14     for(int j=0;j<q[i].size();j++)
    15     {
    16         
    17         maxx=max(dfs2(q[i][j],c),maxx);
    18     }
    19     if(s[i]==c) return vi[i][c-'a']=maxx+1;
    20     else return vi[i][c-'a']=maxx;
    21     
    22 }
    23 int main()
    24 {
    25     while(~scanf("%d%d",&n,&m))    
    26     {
    27 
    28         scanf(" %s",s+1);
    29         for(int i=0;i<m;i++)
    30         {
    31             int u,v;
    32             rd(u),rd(v);
    33             ru[v]++;
    34             q[u].push_back(v);
    35         }
    36         stack<int>qq;
    37         int cnt=0;
    38         for(int i=1;i<=n;i++)
    39         {
    40             
    41             if(!ru[i])
    42             {
    43                  qq.push(i);cnt++;
    44             }
    45         }    
    46         
    47         while(!qq.empty())
    48         {
    49             int v=qq.top();
    50             qq.pop();
    51             for(int i=0;i<q[v].size();i++)
    52             {    
    53                 ru[q[v][i]]--;
    54                 if(!ru[q[v][i]]) qq.push(q[v][i]),cnt++;;
    55             }
    56         }
    57         if(cnt<n)
    58         {
    59             printf("-1
    "); return 0;
    60         }
    61         memset(vi,-1,sizeof(vi));
    62         int maxx=0;
    63         for(int i=1;i<=n;i++)
    64         {
    65             for(int j='a';j<='z';j++)
    66             {
    67                 maxx=max(maxx,dfs2(i,j));
    68             }
    69             
    70         }
    71         printf("%d
    ",maxx);
    72         
    73         
    74     }
    75         
    76     
    77     
    78     return 0;
    79 }
    80 
    81 
    82 
    83 
    84  
    View Code

    2  dfs判环 +dfs 搜索

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,m;
     4 int vis[300007];
     5 int flag=0;
     6 vector<int>q[300007];
     7 int vi[300007][29];
     8 char s[300007];
     9 bool dfs(int a)
    10 {
    11     if(vis[a]==2)     return true;
    12     if(vis[a]==1) return false;
    13     vis[a]=1;
    14     for(int i=0;i<q[a].size();i++)
    15     {
    16         if(!dfs(q[a][i])) return false;
    17     }
    18     vis[a]=2;
    19     return true;
    20 }
    21 #define rd(a) scanf("%d",&a)
    22 int dfs2(int i,char c)
    23 {    
    24     if(vi[i][c-'a']!=-1) return vi[i][c-'a'];
    25     int maxx=0;
    26     for(int j=0;j<q[i].size();j++)
    27     {
    28         
    29         maxx=max(dfs2(q[i][j],c),maxx);
    30     }
    31     if(s[i]==c) return vi[i][c-'a']=maxx+1;
    32     else return vi[i][c-'a']=maxx;
    33     
    34 }
    35 int main()
    36 {
    37     while(~scanf("%d%d",&n,&m))    
    38     {
    39 
    40         scanf(" %s",s+1);
    41         for(int i=0;i<m;i++)
    42         {
    43             int u,v;
    44             rd(u),rd(v);
    45     
    46             q[u].push_back(v);
    47         }
    48         for(int i=1;i<=n;i++)
    49         {    
    50             if(!vis[i]&&!dfs(i))
    51             {
    52                 printf("-1
    ");
    53                 return 0;
    54             }
    55             
    56             
    57         }
    58         memset(vi,-1,sizeof(vi));
    59         int maxx=0;
    60         for(int i=1;i<=n;i++)
    61         {
    62             for(int j='a';j<='z';j++)
    63             {
    64                 maxx=max(maxx,dfs2(i,j));
    65             }
    66             
    67         }
    68         printf("%d
    ",maxx);
    69         
    70         
    71     }
    72         
    73     
    74     
    75     return 0;
    76 }
    View Code
    你要知道你什么都没有,你要靠自己。
  • 相关阅读:
    简单三层代码生成器原理剖析
    深入浅出三层架构
    SQL Server数据的批量导入
    BULK INSERT如何将大量数据高效地导入SQL Server
    用扩展方法优化多条件查询
    SQL Server 数据库访问通用类(更新)
    SQL中自增量字段Int型 identity(1,1)与Guid作为主键的性能比较
    MD5加盐实现登录
    Pi币
    SQL Server 分割字符串转列
  • 原文地址:https://www.cnblogs.com/qq1976648487/p/8398017.html
Copyright © 2011-2022 走看看