zoukankan      html  css  js  c++  java
  • Reposts CodeForces

    One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.

    These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.

    Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.

    Input

    The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.

    We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

    Output

    Print a single integer — the maximum length of a repost chain.

    Example

    Input
    5
    tourist reposted Polycarp
    Petr reposted Tourist
    WJMZBMR reposted Petr
    sdya reposted wjmzbmr
    vepifanov reposted sdya
    Output
    6
    Input
    6
    Mike reposted Polycarp
    Max reposted Polycarp
    EveryOne reposted Polycarp
    111 reposted Polycarp
    VkCup reposted Polycarp
    Codeforces reposted Polycarp
    Output
    2
    Input
    1
    SoMeStRaNgEgUe reposted PoLyCaRp
    Output
    2
    题解:注意两点,此题如果建图则不会出现环,此外,边也是有向边(理解题意超级重要)。然后普通的DFS就能过了,又熟悉了一波map的用法。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int n,tot,ans;
     5 int head[205];
     6 map<string,int> p;
     7 
     8 struct node{
     9     int to,next;
    10 }e[405];
    11 
    12 void Inite(){
    13     ans=tot=0;
    14     memset(head,-1,sizeof(head));
    15 }
    16 
    17 void addedge(int u,int v){
    18     e[tot].to=v;
    19     e[tot].next=head[u];
    20     head[u]=tot++;
    21 }
    22 
    23 void DFS(int v,int d){
    24     ans=max(ans,d);
    25     for(int i=head[v];i!=-1;i=e[i].next) DFS(e[i].to,d+1);
    26 }
    27 
    28 string change(string s){
    29     for(int i=0;i<s.size();i++) s[i]=tolower(s[i]);
    30     return s;
    31 }
    32 
    33 int main()
    34 {   cin>>n;
    35     Inite();
    36     int cnt=0;
    37     while(n--){
    38         string a,b,c;
    39         cin>>a>>b>>c;
    40         a=change(a);
    41         c=change(c);
    42         if(!p[a]) p[a]=++cnt;
    43         if(!p[c]) p[c]=++cnt;
    44         addedge(p[a],p[c]);
    45     }
    46     for(int i=1;i<=cnt;i++) DFS(i,1);
    47     cout<<ans<<endl;
    48 }
  • 相关阅读:
    用户控件包装器的设计与实现
    rskeymgmt 实用工具
    外连接(outer join)
    SQL Server中 char与varchar  
    正则表达式的优先级顺序
    正则表达式语法
    SQL各个子句: outer join,on,where,group by,having,select case子句执行
    SQL语法手册
    ACESS编程中判断空的sql语句
    Sql Server 中一个非常强大的日期格式化函数
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7858393.html
Copyright © 2011-2022 走看看